// The title-bar search: filters the lists on the page and shows matching events and test cases // underneath, from any page. "/" focuses it, arrows move, Enter opens, Esc clears. import { forwardRef, useMemo, useState } from "react"; import { Icon } from "../lib/icons.jsx"; import { useData } from "../lib/data.jsx"; import { go, useUi } from "../lib/ui.jsx"; import { KindTag, Status } from "./common.jsx"; import { tr } from "../lib/i18n.js"; const MAX_EVENTS = 6, MAX_CASES = 8; function useResults(q) { const d = useData(); return useMemo(() => { if (!q) return { events: [], cases: [], more: 0 }; const hit = (s) => s.toLowerCase().includes(q); const events = d.traces.filter((t) => hit(`${t.event_id} ${t.text} ${t.source || ""}`)).map((t) => ({ key: `e:${t.event_id}`, kind: "event", id: t.event_id, text: t.text, tag: t.incidents?.[0]?.kind, status: d.outcome(t.event_id), })); // One row per case per test set, from the set's newest run (the case text is the same in every run). const cases = []; for (const s of d.reports?.suites || []) { const r = [...s.readers].sort((a, b) => (b.updated || "").localeCompare(a.updated || ""))[0]; for (const c of r?.cases || []) if (hit(`${c.id} ${c.text}`)) cases.push({ key: `c:${s.id}:${c.id}`, kind: "case", id: c.id, text: c.text, tag: c.category, set: s.title, href: `#/tests/${s.id}/${r.id}/${encodeURIComponent(c.id)}`, pass: c.verdict === "PASS", }); } return { events: events.slice(0, MAX_EVENTS), cases: cases.slice(0, MAX_CASES), more: Math.max(0, events.length - MAX_EVENTS) + Math.max(0, cases.length - MAX_CASES), }; }, [d, q]); } // Bold the matched part of a line of text. function Mark({ text, q }) { const s = String(text || ""), i = s.toLowerCase().indexOf(q); if (i < 0) return s; const start = Math.max(0, i - 40); return <>{start ? "…" : ""}{s.slice(start, i)}{s.slice(i, i + q.length)}{s.slice(i + q.length)}>; } export const Search = forwardRef(function Search(_, ref) { const { ui, set } = useUi(); const [open, setOpen] = useState(false); const [active, setActive] = useState(0); const q = ui.q.trim().toLowerCase(); const { events, cases, more } = useResults(q); const rows = [...events, ...cases]; const pick = (row) => { if (row.kind === "event") { set({ q: "", sel: row.id, openEvent: row.id }); go("#/events"); } else { set({ q: "" }); go(row.href); } setOpen(false); ref.current?.blur(); }; const onKey = (e) => { if (e.key === "ArrowDown") { e.preventDefault(); setActive((a) => Math.min(rows.length - 1, a + 1)); } if (e.key === "ArrowUp") { e.preventDefault(); setActive((a) => Math.max(0, a - 1)); } if (e.key === "Enter" && rows[active]) { e.preventDefault(); pick(rows[active]); } }; const row = (r, i) => ( ); return (