// Global search (fly_trader/ui/api/search.py): a token by symbol or address, a Kalshi market by ticker or title, a // transaction by signature or hash. ↑↓ to move, Enter to open (the inspector, or the page it lives on), Esc to close; // "/" anywhere focuses it. import { useEffect, useRef, useState } from 'react' import { useNavigate } from 'react-router' import { useApp, type InspectTarget } from '../context' import { readTopic } from '../lib/api' import { Icon } from './ui' type Target = InspectTarget | { kind: 'route'; to: string } | null type Result = { kind: 'token' | 'market' | 'tx' | 'note'; label: string; sub: string; target: Target } type Answer = { q: string; results: Result[]; note: string | null } const KIND_ICON: Record = { token: 'bx-coin', market: 'bx-target-lock', tx: 'bx-receipt', note: 'bx-info-circle' } const KIND_LOOK: Record = { token: ['rgb(255 47 185 / 24%)', '#ff8fd8'], market: ['rgb(13 202 240 / 18%)', '#7ad8f5'], tx: ['rgb(139 92 246 / 26%)', '#c4b1ff'], note: ['rgb(255 255 255 / 12%)', '#fff'], } const DEBOUNCE_MS = 300 export function SearchBox() { const { openInspector } = useApp() const navigate = useNavigate() const [text, setText] = useState('') const [open, setOpen] = useState(false) const [ans, setAns] = useState(null) const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const [active, setActive] = useState(0) const input = useRef(null) const box = useRef(null) const seq = useRef(0) const q = text.trim() useEffect(() => { if (q.length < 2) { setAns(null); setBusy(false); setError(null); return } const my = ++seq.current setBusy(true) const h = setTimeout(() => { readTopic('search.query', { q }) .then((a) => { if (my === seq.current) { setAns(a); setError(null); setActive(0) } }) .catch((e: Error) => { if (my === seq.current) setError(e.message) }) .finally(() => { if (my === seq.current) setBusy(false) }) }, DEBOUNCE_MS) return () => clearTimeout(h) }, [q]) useEffect(() => { const key = (e: KeyboardEvent) => { const el = e.target as HTMLElement | null if (e.key === '/' && !(el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT' || el.isContentEditable))) { e.preventDefault(); input.current?.focus() } } const click = (e: MouseEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false) } window.addEventListener('keydown', key) document.addEventListener('mousedown', click) return () => { window.removeEventListener('keydown', key); document.removeEventListener('mousedown', click) } }, []) const results = (ans && ans.q === q ? ans.results : []).filter((r) => r.target) const notes = ans && ans.q === q ? ans.results.filter((r) => !r.target) : [] const choose = (r: Result | undefined) => { if (!r || !r.target) return if (r.target.kind === 'route') navigate(r.target.to) else openInspector(r.target) setOpen(false); setText(''); input.current?.blur() } const onKey = (e: React.KeyboardEvent) => { if (e.key === 'ArrowDown') { e.preventDefault(); setOpen(true); setActive((a) => Math.min(results.length - 1, a + 1)) } else if (e.key === 'ArrowUp') { e.preventDefault(); setActive((a) => Math.max(0, a - 1)) } else if (e.key === 'Enter') { e.preventDefault(); choose(results[active] ?? results[0]) } else if (e.key === 'Escape') { setOpen(false); input.current?.blur() } } const status = q.length === 0 ? null : q.length < 2 ? 'Type at least 2 characters.' : error ? `Search failed: ${error}` : busy && !results.length ? 'Searching…' : ans && ans.q === q && !results.length ? ans.note || 'Nothing matches.' : null const kindName = (k: string) => (k === 'token' ? 'Token' : k === 'market' ? 'Kalshi market' : k === 'tx' ? 'Transaction' : 'Note') return (
0} aria-controls="search-results" aria-autocomplete="list" onChange={(e) => { setText(e.target.value); setOpen(true) }} onFocus={() => setOpen(true)} onKeyDown={onKey} /> {open && q.length > 0 && (

Search

{results.length > 0 &&

{results.length} result{results.length === 1 ? '' : 's'}

} {busy && }
{status &&
{status}
} {results.map((r, i) => { const [bg, color] = KIND_LOOK[r.kind] ?? KIND_LOOK.note return (
setActive(i)} onMouseDown={(e) => { e.preventDefault(); choose(r) }}>
{r.label} {kindName(r.kind)} · {r.target?.kind === 'route' ? `→ ${r.target.to}` : 'inspect'}

{r.sub}

) })} {notes.map((r, i) =>
{r.label}: {r.sub}
)}
↑ ↓ to move · Enter to open · Esc to close
)}
) }