-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch-form.tsx
179 lines (159 loc) · 5.31 KB
/
search-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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'
import { useSearchParams } from 'next/navigation'
import { AlertCircleIcon, LoaderCircleIcon } from 'lucide-react'
import { isMobile } from 'react-device-detect'
import { toast } from 'sonner'
import { ClassifyStatus } from '@/types/classify'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'
type FormSearchProps = {
handleSearch: (term: string, save?: boolean) => void
setStatusForm: Dispatch<SetStateAction<ClassifyStatus>>
setPromptEvaluationResult: Dispatch<SetStateAction<string | undefined>>
promptEvaluationResult: string | undefined
}
export function FormSearch({
handleSearch,
setStatusForm,
setPromptEvaluationResult,
promptEvaluationResult
}: FormSearchProps) {
const searchParams = useSearchParams()
const query = searchParams.get('query')?.toString() ?? ''
const [isClassifying, setIsClassifying] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (!inputRef.current) return
const isInForm =
document.activeElement?.tagName === 'INPUT' ||
document.activeElement?.tagName === 'TEXTAREA'
if (event.key.toLowerCase() === 's' && !isInForm) {
event.preventDefault()
inputRef.current.focus()
}
}
window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
}, [])
const search = async (prompt: string) => {
if (prompt.trim().length < 5) {
toast.error('Please enter a valid search term')
return
}
inputRef.current?.blur()
setIsClassifying(true)
const response = await fetch('/api/query-classify', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
input: prompt
})
})
const { category, error } = await response.json()
setIsClassifying(false)
if (error || !category) {
toast.error(`Something went wrong while classifying the query: ${prompt}`)
return
}
if (category === 'non-technical') {
setStatusForm('error')
setPromptEvaluationResult(`No resources for non-technical queries.`)
return
}
handleSearch(prompt, true)
}
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
const input = event.currentTarget.prompt.value
await search(input)
}
return (
<form className='flex w-full items-center px-2 py-1' onSubmit={handleSubmit}>
<div className='relative w-full'>
<label className='sr-only' htmlFor='prompt'>
Prompt
</label>
<Input
key={query}
ref={inputRef}
className='block
h-10
w-full
p-2
border-none
bg-transparent
border
border-input
focus-visible:outline-none
focus-visible:ring-0
focus-visible:ring-offset-0
whitespace-nowrap
overflow-hidden
placeholder:text-neutral-500
focus-within:placeholder:text-neutral-300
dark:placeholder:text-neutral-300
dark:focus-within:placeholder:text-neutral-500
dark:focus-within:text-white
dark:text-neutral-400
focus-within:text-black
text-neutral-900'
id='prompt'
spellCheck='false'
aria-label='Search'
defaultValue={query}
placeholder='Typescript books'
autoComplete='off'
onChange={() => {
if (promptEvaluationResult) {
setStatusForm('idle')
setPromptEvaluationResult(undefined)
}
}}
/>
</div>
<div className='flex justify-end mx-1'>
<div className='flex gap-1'>
<div className='text-black dark:text-yellow-200'>
{promptEvaluationResult && !isMobile && (
<ToolTipError promptEvaluationResult={promptEvaluationResult} />
)}
</div>
{isClassifying && <LoaderCircleIcon className='size-5 animate-spin' />}
{!isMobile && !isClassifying && !promptEvaluationResult && (
<kbd className='bg-light-600 dark:bg-neutral-700 text-light-900 dark:text-white rounded-sm px-2 py-1 text-xs'>
S
</kbd>
)}
</div>
</div>
</form>
)
}
function ToolTipError({ promptEvaluationResult }: { promptEvaluationResult: string }) {
return (
<TooltipProvider delayDuration={100}>
<Tooltip>
<TooltipTrigger asChild>
<div>
<Button
variant='outline'
size='icon'
className='bg-transparent border-none hover:bg-transparent size-5 mt-1.5'
>
<AlertCircleIcon className='text-red-500 dark:text-red-400 size-5' />
</Button>
</div>
</TooltipTrigger>
<TooltipContent side='right' className='border-light-600 dark:border-neutral-800/70'>
<p>{promptEvaluationResult}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)
}