"use client"; import { useEffect, useRef, useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { Search, X } from "lucide-react"; export function GridSearchInput() { const router = useRouter(); const params = useSearchParams(); const initial = params.get("q") ?? ""; const [value, setValue] = useState(initial); const debounce = useRef(null); const lastApplied = useRef(initial); useEffect(() => { return () => { if (debounce.current) window.clearTimeout(debounce.current); }; }, []); // Sync state from URL (e.g. when navigating, "All" link clears it). useEffect(() => { const fromUrl = params.get("q") ?? ""; if (fromUrl !== lastApplied.current) { lastApplied.current = fromUrl; setValue(fromUrl); } }, [params]); function apply(next: string) { if (next === lastApplied.current) return; lastApplied.current = next; const sp = new URLSearchParams(params.toString()); if (next.trim()) { sp.set("q", next.trim()); // Activating search clears the letter filter so the user sees all matches. sp.delete("letter"); } else { sp.delete("q"); } router.push(`?${sp.toString()}`, { scroll: false }); } function onChange(e: React.ChangeEvent) { const next = e.target.value; setValue(next); if (debounce.current) window.clearTimeout(debounce.current); debounce.current = window.setTimeout(() => apply(next), 300); } function clear() { setValue(""); if (debounce.current) window.clearTimeout(debounce.current); apply(""); } return (
{ if (e.key === "Enter") { if (debounce.current) window.clearTimeout(debounce.current); apply(value); } else if (e.key === "Escape") { clear(); } }} placeholder="Search Code, Title, Notes…" className="glass rounded-lg pl-8 pr-7 py-1.5 text-sm outline-none focus:border-[var(--color-cyan)] w-56" /> {value && ( )}
); }