import { useState, useEffect } from "react"; import { Plus, Trash2, ChevronLeft, ChevronRight } from "lucide-react"; /** * ChatSidebar Component * * Features: * - Display chat history from local storage * - Create new chat * - Delete chat history * - Switch between chats * - Collapsible sidebar * - Glassmorphism UI matching theme */ export interface ChatSession { id: string; title: string; timestamp: number; mode: "ask" | "imagine"; messageCount: number; } interface ChatSidebarProps { currentChatId: string | null; onNewChat: () => void; onSelectChat: (chatId: string) => void; onDeleteChat: (chatId: string) => void; isOpen: boolean; onToggle: () => void; } export default function ChatSidebar({ currentChatId, onNewChat, onSelectChat, onDeleteChat, isOpen, onToggle, }: ChatSidebarProps) { const [chats, setChats] = useState([]); // Load chats from local storage useEffect(() => { const savedChats = localStorage.getItem("domify_chats"); if (savedChats) { try { const parsed = JSON.parse(savedChats); setChats(parsed.sort((a: ChatSession, b: ChatSession) => b.timestamp - a.timestamp)); } catch (error) { console.error("Error loading chats:", error); } } }, []); // Handle delete chat const handleDelete = (chatId: string, e: React.MouseEvent) => { e.stopPropagation(); // Remove from local storage const savedChats = localStorage.getItem("domify_chats"); if (savedChats) { const parsed = JSON.parse(savedChats); const filtered = parsed.filter((c: ChatSession) => c.id !== chatId); localStorage.setItem("domify_chats", JSON.stringify(filtered)); setChats(filtered); } // Also remove the chat messages localStorage.removeItem(`domify_chat_${chatId}`); onDeleteChat(chatId); }; // Format date const formatDate = (timestamp: number) => { const date = new Date(timestamp); const today = new Date(); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); if (date.toDateString() === today.toDateString()) { return date.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit" }); } else if (date.toDateString() === yesterday.toDateString()) { return "Yesterday"; } else { return date.toLocaleDateString("en-US", { month: "short", day: "numeric" }); } }; return ( <> {/* Sidebar */}
{/* Header */}

Chats

{/* New Chat Button */}
{/* Chat List */}
{chats.length === 0 ? (

No chats yet

Start a new conversation

) : ( chats.map((chat) => (
)) )}
{/* Footer */}

Chats stored locally

{/* Toggle Button (when sidebar is closed) */} {!isOpen && ( )} {/* Overlay (when sidebar is open on mobile) */} {isOpen && (
)} ); }