33import { Input } from "@/components/ui/input" ;
44import { Search , X } from "lucide-react" ;
55import { useRouter , useSearchParams , usePathname } from "next/navigation" ;
6- import { useDebouncedCallback } from "use-debounce" ;
76import { Button } from "@/components/ui/button" ;
8- import { useState } from "react" ;
7+ import { useState , useEffect } from "react" ;
98
109export function SearchBar ( ) {
1110 const router = useRouter ( ) ;
1211 const pathname = usePathname ( ) ;
1312 const searchParams = useSearchParams ( ) ;
14- const [ searchTerm , setSearchTerm ] = useState ( searchParams . get ( 'message' ) ?? '' ) ;
13+ const [ inputValue , setInputValue ] = useState ( '' ) ;
1514
16- const handleSearch = useDebouncedCallback ( ( term : string ) => {
15+ useEffect ( ( ) => {
16+ setInputValue ( searchParams . get ( 'message' ) ?? '' ) ;
17+ } , [ searchParams ] ) ;
18+
19+ const handleSearch = ( term : string ) => {
1720 const params = new URLSearchParams ( searchParams . toString ( ) ) ;
1821
1922 if ( term ) {
@@ -23,26 +26,30 @@ export function SearchBar() {
2326 }
2427
2528 router . replace ( `${ pathname } ?${ params . toString ( ) } ` ) ;
26- } , 300 ) ;
29+ } ;
2730
2831 const handleClear = ( ) => {
29- setSearchTerm ( '' ) ;
32+ setInputValue ( '' ) ;
3033 handleSearch ( '' ) ;
3134 } ;
3235
36+ const handleKeyDown = ( e : React . KeyboardEvent < HTMLInputElement > ) => {
37+ if ( e . key === 'Enter' ) {
38+ handleSearch ( inputValue ) ;
39+ }
40+ } ;
41+
3342 return (
3443 < div className = "relative flex-1" >
3544 < Search className = "absolute left-3 top-3 h-4 w-4 text-[var(--button-text)]" />
3645 < Input
3746 placeholder = "Search logs..."
3847 className = "h-10 pl-9 pr-9 border-[var(--border-gray)]"
39- value = { searchTerm }
40- onChange = { ( e ) => {
41- setSearchTerm ( e . target . value ) ;
42- handleSearch ( e . target . value ) ;
43- } }
48+ value = { inputValue }
49+ onChange = { ( e ) => setInputValue ( e . target . value ) }
50+ onKeyDown = { handleKeyDown }
4451 />
45- { searchTerm && (
52+ { inputValue && (
4653 < Button
4754 variant = "ghost"
4855 size = "icon"
0 commit comments