"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useRouter } from "next/navigation"; import { History, Loader2, RefreshCw, Search, type LucideIcon, } from "lucide-react"; import { useTranslation } from "react-i18next"; import SessionList from "@/components/SessionList"; import SpaceSectionHeader from "@/components/space/SpaceSectionHeader"; import { useAppShell } from "@/context/AppShellContext"; import { deleteSession, listSessions, updateSessionTitle, type SessionSummary, } from "@/lib/session-api"; /** * Sessions list for chat history. Reopened sessions always route back to * the main chat surface. */ export interface ChatHistorySectionProps { icon?: LucideIcon; title?: string; description?: string; } export default function ChatHistorySection({ icon, title, description, }: ChatHistorySectionProps = {}) { const basePath = "/home"; const { t } = useTranslation(); const router = useRouter(); const { activeSessionId, setActiveSessionId } = useAppShell(); const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const [query, setQuery] = useState(""); const load = useCallback(async (force = false) => { setLoading(true); try { setSessions(await listSessions(200, 0, { force })); } finally { setLoading(false); } }, []); useEffect(() => { void load(true); }, [load]); const filteredSessions = useMemo(() => { const needle = query.trim().toLowerCase(); if (!needle) return sessions; return sessions.filter((session) => [session.title, session.last_message] .filter(Boolean) .some((value) => value.toLowerCase().includes(needle)), ); }, [query, sessions]); const handleSelect = useCallback( (sessionId: string) => { setActiveSessionId(sessionId); router.push(`${basePath}/${sessionId}`); }, [basePath, router, setActiveSessionId], ); const handleRename = useCallback( async (sessionId: string, title: string) => { await updateSessionTitle(sessionId, title); await load(true); }, [load], ); const handleDelete = useCallback( async (sessionId: string) => { if (!window.confirm(t("Delete this chat?"))) return; await deleteSession(sessionId); if (activeSessionId === sessionId) setActiveSessionId(null); setSessions((prev) => prev.filter((session) => session.session_id !== sessionId), ); }, [activeSessionId, setActiveSessionId, t], ); const HeaderIcon = icon ?? History; const headerTitle = title ?? t("Chat History"); const headerDescription = description ?? t( "Browse, rename, delete, and reopen previous conversations from your learning space.", ); return (
{sessions.length} {t("conversations")} } action={ } />
); }