-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprompt-form.tsx
executable file
·109 lines (103 loc) · 3.77 KB
/
prompt-form.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
import { Button } from '@/components/ui/button'
import { IconChat, IconCommand, IconSpinner } from '@/components/ui/icons'
import { useEnterSubmit } from '@/lib/hooks/use-enter-submit'
import { UseChatHelpers } from 'ai/react'
import * as React from 'react'
import Textarea from 'react-textarea-autosize'
import { HoverCard, HoverCardTrigger, HoverCardContent } from '@/components/ui/hovercard'
export interface PromptProps
extends Pick<UseChatHelpers, 'input' | 'setInput'> {
onSubmit: (value: string) => Promise<void>
isLoading: boolean
}
export function PromptForm({
onSubmit,
input,
setInput,
isLoading
}: PromptProps) {
const { formRef, onKeyDown } = useEnterSubmit()
const inputRef = React.useRef<HTMLTextAreaElement>(null)
React.useEffect(() => {
if (inputRef.current) {
inputRef.current.focus()
}
}, [])
return (
<form
onSubmit={async e => {
e.preventDefault()
if (!input?.trim()) {
return
}
setInput('')
await onSubmit(input)
}}
ref={formRef}
>
<div className="bg-background relative flex max-h-60 w-full grow flex-col overflow-hidden px-2 pb-2 sm:rounded-2xl sm:border">
<div className="flex w-full flex-col">
<label
htmlFor="playground"
className="text-config text-foreground flex justify-between gap-y-4 rounded-xl px-3 py-4 font-medium leading-6 md:flex-row md:items-center md:gap-y-0"
>
<div className="flex items-center gap-x-2">
<IconChat
className="text-muted-foreground/50 h-5 w-5"
aria-hidden="true"
/>
<h3>Ask</h3>
<HoverCard>
<HoverCardTrigger asChild>
<Button variant="link" size="lg" className="text-inherit">@conversation tips</Button>
</HoverCardTrigger>
<HoverCardContent>
<ul className="list-disc pl-4">
<li>Say Hello to start a conversation, or simply describe your spreadsheet problem in detail.</li>
<li>ExcelMaster is an AI assistant that specializes in Excel formulas. It provides easy-to-understand, step-by-step solutions using Excel formulas, along with tips and optimizations.</li>
<li>To get the best results, try to provide simple, clear and step-by-step instruction to your spreadsheet problem.</li>
</ul>
</HoverCardContent>
</HoverCard>
</div>
<div className="flex items-center justify-center gap-2 md:justify-start">
{/* Reset chat */}
<Button
variant="ghost"
className="max-w-xs"
onClick={e => {
e.preventDefault()
location.reload()
}}
>
Reset
</Button>
{/* Send button */}
<Button type="submit" disabled={isLoading || input === ''}>
{isLoading ? (
<IconSpinner />
) : (
<IconCommand className="size-4" />
)}
Send
<span className="sr-only">Send message</span>
</Button>
</div>
</label>
</div>
<Textarea
ref={inputRef}
tabIndex={0}
onKeyDown={onKeyDown}
rows={1}
maxRows={10}
value={input}
onChange={e => setInput(e.target.value)}
placeholder="Enter your prompt message..."
spellCheck={false}
className="bg-muted min-h-[60px] w-full resize-none rounded-lg px-4 py-[1.3rem] focus-within:outline-none sm:text-sm"
/>
</div>
</form>
)
}