forked from vercel/ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-panel.tsx
139 lines (129 loc) · 4.57 KB
/
chat-panel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as React from 'react'
import { shareChat } from '@/app/actions'
import { Button } from '@/components/ui/button'
import { PromptForm } from '@/components/prompt-form'
import { ButtonScrollToBottom } from '@/components/button-scroll-to-bottom'
import { IconShare } from '@/components/ui/icons'
import { FooterText } from '@/components/footer'
import { ChatShareDialog } from '@/components/chat-share-dialog'
import { useAIState, useActions, useUIState } from 'ai/rsc'
import type { AI } from '@/lib/chat/actions'
import { nanoid } from 'nanoid'
import { UserMessage } from './stocks/message'
export interface ChatPanelProps {
id?: string
title?: string
input: string
setInput: (value: string) => void
isAtBottom: boolean
scrollToBottom: () => void
}
export function ChatPanel({
id,
title,
input,
setInput,
isAtBottom,
scrollToBottom
}: ChatPanelProps) {
const [aiState] = useAIState()
const [messages, setMessages] = useUIState<typeof AI>()
const { submitUserMessage } = useActions()
const [shareDialogOpen, setShareDialogOpen] = React.useState(false)
const exampleMessages = [
{
heading: 'What are the',
subheading: 'trending memecoins today?',
message: `What are the trending memecoins today?`
},
{
heading: 'What is the price of',
subheading: '$DOGE right now?',
message: 'What is the price of $DOGE right now?'
},
{
heading: 'I would like to buy',
subheading: '42 $DOGE',
message: `I would like to buy 42 $DOGE`
},
{
heading: 'What are some',
subheading: `recent events about $DOGE?`,
message: `What are some recent events about $DOGE?`
}
]
return (
<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]">
<ButtonScrollToBottom
isAtBottom={isAtBottom}
scrollToBottom={scrollToBottom}
/>
<div className="mx-auto sm:max-w-2xl sm:px-4">
<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>
))}
</div>
{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} />
<FooterText className="hidden sm:block" />
</div>
</div>
</div>
)
}