// src/components/ChatSearchResults.tsx // Copyright (C) 2026 Robert Colbert // All Rights Reserved import { useState, useEffect, useCallback } from "react"; import { Search, X, Loader2 } from "lucide-react"; import { searchApi, ISearchResult } from "../lib/api"; interface ChatSearchResultsProps { query: string; filters?: { projectId?: string; sessionId?: string; turnId?: string }; onSelect: (result: ISearchResult) => void; onClose: () => void; } export default function ChatSearchResults({ query, filters, onSelect, onClose, }: ChatSearchResultsProps) { const [editQuery, setEditQuery] = useState(query); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const performSearch = useCallback(async () => { if (!editQuery.trim()) return; setLoading(true); setError(null); try { const data = await searchApi.search(editQuery, filters, 20); setResults(data); } catch (err) { setError((err as Error).message || "Search failed"); } finally { setLoading(false); } }, [editQuery, filters]); useEffect(() => { performSearch(); }, [performSearch]); const handleSearchKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); performSearch(); } }; const formatScore = (score: number): string => `${Math.round(score * 100)}%`; const formatTimestamp = (iso: string): string => { if (!iso) return ""; try { return new Date(iso).toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "2-digit" }); } catch { return iso; } }; return (
{ if (e.target === e.currentTarget) onClose(); }} onKeyDown={(e) => { if (e.key === "Escape") onClose(); }} >
{/* Header with editable search input */}
setEditQuery(e.target.value)} onKeyDown={handleSearchKeyDown} className="flex-1 bg-transparent text-text-primary text-sm focus:outline-none placeholder:text-text-tertiary" placeholder="Edit search query…" autoFocus />
{/* Result count bar */} {!loading && (
{results.length} result{results.length !== 1 ? "s" : ""} for "{query === editQuery ? query : editQuery}"
)}
{loading && (
Searching…
)} {error &&

{error}

} {!loading && !error && results.length === 0 &&

No results found

} {!loading && !error && results.length > 0 && (
{results.map((result) => ( ))}
)}
); }