"use client"; import { useState, useEffect } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Search, X, Loader2 } from "lucide-react"; interface KBSearchBarProps { value: string; onChange: (value: string) => void; onSearch?: () => void; placeholder?: string; isLoading?: boolean; debounceMs?: number; } export function KBSearchBar({ value, onChange, onSearch, placeholder = "Search knowledge base...", isLoading = false, debounceMs = 300, }: KBSearchBarProps) { const [localValue, setLocalValue] = useState(value); // Debounce the onChange callback useEffect(() => { const timer = setTimeout(() => { if (localValue !== value) { onChange(localValue); } }, debounceMs); return () => clearTimeout(timer); }, [localValue, debounceMs, onChange, value]); // Sync external value changes useEffect(() => { setLocalValue(value); }, [value]); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && onSearch) { onChange(localValue); onSearch(); } }; const handleClear = () => { setLocalValue(""); onChange(""); }; return (
setLocalValue(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} className="pl-9 pr-9" /> {localValue && ( )}
{onSearch && ( )}
); }