|
| 1 | +import { useEffect, useMemo, useRef, useState } from "react"; |
| 2 | +import { listSources, rebuildIndex, search, type SearchResult, type SourceSummary } from "@/lib/api"; |
| 3 | + |
| 4 | +interface Props { |
| 5 | + /** Compact = floating palette (no header, no rebuild). */ |
| 6 | + compact?: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +export function SearchUI({ compact = false }: Props) { |
| 10 | + const [q, setQ] = useState(""); |
| 11 | + const [source, setSource] = useState<string>(""); |
| 12 | + const [sources, setSources] = useState<SourceSummary[]>([]); |
| 13 | + const [results, setResults] = useState<SearchResult[]>([]); |
| 14 | + const [loading, setLoading] = useState(false); |
| 15 | + const [reindexing, setReindexing] = useState(false); |
| 16 | + const [reindexMsg, setReindexMsg] = useState<string | null>(null); |
| 17 | + const inputRef = useRef<HTMLInputElement>(null); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + listSources().then(setSources).catch(() => {}); |
| 21 | + inputRef.current?.focus(); |
| 22 | + }, []); |
| 23 | + |
| 24 | + // Debounced search. |
| 25 | + useEffect(() => { |
| 26 | + if (!q.trim()) { |
| 27 | + setResults([]); |
| 28 | + return; |
| 29 | + } |
| 30 | + const handle = setTimeout(async () => { |
| 31 | + setLoading(true); |
| 32 | + try { |
| 33 | + const r = await search({ |
| 34 | + q, |
| 35 | + source: source || undefined, |
| 36 | + limit: compact ? 10 : 30, |
| 37 | + }); |
| 38 | + setResults(r); |
| 39 | + } catch (e) { |
| 40 | + console.error(e); |
| 41 | + } finally { |
| 42 | + setLoading(false); |
| 43 | + } |
| 44 | + }, 150); |
| 45 | + return () => clearTimeout(handle); |
| 46 | + }, [q, source, compact]); |
| 47 | + |
| 48 | + const totalDocs = useMemo( |
| 49 | + () => sources.reduce((s, x) => s + x.count, 0), |
| 50 | + [sources], |
| 51 | + ); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className={compact ? "flex h-screen flex-col" : "min-h-screen"}> |
| 55 | + {!compact && ( |
| 56 | + <header className="border-b border-border bg-card px-6 py-4"> |
| 57 | + <div className="mx-auto flex max-w-5xl items-baseline gap-4"> |
| 58 | + <h1 className="font-serif text-xl text-foreground">Lovcode</h1> |
| 59 | + <span className="text-xs text-muted-foreground"> |
| 60 | + {totalDocs.toLocaleString()} conversations indexed |
| 61 | + </span> |
| 62 | + <div className="ml-auto flex items-center gap-2"> |
| 63 | + {reindexMsg && ( |
| 64 | + <span className="text-xs text-muted-foreground">{reindexMsg}</span> |
| 65 | + )} |
| 66 | + <button |
| 67 | + type="button" |
| 68 | + disabled={reindexing} |
| 69 | + onClick={async () => { |
| 70 | + setReindexing(true); |
| 71 | + setReindexMsg(null); |
| 72 | + try { |
| 73 | + const n = await rebuildIndex(); |
| 74 | + setReindexMsg(`Indexed ${n.toLocaleString()}`); |
| 75 | + const fresh = await listSources(); |
| 76 | + setSources(fresh); |
| 77 | + } catch (e) { |
| 78 | + setReindexMsg(`Error: ${e}`); |
| 79 | + } finally { |
| 80 | + setReindexing(false); |
| 81 | + } |
| 82 | + }} |
| 83 | + className="rounded-lg border border-border bg-card px-3 py-1.5 text-xs text-foreground hover:bg-muted disabled:opacity-50" |
| 84 | + > |
| 85 | + {reindexing ? "Indexing…" : "Rebuild index"} |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </header> |
| 90 | + )} |
| 91 | + |
| 92 | + <div className={compact ? "flex-1 overflow-hidden p-4" : "mx-auto max-w-5xl px-6 py-6"}> |
| 93 | + <div className="flex items-center gap-2"> |
| 94 | + <input |
| 95 | + ref={inputRef} |
| 96 | + type="text" |
| 97 | + value={q} |
| 98 | + onChange={(e) => setQ(e.target.value)} |
| 99 | + placeholder="Search every conversation…" |
| 100 | + className="w-full rounded-xl border border-border bg-card px-4 py-3 text-base text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-primary/40" |
| 101 | + /> |
| 102 | + {!compact && ( |
| 103 | + <select |
| 104 | + value={source} |
| 105 | + onChange={(e) => setSource(e.target.value)} |
| 106 | + className="rounded-xl border border-border bg-card px-3 py-3 text-sm text-foreground focus:outline-none" |
| 107 | + > |
| 108 | + <option value="">All sources</option> |
| 109 | + {sources.map((s) => ( |
| 110 | + <option key={s.id} value={s.id}> |
| 111 | + {s.name} ({s.count}) |
| 112 | + </option> |
| 113 | + ))} |
| 114 | + </select> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + |
| 118 | + <div className={compact ? "mt-3 flex-1 overflow-y-auto" : "mt-6"}> |
| 119 | + {loading && results.length === 0 && ( |
| 120 | + <p className="text-sm text-muted-foreground">Searching…</p> |
| 121 | + )} |
| 122 | + {!loading && q && results.length === 0 && ( |
| 123 | + <p className="text-sm text-muted-foreground">No results.</p> |
| 124 | + )} |
| 125 | + <ul className="space-y-3"> |
| 126 | + {results.map((r) => ( |
| 127 | + <ResultCard key={r.conversation_id + r.source} r={r} compact={compact} /> |
| 128 | + ))} |
| 129 | + </ul> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function ResultCard({ r, compact }: { r: SearchResult; compact: boolean }) { |
| 137 | + const date = r.timestamp ? new Date(r.timestamp).toISOString().slice(0, 10) : "—"; |
| 138 | + return ( |
| 139 | + <li className={`rounded-xl border border-border bg-card ${compact ? "p-3" : "p-4"}`}> |
| 140 | + <div className="flex items-baseline gap-3 text-xs text-muted-foreground"> |
| 141 | + <span className="rounded bg-muted px-1.5 py-0.5 font-mono">{r.source}</span> |
| 142 | + <span>{date}</span> |
| 143 | + {r.project && <span className="truncate">{r.project}</span>} |
| 144 | + <span className="ml-auto font-mono">{r.score.toFixed(1)}</span> |
| 145 | + </div> |
| 146 | + {r.title && ( |
| 147 | + <div className={`mt-1 font-medium text-foreground ${compact ? "line-clamp-1" : "line-clamp-2"}`}> |
| 148 | + {r.title} |
| 149 | + </div> |
| 150 | + )} |
| 151 | + <div className={`mt-1 text-sm text-muted-foreground ${compact ? "line-clamp-2" : "line-clamp-3"}`}> |
| 152 | + {r.snippet} |
| 153 | + </div> |
| 154 | + </li> |
| 155 | + ); |
| 156 | +} |
0 commit comments