import { saveAs } from 'file-saver'; import { motion, type Variants } from 'framer-motion'; import { useCallback, useEffect, useRef, useState } from 'react'; import { toast } from 'react-toastify'; import { HistoryItem } from './HistoryItem'; import { binDates } from './date-binning'; import { Dialog, DialogButton, DialogDescription, DialogRoot, DialogTitle } from '~/components/ui/Dialog'; import { ThemeSwitch } from '~/components/ui/ThemeSwitch'; import { db, deleteById, getAll, chatId, type ChatHistoryItem, setMessages } from '~/lib/persistence'; import { cubicEasingFn } from '~/utils/easings'; import { logger } from '~/utils/logger'; const menuVariants = { closed: { opacity: 0, visibility: 'hidden', left: '-150px', transition: { duration: 0.2, ease: cubicEasingFn, }, }, open: { opacity: 1, visibility: 'initial', left: 0, transition: { duration: 0.2, ease: cubicEasingFn, }, }, } satisfies Variants; type DialogContent = { type: 'delete'; item: ChatHistoryItem } | null; export function Menu() { const menuRef = useRef(null); const [list, setList] = useState([]); const [open, setOpen] = useState(false); const [dialogContent, setDialogContent] = useState(null); const loadEntries = useCallback(() => { if (db) { getAll(db) .then((list) => list.filter((item) => item.urlId && item.description)) .then(setList) .catch((error) => toast.error(error.message)); } }, []); const deleteItem = useCallback((event: React.UIEvent, item: ChatHistoryItem) => { event.preventDefault(); if (db) { deleteById(db, item.id) .then(() => { loadEntries(); if (chatId.get() === item.id) { // hard page navigation to clear the stores window.location.pathname = '/'; } }) .catch((error) => { toast.error('Failed to delete conversation'); logger.error(error); }); } }, []); const renameItem = useCallback( (id: string, newDescription: string) => { if (db) { const item = list.find((item) => item.id === id); if (item) { setMessages(db, id, item.messages, item.urlId, newDescription) .then(() => { loadEntries(); toast.success('Chat renamed successfully'); }) .catch((error) => { toast.error('Failed to rename chat'); logger.error(error); }); } } }, [list], ); const exportItem = useCallback((item: ChatHistoryItem) => { try { const chatData = { description: item.description, messages: item.messages, timestamp: item.timestamp, }; const blob = new Blob([JSON.stringify(chatData, null, 2)], { type: 'application/json' }); const filename = `${item.description || 'chat'}-${new Date(item.timestamp).toISOString().split('T')[0]}.json`; saveAs(blob, filename); toast.success('Chat exported successfully'); } catch (error) { toast.error('Failed to export chat'); logger.error(error); } }, []); const closeDialog = () => { setDialogContent(null); }; useEffect(() => { if (open) { loadEntries(); } }, [open]); useEffect(() => { const enterThreshold = 40; const exitThreshold = 40; function onMouseMove(event: MouseEvent) { if (event.pageX < enterThreshold) { setOpen(true); } if (menuRef.current && event.clientX > menuRef.current.getBoundingClientRect().right + exitThreshold) { setOpen(false); } } window.addEventListener('mousemove', onMouseMove); return () => { window.removeEventListener('mousemove', onMouseMove); }; }, []); return (
{/* Placeholder */}
Your Chats
{list.length === 0 &&
No previous conversations
} {binDates(list).map(({ category, items }) => (
{category}
{items.map((item) => ( setDialogContent({ type: 'delete', item })} onRename={renameItem} onExport={exportItem} /> ))}
))} {dialogContent?.type === 'delete' && ( <> Delete Chat?

You are about to delete {dialogContent.item.description}.

Are you sure you want to delete this chat?

Cancel { deleteItem(event, dialogContent.item); closeDialog(); }} > Delete
)}
); }