import { type KeyboardEvent, useEffect, useMemo, useRef, useState } from "react"; import { Search } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Dialog, DialogContent, DialogDescription, DialogTitle, } from "@/components/ui/dialog"; import { deriveTitle, visibleSessionPreview } from "@/lib/format"; import { cn } from "@/lib/utils"; import type { ChatSummary } from "@/lib/types"; interface SessionSearchDialogProps { open: boolean; sessions: ChatSummary[]; activeKey: string | null; loading: boolean; titleOverrides?: Record; onOpenChange: (open: boolean) => void; onSelect: (key: string) => void; } const EMPTY_TITLE_OVERRIDES: Record = {}; export function SessionSearchDialog({ open, sessions, activeKey, loading, titleOverrides = EMPTY_TITLE_OVERRIDES, onOpenChange, onSelect, }: SessionSearchDialogProps) { const { t } = useTranslation(); const inputRef = useRef(null); const scrollRef = useRef(null); const [scrollTop, setScrollTop] = useState(0); const itemRefs = useRef>([]); const [query, setQuery] = useState(""); const [highlightedIndex, setHighlightedIndex] = useState(0); const normalizedQuery = query.trim().toLowerCase(); const searchableSessions = useMemo(() => sessions.map((session) => ({ session, text: [titleOverrides[session.key], session.title, visibleSessionPreview(session.preview)] .filter(Boolean).join(" ").toLowerCase(), })), [sessions, titleOverrides]); const sessionResults = useMemo(() => { if (!open) return []; if (!normalizedQuery) return sessions; const terms = normalizedQuery.split(/\s+/).filter(Boolean); return searchableSessions.filter(({ text }) => terms.every((term) => text.includes(term))) .map(({ session }) => session); }, [normalizedQuery, open, sessions, searchableSessions]); const itemCount = sessionResults.length; const rowHeight = 64; const windowStart = Math.max(0, Math.min(itemCount - 1, Math.floor(scrollTop / rowHeight) - 4)); const windowEnd = Math.min(itemCount, windowStart + 24); useEffect(() => { if (!open) return; setQuery(""); setHighlightedIndex(0); window.setTimeout(() => inputRef.current?.focus(), 0); }, [open]); useEffect(() => { setHighlightedIndex(0); setScrollTop(0); if (scrollRef.current) scrollRef.current.scrollTop = 0; }, [normalizedQuery]); useEffect(() => { setHighlightedIndex((index) => itemCount === 0 ? 0 : Math.min(index, itemCount - 1), ); }, [itemCount]); useEffect(() => { itemRefs.current = itemRefs.current.slice(0, itemCount); }, [itemCount]); useEffect(() => { if (!open) return; const scroller = scrollRef.current; if (scroller) { const top = highlightedIndex * rowHeight; if (top < scroller.scrollTop || top + rowHeight > scroller.scrollTop + scroller.clientHeight) { scroller.scrollTop = top; setScrollTop(top); } } itemRefs.current[highlightedIndex]?.scrollIntoView({ block: "nearest", inline: "nearest", }); }, [highlightedIndex, open]); const handleSelect = (key: string) => { onOpenChange(false); onSelect(key); }; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "ArrowDown") { event.preventDefault(); setHighlightedIndex((index) => itemCount === 0 ? 0 : (index + 1) % itemCount, ); return; } if (event.key === "ArrowUp") { event.preventDefault(); setHighlightedIndex((index) => itemCount === 0 ? 0 : (index - 1 + itemCount) % itemCount, ); return; } if (event.key === "Enter") { const highlighted = sessionResults[highlightedIndex]; if (!highlighted) return; event.preventDefault(); handleSelect(highlighted.key); } }; const emptyLabel = normalizedQuery ? t("sidebar.noSearchResults") : t("chat.noSessions"); const sectionLabel = normalizedQuery ? t("sidebar.searchResults") : t("sidebar.recent"); if (!open) return null; return ( {t("sidebar.searchAria")} {t("sidebar.searchPlaceholder")}
setQuery(event.target.value)} onKeyDown={handleKeyDown} placeholder={t("sidebar.searchPlaceholder")} aria-label={t("sidebar.searchAria")} className="h-full min-w-0 flex-1 bg-transparent text-[19px] font-normal leading-none text-foreground outline-none placeholder:text-muted-foreground" />
setScrollTop(event.currentTarget.scrollTop)} data-testid="session-search-scroll" className="min-h-0 flex-1 overflow-y-auto overscroll-contain p-2.5 scrollbar-thin scrollbar-track-transparent" >
{sectionLabel}
{loading && sessions.length === 0 ? (
{t("chat.loading")}
) : sessionResults.length === 0 ? (
{emptyLabel}
) : (
  • {sessionResults.slice(windowStart, windowEnd).map((session, offset) => { const index = windowStart + offset; const title = titleOverrides[session.key]?.trim() || session.title?.trim() || deriveTitle(session.preview, t("chat.newChat")); const preview = visibleSessionPreview(session.preview); const showPreview = preview.length > 0 && preview.toLowerCase() !== title.trim().toLowerCase(); const highlighted = index === highlightedIndex; const active = session.key === activeKey; return (
  • ); })}
)}
); }