Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

Search.tsx

41 lines912 Bytes

1'use client'
2​
3import { useState } from 'react'
4import QuickPrompts from './QuickPrompts'
5import SearchForm from './SearchForm'
6import type { FormEvent } from 'react'
7import useSearchMutation from '@/hooks/useSearchMutation.ts'
8​
9function Search() {
10 const [value, setValue] = useState('')
11​
12 const { mutate: search, isPending, error } = useSearchMutation()
13​
14 function handleSubmit(event: FormEvent) {
15 event.preventDefault()
16​
17 if (value.trim() && !isPending) {
18 search(value)
19 setValue('')
20 }
21 }
22​
23 return (
24 <div className="space-y-4">
25 <SearchForm
26 value={value}
27 onChange={setValue}
28 onSubmit={handleSubmit}
29 isLoading={isPending}
30 />
31 {error && (
32 <p className="text-red-500 text-center text-sm">
33 Error: {error.message}
34 </p>
35 )}
36 <QuickPrompts onClick={setValue} />
37 </div>
38 )
40​
41export default Search