chatbot-template/components/chat-panel.tsx

140 lines
4.6 KiB
TypeScript
Raw Normal View History

import * as React from 'react'
import { shareChat } from '@/app/actions'
import { Button } from '@/components/ui/button'
import { PromptForm } from '@/components/prompt-form'
2023-06-11 22:58:00 +04:00
import { ButtonScrollToBottom } from '@/components/button-scroll-to-bottom'
2024-03-14 20:00:52 +03:00
import { IconShare } from '@/components/ui/icons'
2023-06-16 17:06:23 +04:00
import { FooterText } from '@/components/footer'
import { ChatShareDialog } from '@/components/chat-share-dialog'
2024-03-14 20:00:52 +03:00
import { useAIState, useActions, useUIState } from 'ai/rsc'
import type { AI } from '@/lib/chat/actions'
import { nanoid } from 'nanoid'
import { UserMessage } from './stocks/message'
2024-03-14 20:00:52 +03:00
export interface ChatPanelProps {
id?: string
title?: string
2024-03-14 20:00:52 +03:00
input: string
setInput: (value: string) => void
2024-03-20 04:37:08 +03:00
isAtBottom: boolean
scrollToBottom: () => void
}
2024-03-20 04:37:08 +03:00
export function ChatPanel({
id,
title,
input,
setInput,
isAtBottom,
scrollToBottom
}: ChatPanelProps) {
2024-03-14 20:00:52 +03:00
const [aiState] = useAIState()
const [messages, setMessages] = useUIState<typeof AI>()
const { submitUserMessage } = useActions()
const [shareDialogOpen, setShareDialogOpen] = React.useState(false)
2024-03-14 20:00:52 +03:00
const exampleMessages = [
{
heading: 'What are the',
subheading: 'trending memecoins today?',
message: `What are the trending memecoins today?`
2024-03-14 20:00:52 +03:00
},
{
heading: 'What is the price of',
2024-03-20 04:37:08 +03:00
subheading: '$DOGE right now?',
message: 'What is the price of $DOGE right now?'
2024-03-14 20:00:52 +03:00
},
{
heading: 'I would like to buy',
2024-03-20 04:37:08 +03:00
subheading: '42 $DOGE',
message: `I would like to buy 42 $DOGE`
2024-03-14 20:00:52 +03:00
},
{
heading: 'What are some',
2024-03-20 04:37:08 +03:00
subheading: `recent events about $DOGE?`,
message: `What are some recent events about $DOGE?`
2024-03-14 20:00:52 +03:00
}
]
return (
2024-03-14 20:00:52 +03:00
<div className="fixed inset-x-0 bottom-0 w-full bg-gradient-to-b from-muted/30 from-0% to-muted/30 to-50% duration-300 ease-in-out animate-in dark:from-background/10 dark:from-10% dark:to-background/80 peer-[[data-state=open]]:group-[]:lg:pl-[250px] peer-[[data-state=open]]:group-[]:xl:pl-[300px]">
2024-03-20 04:37:08 +03:00
<ButtonScrollToBottom
isAtBottom={isAtBottom}
scrollToBottom={scrollToBottom}
/>
2024-03-14 20:00:52 +03:00
<div className="mx-auto sm:max-w-2xl sm:px-4">
2024-03-14 20:00:52 +03:00
<div className="mb-4 grid grid-cols-2 gap-2 px-4 sm:px-0">
{messages.length === 0 &&
exampleMessages.map((example, index) => (
<div
key={example.heading}
className={`cursor-pointer rounded-lg border bg-white p-4 hover:bg-zinc-50 dark:bg-zinc-950 dark:hover:bg-zinc-900 ${
index > 1 && 'hidden md:block'
}`}
onClick={async () => {
setMessages(currentMessages => [
...currentMessages,
{
id: nanoid(),
display: <UserMessage>{example.message}</UserMessage>
}
])
const responseMessage = await submitUserMessage(
example.message
)
setMessages(currentMessages => [
...currentMessages,
responseMessage
])
}}
>
<div className="text-sm font-semibold">{example.heading}</div>
<div className="text-sm text-zinc-600">
{example.subheading}
</div>
</div>
2024-03-14 20:00:52 +03:00
))}
</div>
2024-03-14 20:00:52 +03:00
{messages?.length >= 2 ? (
<div className="flex h-12 items-center justify-center">
<div className="flex space-x-2">
{id && title ? (
<>
<Button
variant="outline"
onClick={() => setShareDialogOpen(true)}
>
<IconShare className="mr-2" />
Share
</Button>
<ChatShareDialog
open={shareDialogOpen}
onOpenChange={setShareDialogOpen}
onCopy={() => setShareDialogOpen(false)}
shareChat={shareChat}
chat={{
id,
title,
messages: aiState.messages
}}
/>
</>
) : null}
</div>
</div>
) : null}
<div className="space-y-4 border-t bg-background px-4 py-2 shadow-lg sm:rounded-t-xl sm:border md:py-4">
<PromptForm input={input} setInput={setInput} />
2023-06-16 17:06:23 +04:00
<FooterText className="hidden sm:block" />
</div>
</div>
</div>
)
}