"use client"; import { Check, Pencil, Trash2 } from "lucide-react"; import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { type SessionSummary } from "@/lib/session-api"; import { normalizeMessageContent, truncateText } from "@/lib/message-content"; type SessionRuntimeStatus = | "idle" | "running" | "completed" | "failed" | "cancelled" | "rejected"; interface SessionListProps { sessions: SessionSummary[]; activeSessionId: string | null; loading?: boolean; compact?: boolean; onSelect: (sessionId: string) => void | Promise; onRename: (sessionId: string, title: string) => void | Promise; onDelete: (sessionId: string) => void | Promise; } function statusColor(status?: SessionRuntimeStatus): string { switch (status) { case "running": return "bg-blue-500"; case "completed": return "bg-emerald-400"; case "failed": return "bg-rose-500"; case "rejected": return "bg-fuchsia-500"; case "cancelled": return "bg-amber-500"; default: return "bg-[var(--muted-foreground)]/25"; } } function StatusIndicator({ status }: { status?: SessionRuntimeStatus }) { if (!status || status === "idle") return null; if (status === "running") { return ( ); } if (status === "completed") { return ( ); } if (status === "failed") { return ( ); } if (status === "rejected") { return ( ); } if (status === "cancelled") { return ( ); } return null; } function groupLabel(timestamp: number): string { const now = new Date(); const date = new Date(timestamp * 1000); const startOfToday = new Date( now.getFullYear(), now.getMonth(), now.getDate(), ).getTime(); const startOfItemDay = new Date( date.getFullYear(), date.getMonth(), date.getDate(), ).getTime(); const diffDays = Math.floor((startOfToday - startOfItemDay) / 86400000); if (diffDays <= 0) return "Today"; if (diffDays === 1) return "Yesterday"; if (diffDays < 7) return "Last 7 days"; return "Earlier"; } function relativeTime(timestamp: number): string { const diffSeconds = Math.round(timestamp - Date.now() / 1000); const formatter = new Intl.RelativeTimeFormat("en", { numeric: "auto" }); const abs = Math.abs(diffSeconds); if (abs < 60) return formatter.format(diffSeconds, "second"); if (abs < 3600) return formatter.format(Math.round(diffSeconds / 60), "minute"); if (abs < 86400) return formatter.format(Math.round(diffSeconds / 3600), "hour"); return formatter.format(Math.round(diffSeconds / 86400), "day"); } export default function SessionList({ sessions, activeSessionId, loading = false, compact = false, onSelect, onRename, onDelete, }: SessionListProps) { const { t } = useTranslation(); const [editingId, setEditingId] = useState(null); const [draftTitle, setDraftTitle] = useState(""); const grouped = useMemo(() => { const buckets = new Map(); for (const session of sessions) { const label = groupLabel(session.updated_at); const current = buckets.get(label) ?? []; current.push(session); buckets.set(label, current); } return Array.from(buckets.entries()); }, [sessions]); const startEdit = (session: SessionSummary) => { setEditingId(session.session_id); setDraftTitle(session.title); }; const commitEdit = async () => { if (!editingId) return; const nextTitle = draftTitle.trim(); if (!nextTitle) { setEditingId(null); setDraftTitle(""); return; } await onRename(editingId, nextTitle); setEditingId(null); setDraftTitle(""); }; if (loading) { if (compact) { return (
{[1, 2, 3].map((i) => (
))}
); } return (
{[1, 2, 3].map((i) => (
))}
); } if (sessions.length === 0) { if (compact) return null; return (
{t("No conversations yet")}
); } /* ---- Compact tree-line style (under Chat nav item) ---- */ if (compact) { return (
{grouped.map(([label, items], groupIdx) => (
{groupIdx > 0 && (
)}
{label}
{items.map((session) => { const active = activeSessionId === session.session_id; const isEditing = editingId === session.session_id; return (
void onSelect(session.session_id)} onKeyDown={(event) => { if (event.key === "Enter" || event.key === " ") { event.preventDefault(); void onSelect(session.session_id); } }} role="button" tabIndex={0} className={`group flex items-center gap-2 rounded-r-lg py-1 pl-3 pr-2 transition-colors ${ active ? "bg-[var(--background)]/50 text-[var(--foreground)]" : "text-[var(--muted-foreground)] hover:bg-[var(--background)]/40 hover:text-[var(--foreground)]" }`} > {isEditing ? ( setDraftTitle(event.target.value)} onBlur={() => void commitEdit()} onKeyDown={(event) => { if (event.key === "Enter") void commitEdit(); if (event.key === "Escape") { setEditingId(null); setDraftTitle(""); } }} onClick={(event) => event.stopPropagation()} className="min-w-0 flex-1 rounded border border-[var(--border)] bg-[var(--background)] px-1.5 py-px text-[12px] text-[var(--foreground)] outline-none focus:ring-1 focus:ring-[var(--primary)]/40" /> ) : ( {session.title || "Untitled chat"} )}
{isEditing ? ( ) : ( )}
); })}
))}
); } /* ---- Classic style ---- */ return (
{grouped.map(([label, items]) => (
{label}
{items.map((session) => { const active = activeSessionId === session.session_id; const isEditing = editingId === session.session_id; return (
void onSelect(session.session_id)} onKeyDown={(event) => { if (event.key === "Enter" || event.key === " ") { event.preventDefault(); void onSelect(session.session_id); } }} role="button" tabIndex={0} className={`group relative w-full px-3 py-2.5 text-left transition-colors duration-150 ${ active ? "bg-[var(--background)]/70 text-[var(--foreground)]" : "text-[var(--muted-foreground)] hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]" }`} > {active && ( )}
{isEditing ? ( setDraftTitle(event.target.value) } onBlur={() => void commitEdit()} onKeyDown={(event) => { if (event.key === "Enter") void commitEdit(); if (event.key === "Escape") { setEditingId(null); setDraftTitle(""); } }} onClick={(event) => event.stopPropagation()} className="w-full rounded border border-[var(--border)] bg-[var(--background)] px-2 py-0.5 text-[12px] text-[var(--foreground)] outline-none focus:ring-1 focus:ring-[var(--primary)]/40" /> ) : (
{session.title || "Untitled chat"}
)} {!isEditing && (
{truncateText( normalizeMessageContent(session.last_message), 120, ) || relativeTime(session.updated_at)}
)}
{isEditing ? ( ) : ( )}
); })}
))}
); }