"use client"; import { History, Plus, Search, SquarePen, Trash2 } from "lucide-react"; import { useRouter, useSearchParams } from "next/navigation"; import { type MouseEvent, useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Sheet, SheetDescription, SheetHeader, SheetPanel, SheetPopup, SheetTitle, } from "@/components/ui/sheet"; import { deleteThread, listThreads, THREADS_CHANGED_EVENT, type ThreadSummary, } from "@/lib/ai-chat-history"; import { cn } from "@/lib/utils"; // The pill (panel toggle + search) that sits top-left of the AI chat, next to // the sidebar. Opens a sheet listing saved chats with a "Start new chat" button // — so chat history is reachable from inside the chat, not just the sidebar. export function ChatHistoryPanel() { const { t } = useTranslation(); const router = useRouter(); const searchParams = useSearchParams(); const activeThread = searchParams.get("thread"); const [open, setOpen] = useState(false); const [threads, setThreads] = useState([]); const [query, setQuery] = useState(""); useEffect(() => { const refresh = () => { listThreads() .then(setThreads) .catch(() => { /* not signed in / no clinic — show nothing */ }); }; refresh(); window.addEventListener(THREADS_CHANGED_EVENT, refresh); return () => window.removeEventListener(THREADS_CHANGED_EVENT, refresh); }, []); const filtered = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return threads; return threads.filter((x) => x.title.toLowerCase().includes(q)); }, [threads, query]); const go = (href: string) => { setOpen(false); router.push(href); }; const remove = async (event: MouseEvent, id: string) => { event.preventDefault(); event.stopPropagation(); setThreads((prev) => prev.filter((x) => x.id !== id)); await deleteThread(id).catch(() => { /* ignore */ }); }; return ( <>
{t("chat.history.title")} {t("chat.history.open")}
setQuery(e.target.value)} placeholder={t("chat.history.search")} value={query} />
{filtered.length === 0 ? (

{t("chat.history.empty")}

) : ( filtered.map((thread) => { const active = activeThread === thread.id; return ( ); }) )}
); }