"use client"; import { useEffect, useRef, useSyncExternalStore } from "react"; import { Search, X } from "lucide-react"; import posthog from "posthog-js"; interface SearchBarProps { value: string; onChange: (value: string) => void; totalCount: number; filteredCount: number; } export function SearchBar({ value, onChange, }: SearchBarProps) { const inputRef = useRef(null); const isMac = useSyncExternalStore( () => () => {}, () => navigator.userAgent.includes("Mac"), () => false, ); const searchTimerRef = useRef | null>(null); useEffect(() => { function handleKeyDown(e: KeyboardEvent) { if ((e.metaKey || e.ctrlKey) && e.key === "k") { e.preventDefault(); inputRef.current?.focus(); } if (e.key === "Escape") { inputRef.current?.blur(); onChange(""); } } document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, [onChange]); return (
{ const next = e.target.value; onChange(next); if (searchTimerRef.current) clearTimeout(searchTimerRef.current); if (next.trim()) { searchTimerRef.current = setTimeout(() => { posthog.capture("icon_searched", { query: next.trim(), query_length: next.trim().length }); }, 800); } }} placeholder="Search icons..." className="h-9 w-full rounded-lg border border-border/60 bg-card/60 pr-20 pl-9 text-sm backdrop-blur-sm outline-none transition-all placeholder:text-muted-foreground/50 focus:border-ring focus:bg-card focus:ring-1 focus:ring-ring" aria-label="Search icons" />
{value && ( )} {isMac ? "\u2318K" : "Ctrl+K"}
); }