({
initialQuery,
onSelect,
onCancel
}: Props)
| 25 | age: string; |
| 26 | }; |
| 27 | export function HistorySearchDialog({ |
| 28 | initialQuery, |
| 29 | onSelect, |
| 30 | onCancel |
| 31 | }: Props): React.ReactNode { |
| 32 | useRegisterOverlay('history-search'); |
| 33 | const { |
| 34 | columns |
| 35 | } = useTerminalSize(); |
| 36 | const [items, setItems] = useState<Item[] | null>(null); |
| 37 | const [query, setQuery] = useState(initialQuery ?? ''); |
| 38 | useEffect(() => { |
| 39 | let cancelled = false; |
| 40 | void (async () => { |
| 41 | const reader = getTimestampedHistory(); |
| 42 | const loaded: Item[] = []; |
| 43 | for await (const entry of reader) { |
| 44 | if (cancelled) { |
| 45 | void reader.return(undefined); |
| 46 | return; |
| 47 | } |
| 48 | const display = entry.display; |
| 49 | const nl = display.indexOf('\n'); |
| 50 | const age = formatRelativeTimeAgo(new Date(entry.timestamp)); |
| 51 | loaded.push({ |
| 52 | entry, |
| 53 | display, |
| 54 | lower: display.toLowerCase(), |
| 55 | firstLine: nl === -1 ? display : display.slice(0, nl), |
| 56 | age: age + ' '.repeat(Math.max(0, AGE_WIDTH - stringWidth(age))) |
| 57 | }); |
| 58 | } |
| 59 | if (!cancelled) setItems(loaded); |
| 60 | })(); |
| 61 | return () => { |
| 62 | cancelled = true; |
| 63 | }; |
| 64 | }, []); |
| 65 | const filtered = useMemo(() => { |
| 66 | if (!items) return []; |
| 67 | const q = query.trim().toLowerCase(); |
| 68 | if (!q) return items; |
| 69 | const exact: Item[] = []; |
| 70 | const fuzzy: Item[] = []; |
| 71 | for (const item of items) { |
| 72 | if (item.lower.includes(q)) { |
| 73 | exact.push(item); |
| 74 | } else if (isSubsequence(item.lower, q)) { |
| 75 | fuzzy.push(item); |
| 76 | } |
| 77 | } |
| 78 | return exact.concat(fuzzy); |
| 79 | }, [items, query]); |
| 80 | const previewOnRight = columns >= 100; |
| 81 | const listWidth = previewOnRight ? Math.floor((columns - 6) * 0.5) : columns - 6; |
| 82 | const rowWidth = Math.max(20, listWidth - AGE_WIDTH - 1); |
| 83 | const previewWidth = previewOnRight ? Math.max(20, columns - listWidth - 12) : Math.max(20, columns - 10); |
| 84 | return <FuzzyPicker title="Search prompts" placeholder="Filter history…" initialQuery={initialQuery} items={filtered} getKey={item_0 => String(item_0.entry.timestamp)} onQueryChange={setQuery} onSelect={item_1 => { |
nothing calls this directly
no test coverage detected