Open History to see the last commit
1'use client'
2
3import {
4 ArrowRightIcon,
5 LoaderCircleIcon,
6 MicIcon,
7 SearchIcon,
8} from 'lucide-react'
9import { useEffect } from 'react'
10import type { FormEvent } from 'react'
11import { useSpeechRecognition } from '@/hooks/useSpeechRecognition'
12
13const PLACEHOLDER = 'e.g. Show all the expired orders created this year'
14
15type SearchFormProps = {
16 value: string
17 onChange: (value: string) => void
18 onSubmit: (event: FormEvent) => void
19 isLoading: boolean
20}
21
22function SearchForm({ onChange, onSubmit, value, isLoading }: SearchFormProps) {
23 const { listening, transcript, startListening, stopListening } =
24 useSpeechRecognition()
25
26 useEffect(() => {
27 if (transcript) {
28 onChange(transcript)
29 }
30 }, [transcript])
31
32 const hasValue = Boolean(value.trim().length)
33
34 function handleQueryChange(event: FormEvent<HTMLInputElement>) {
35 onChange(event.currentTarget.value)
36 }
37
38 function handleVoiceOverClick() {
39 if (listening) {
40 stopListening()
41 } else {
42 startListening()
43 }
44 }
45
46 return (
47 <form onSubmit={onSubmit}>
48 <div className="relative bg-background rounded-2xl">
49 <input
50 className="placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input w-full border bg-background px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none md:text-sm focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] peer ps-11 pe-22 h-14 rounded-2xl"
51 type="search"
52 placeholder={PLACEHOLDER}
53 name="query"
54 value={value}
55 onChange={handleQueryChange}
56 />
57 <div className="pointer-events-none absolute inset-y-0 start-0 flex items-center justify-center ps-4 text-muted-foreground/80">
58 <SearchIcon className="size-4" />
59 </div>
60 <button
61 className="absolute inset-y-3 end-12 flex size-8 items-center justify-center transition-[color,box-shadow,background-color] outline-none hover:text-foreground focus:z-10 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 data-[active=true]:text-blue-600 disabled:pointer-events-none disabled:opacity-40 cursor-pointer bg-input/70 hover:bg-input/50 rounded-full"
62 type="button"
63 data-active={listening}
64 disabled={isLoading}
65 onClick={handleVoiceOverClick}
66 >
67 <MicIcon className="size-4" />
68 </button>
69 <button
70 className="absolute inset-y-0 end-0 flex h-full w-11 items-center justify-center rounded-e-2xl text-muted-foreground/80 transition-[color,box-shadow,background-color] outline-none hover:text-foreground focus:z-10 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 cursor-pointer pr-2"
71 type="submit"
72 disabled={!hasValue || isLoading}
73 >
74 {isLoading ? (
75 <LoaderCircleIcon className="size-4 animate-spin" />
76 ) : (
77 <ArrowRightIcon className="size-4" />
78 )}
79 </button>
80 </div>
81 </form>
82 )
83}
84
85export default SearchForm