"use client" import { ChevronDown, ChevronUp, MessageSquare, Search, Trash2, X, } from "lucide-react" import { useEffect, useState } from "react" import { TemplatePanel } from "@/components/chat/TemplatePanel" import ExamplePanel from "@/components/chat-example-panel" import Image from "@/components/image-with-basepath" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { STORAGE_KEYS } from "@/lib/storage" import type { Template } from "@/lib/template-storage" interface SessionMetadata { id: string title: string updatedAt: number thumbnailDataUrl?: string } interface ChatLobbyProps { sessions: SessionMetadata[] onSelectSession: (id: string) => void onDeleteSession?: (id: string) => void setInput: (input: string) => void setFiles: (files: File[]) => void onSendTemplate?: (template: Template) => void currentInput?: string dict: { sessionHistory?: { recentChats?: string searchPlaceholder?: string noResults?: string justNow?: string deleteTitle?: string deleteDescription?: string } templates?: { title?: string myTemplates?: string } examples?: { quickExamples?: string } common: { delete: string cancel: string } } } // Helper to format session date function formatSessionDate( timestamp: number, dict?: { justNow?: string }, ): string { const date = new Date(timestamp) const now = new Date() const diffMs = now.getTime() - date.getTime() const diffMins = Math.floor(diffMs / (1000 * 60)) const diffHours = Math.floor(diffMs / (1000 * 60 * 60)) if (diffMins < 1) return dict?.justNow || "Just now" if (diffMins < 60) return `${diffMins}m ago` if (diffHours < 24) return `${diffHours}h ago` return date.toLocaleDateString(undefined, { month: "short", day: "numeric", }) } function getPanelVisibility() { if (typeof window === "undefined") return { recentChats: true, myTemplates: true, quickExamples: true } return { recentChats: localStorage.getItem(STORAGE_KEYS.showRecentChats) !== "false", myTemplates: localStorage.getItem(STORAGE_KEYS.showMyTemplates) !== "false", quickExamples: localStorage.getItem(STORAGE_KEYS.showQuickExamples) !== "false", } } export function ChatLobby({ sessions, onSelectSession, onDeleteSession, setInput, setFiles, onSendTemplate, currentInput = "", dict, }: ChatLobbyProps) { const [templatesExpanded, setTemplatesExpanded] = useState(true) const [examplesExpanded, setExamplesExpanded] = useState(true) const [panelVisibility, setPanelVisibility] = useState(getPanelVisibility) const [deleteDialogOpen, setDeleteDialogOpen] = useState(false) const [sessionToDelete, setSessionToDelete] = useState(null) const [searchQuery, setSearchQuery] = useState("") // Listen for panel visibility changes from settings useEffect(() => { const handler = () => setPanelVisibility(getPanelVisibility()) window.addEventListener("panelVisibilityChange", handler) return () => window.removeEventListener("panelVisibilityChange", handler) }, []) const hasHistory = sessions.length > 0 if (!hasHistory) { if (!panelVisibility.myTemplates && !panelVisibility.quickExamples) { return null } return (
{panelVisibility.myTemplates && ( )} {panelVisibility.quickExamples && (
)}
) } // Show history + collapsible examples when there are sessions return (
{/* Recent Chats Section */} {panelVisibility.recentChats && (

{dict.sessionHistory?.recentChats || "Recent Chats"}

{/* Search Bar */}
setSearchQuery(e.target.value)} className="w-full pl-9 pr-3 py-2 text-sm rounded-lg border border-border/60 bg-background focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary/50 transition-all" /> {searchQuery && ( )}
{sessions .filter((session) => session.title .toLowerCase() .includes(searchQuery.toLowerCase()), ) .map((session) => ( // biome-ignore lint/a11y/useSemanticElements: Cannot use button - has nested delete button which causes hydration error
onSelectSession(session.id)} onKeyDown={(e) => { if ( e.key === "Enter" || e.key === " " ) { e.preventDefault() onSelectSession(session.id) } }} > {session.thumbnailDataUrl ? (
) : (
)}
{session.title}
{formatSessionDate( session.updatedAt, dict.sessionHistory, )}
{onDeleteSession && ( )}
))} {sessions.filter((s) => s.title .toLowerCase() .includes(searchQuery.toLowerCase()), ).length === 0 && searchQuery && (

{dict.sessionHistory?.noResults || "No chats found"}

)}
)} {/* Collapsible My Templates Section */} {panelVisibility.myTemplates && (
{templatesExpanded && (
)}
)} {/* Collapsible Quick Examples Section */} {panelVisibility.quickExamples && (
{examplesExpanded && (
)}
)} {/* Delete Confirmation Dialog */} {dict.sessionHistory?.deleteTitle || "Delete this chat?"} {dict.sessionHistory?.deleteDescription || "This will permanently delete this chat session and its diagram. This action cannot be undone."} {dict.common.cancel} { if (sessionToDelete && onDeleteSession) { onDeleteSession(sessionToDelete) } setDeleteDialogOpen(false) setSessionToDelete(null) }} className="border border-red-300 bg-red-50 text-red-700 hover:bg-red-100 hover:border-red-400" > {dict.common.delete}
) }