Refactor to use ai/rsc (#253)

This commit is contained in:
Jeremy 2024-03-14 20:00:52 +03:00 committed by GitHub
parent 69ca8fcc22
commit e85ba803dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
66 changed files with 2799 additions and 740 deletions

View file

@ -1,61 +1,60 @@
'use client'
import { useChat, type Message } from 'ai/react'
import { cn } from '@/lib/utils'
import { ChatList } from '@/components/chat-list'
import { ChatPanel } from '@/components/chat-panel'
import { EmptyScreen } from '@/components/empty-screen'
import { ChatScrollAnchor } from '@/components/chat-scroll-anchor'
import { useLocalStorage } from '@/lib/hooks/use-local-storage'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog'
import { useState } from 'react'
import { Button } from './ui/button'
import { Input } from './ui/input'
import { toast } from 'react-hot-toast'
import { useEffect, useState } from 'react'
import { useUIState, useAIState } from 'ai/rsc'
import { Session } from '@/lib/types'
import { usePathname, useRouter } from 'next/navigation'
import { Message } from '@/lib/chat/actions'
import { toast } from 'sonner'
const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
export interface ChatProps extends React.ComponentProps<'div'> {
initialMessages?: Message[]
id?: string
session?: Session
missingKeys: string[]
}
export function Chat({ id, initialMessages, className }: ChatProps) {
export function Chat({ id, className, session, missingKeys }: ChatProps) {
const router = useRouter()
const path = usePathname()
const [previewToken, setPreviewToken] = useLocalStorage<string | null>(
'ai-token',
null
)
const [previewTokenDialog, setPreviewTokenDialog] = useState(IS_PREVIEW)
const [previewTokenInput, setPreviewTokenInput] = useState(previewToken ?? '')
const { messages, append, reload, stop, isLoading, input, setInput } =
useChat({
initialMessages,
id,
body: {
id,
previewToken
},
onResponse(response) {
if (response.status === 401) {
toast.error(response.statusText)
}
},
onFinish() {
if (!path.includes('chat')) {
window.history.pushState({}, '', `/chat/${id}`)
}
const [input, setInput] = useState('')
const [messages] = useUIState()
const [aiState] = useAIState()
const isLoading = true
const [_, setNewChatId] = useLocalStorage('newChatId', id)
useEffect(() => {
if (session?.user) {
if (!path.includes('chat') && messages.length === 1) {
window.history.replaceState({}, '', `/chat/${id}`)
}
}
}, [id, path, session?.user, messages])
useEffect(() => {
const messagesLength = aiState.messages?.length
if (messagesLength === 2) {
router.refresh()
}
}, [aiState.messages, router])
useEffect(() => {
setNewChatId(id)
})
useEffect(() => {
missingKeys.map(key => {
toast.error(`Missing ${key} environment variable!`)
})
}, [missingKeys])
return (
<>
<div className={cn('pb-[200px] pt-4 md:pt-10', className)}>
@ -68,52 +67,7 @@ export function Chat({ id, initialMessages, className }: ChatProps) {
<EmptyScreen setInput={setInput} />
)}
</div>
<ChatPanel
id={id}
isLoading={isLoading}
stop={stop}
append={append}
reload={reload}
messages={messages}
input={input}
setInput={setInput}
/>
<Dialog open={previewTokenDialog} onOpenChange={setPreviewTokenDialog}>
<DialogContent>
<DialogHeader>
<DialogTitle>Enter your OpenAI Key</DialogTitle>
<DialogDescription>
If you have not obtained your OpenAI API key, you can do so by{' '}
<a
href="https://platform.openai.com/signup/"
className="underline"
>
signing up
</a>{' '}
on the OpenAI website. This is only necessary for preview
environments so that the open source community can test the app.
The token will be saved to your browser&apos;s local storage under
the name <code className="font-mono">ai-token</code>.
</DialogDescription>
</DialogHeader>
<Input
value={previewTokenInput}
placeholder="OpenAI API key"
onChange={e => setPreviewTokenInput(e.target.value)}
/>
<DialogFooter className="items-center">
<Button
onClick={() => {
setPreviewToken(previewTokenInput)
setPreviewTokenDialog(false)
}}
>
Save Token
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<ChatPanel id={id} input={input} setInput={setInput} />
</>
)
}