"use client"; import { ChevronDown, ChevronLeft, ChevronRight, Search } from "lucide-react"; import { useEffect, useMemo, useRef, useState } from "react"; import { TYPE_LABEL, type QuestionDetail, type QuestionSummary } from "@/lib/types"; export function QuestionPicker({ questions, value, onChange, }: { questions: QuestionSummary[]; value: number; onChange: (i: number) => void; }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const [type, setType] = useState("all"); const box = useRef(null); const current = questions.find((q) => q.i === value); useEffect(() => { const onClick = (e: MouseEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false); }; window.addEventListener("mousedown", onClick); return () => window.removeEventListener("mousedown", onClick); }, []); const list = useMemo(() => { const words = query.toLowerCase().split(/\s+/).filter(Boolean); return questions.filter((q) => (type === "all" || q.type === type) && words.every((w) => `${q.question} ${q.company}`.toLowerCase().includes(w))); }, [questions, query, type]); const pos = questions.findIndex((q) => q.i === value); const step = (d: number) => onChange(questions[(pos + d + questions.length) % questions.length].i); return (
{open && (
    {list.map((q) => (
  • ))} {list.length === 0 &&
  • No question matches.
  • }
)}
); } /** Loads one question's detail file (routed filings, every method's top 10, page texts). */ export function useQuestion(i: number) { const [detail, setDetail] = useState(null); const [error, setError] = useState(false); useEffect(() => { let live = true; setError(false); fetch(`/data/q/${i}.json`) .then((r) => (r.ok ? r.json() : Promise.reject())) .then((d) => live && setDetail(d)) .catch(() => live && setError(true)); return () => { live = false; }; }, [i]); return { detail: detail?.i === i ? detail : null, error }; } const STOP = new Set( "what which with were from that this have been their there about than then into over under does did was were how many much per for and the are year fiscal company total amount based using shown give response question relying details answer please calculate round decimal places line items clearly".split(" "), ); /** Words from the question worth highlighting in a page. */ export function keyTerms(question: string): string[] { const words = question.toLowerCase().match(/[a-z0-9][a-z0-9&.-]*/g) ?? []; return [...new Set(words.filter((w) => (w.length >= 4 || /^\d{2,}$/.test(w)) && !STOP.has(w)))].slice(0, 12); } export function Highlight({ text, terms }: { text: string; terms: string[] }) { if (!terms.length) return <>{text}; const re = new RegExp(`(${terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`, "gi"); return ( <> {text.split(re).map((part, k) => (k % 2 === 1 ? {part} : {part}))} ); } export function snippet(text: string, terms: string[], width = 260): string { const flat = text.replace(/\s+/g, " ").trim(); const lower = flat.toLowerCase(); const hit = terms.map((t) => lower.indexOf(t)).filter((p) => p >= 0).sort((a, b) => a - b)[0] ?? 0; const start = Math.max(0, hit - 60); return (start > 0 ? "… " : "") + flat.slice(start, start + width) + (start + width < flat.length ? " …" : ""); }