"use client"; import { FileSearch, ReceiptText, Search } from "lucide-react"; import { useRouter } from "next/navigation"; import { useEffect, useMemo, useRef, useState } from "react"; import type { QuestionSummary } from "@/lib/types"; type Result = { key: string; label: string; sub: string; href: string; kind: "receipt" | "question" }; export function QuickJump() { const router = useRouter(); const [query, setQuery] = useState(""); const [open, setOpen] = useState(false); const [active, setActive] = useState(0); const [questions, setQuestions] = useState(null); const box = useRef(null); const input = useRef(null); const load = () => { if (questions) return; fetch("/data/questions.json") .then((r) => r.json()) .then(setQuestions) .catch(() => setQuestions([])); }; // Ctrl/Cmd + K focuses the search box. useEffect(() => { const onKey = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") { e.preventDefault(); input.current?.focus(); } }; const onClick = (e: MouseEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false); }; window.addEventListener("keydown", onKey); window.addEventListener("mousedown", onClick); return () => { window.removeEventListener("keydown", onKey); window.removeEventListener("mousedown", onClick); }; }, []); const results = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return []; const out: Result[] = []; const num = q.match(/^#?(\d{1,2})$/); if (num && Number(num[1]) < 100) { out.push({ key: `r${num[1]}`, kind: "receipt", label: `Receipt #${num[1]}`, sub: "Open the receipt", href: `/?sample=${Number(num[1])}` }); } const words = q.split(/\s+/).filter(Boolean); for (const item of questions ?? []) { const hay = `${item.question} ${item.company}`.toLowerCase(); if (words.every((w) => hay.includes(w))) { out.push({ key: `q${item.i}`, kind: "question", label: item.question, sub: item.company, href: `/search/?q=${item.i}` }); if (out.length >= 7) break; } } return out; }, [query, questions]); const go = (r: Result) => { setOpen(false); setQuery(""); router.push(r.href); }; return (
{ load(); setOpen(true); }} onChange={(e) => { setQuery(e.target.value); setActive(0); setOpen(true); }} onKeyDown={(e) => { if (e.key === "ArrowDown") { e.preventDefault(); setActive((a) => Math.min(a + 1, results.length - 1)); } else if (e.key === "ArrowUp") { e.preventDefault(); setActive((a) => Math.max(a - 1, 0)); } else if (e.key === "Enter" && results[active]) { go(results[active]); } else if (e.key === "Escape") { setOpen(false); } }} placeholder="Find a receipt number or a question…" autoComplete="off" role="combobox" aria-expanded={open && results.length > 0} aria-controls="quick-jump-list" className="h-10 w-full min-w-0 rounded-xl border border-line bg-surface-solid pr-3 pl-10 sm:pr-14 text-[14px] text-ink shadow-card placeholder:text-muted focus:border-accent focus:outline-none" /> Ctrl K {open && results.length > 0 && (
    {results.map((r, i) => (
  • ))}
)}
); }