@@ -4,6 +4,7 @@ import { X } from "lucide-react";
44import Link from "next/link" ;
55import { useRouter } from "next/navigation" ;
66import { useEffect , useState } from "react" ;
7+ import { useDebounce } from "use-debounce" ;
78import { Badge } from "~/components/ui/badge" ;
89import { Button } from "~/components/ui/button" ;
910import {
@@ -30,22 +31,27 @@ import { useTRPC } from "~/trpc/react";
3031const MIN_QUERY_LENGTH = 2 ;
3132const DEFAULT_THRESHOLD = 0.25 ;
3233const DEFAULT_LIMIT = 10 ;
34+ const DEFAULT_DEBOUNCE_TIME = 500 ;
35+
3336type HeaderSearchButtonProps = React . ComponentProps < typeof Dialog > & {
3437 children : React . ReactNode ;
3538 limit ?: number ;
3639 threshold ?: number ;
40+ debounceTime ?: number ;
3741} ;
3842
3943export function HeaderSearchButton ( {
4044 children,
4145 limit = DEFAULT_LIMIT ,
4246 threshold = DEFAULT_THRESHOLD ,
47+ debounceTime = DEFAULT_DEBOUNCE_TIME ,
4348 ...rest
4449} : HeaderSearchButtonProps ) {
4550 const isMobile = useIsMobile ( ) ;
4651 const [ isOpen , setIsOpen ] = useState ( false ) ;
4752 const [ query , setQuery ] = useState ( "" ) ;
4853 const router = useRouter ( ) ;
54+ const [ debouncedQuery ] = useDebounce ( query , debounceTime ) ;
4955
5056 // Keyboard shortcut handlers
5157 useEffect ( ( ) => {
@@ -65,13 +71,17 @@ export function HeaderSearchButton({
6571 } , [ isOpen ] ) ;
6672
6773 const trpc = useTRPC ( ) ;
68- const { data : notes , isLoading } = useQuery ( {
74+ const {
75+ data : notes ,
76+ isLoading,
77+ isError,
78+ } = useQuery ( {
6979 ...trpc . notes . getSimilarNotes . queryOptions ( {
7080 limit,
71- query,
81+ query : debouncedQuery ,
7282 threshold,
7383 } ) ,
74- enabled : query . length > MIN_QUERY_LENGTH ,
84+ enabled : debouncedQuery . length > MIN_QUERY_LENGTH ,
7585 } ) ;
7686
7787 return (
@@ -97,7 +107,6 @@ export function HeaderSearchButton({
97107 autoCorrect = "off"
98108 spellCheck = { false }
99109 autoFocus
100- value = { query }
101110 onValueChange = { ( value ) => setQuery ( value ) }
102111 />
103112 </ div >
@@ -113,7 +122,9 @@ export function HeaderSearchButton({
113122 </ div >
114123 ) : ! notes ? (
115124 < span className = "text-muted-foreground text-sm" >
116- Write something to search
125+ { isError
126+ ? "Something went wrong. Please try again later."
127+ : "Write something to search." }
117128 </ span >
118129 ) : (
119130 < span className = "text-muted-foreground text-sm" >
0 commit comments