chatbot-template/components/chat.tsx

108 lines
3.2 KiB
TypeScript
Raw Normal View History

'use client'
2023-06-16 22:43:24 +02:00
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'
2023-06-15 15:27:41 +04:00
import { ChatScrollAnchor } from '@/components/chat-scroll-anchor'
2023-06-16 16:30:15 -04:00
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 { useRouter } from 'next/navigation'
2023-06-16 16:46:45 -04:00
const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
export interface ChatProps extends React.ComponentProps<'div'> {
initialMessages?: Message[]
id?: string
}
2023-06-16 21:52:01 +04:00
export function Chat({ id, initialMessages, className }: ChatProps) {
const router = useRouter()
2023-06-16 16:30:15 -04:00
const [previewToken, setPreviewToken] = useLocalStorage<string | null>(
'ai-token',
null
)
2023-06-16 16:46:45 -04:00
const [previewTokenDialog, setPreviewTokenDialog] = useState(IS_PREVIEW)
2023-06-16 16:30:15 -04:00
const [previewTokenInput, setPreviewTokenInput] = useState(previewToken ?? '')
const { messages, append, reload, stop, isLoading, input, setInput } =
useChat({
initialMessages,
2023-06-15 11:31:03 +04:00
id,
body: {
2023-06-16 16:30:15 -04:00
id,
previewToken
2023-06-15 11:31:03 +04:00
}
})
return (
<>
<div className={cn('pb-[200px] pt-4 md:pt-10', className)}>
{messages.length ? (
2023-06-15 15:27:41 +04:00
<>
<ChatList messages={messages} />
<ChatScrollAnchor trackVisibility={isLoading} />
</>
) : (
<EmptyScreen setInput={setInput} />
)}
</div>
<ChatPanel
id={id}
isLoading={isLoading}
stop={stop}
append={append}
reload={reload}
messages={messages}
input={input}
setInput={setInput}
/>
2023-06-16 16:46:45 -04:00
<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"
2023-06-16 16:30:15 -04:00
>
2023-06-16 16:46:45 -04:00
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>
</>
)
}