import { motion, type Variants } from 'framer-motion'; import { useCallback, useEffect, useRef, useState } from 'react'; import { toast } from 'react-toastify'; import { Dialog, DialogButton, DialogDescription, DialogRoot, DialogTitle } from '~/components/ui/Dialog'; import { ThemeSwitch } from '~/components/ui/ThemeSwitch'; import { SettingsWindow } from '~/components/settings/SettingsWindow'; import { SettingsButton } from '~/components/ui/SettingsButton'; import { deleteById, getAllChats, currentChatId } from '~/lib/persistence'; import type { ChatContents } from '~/lib/persistence/db'; import { cubicEasingFn } from '~/utils/easings'; import { logger } from '~/utils/logger'; import { HistoryItem } from './HistoryItem'; import { binDates } from './date-binning'; import { useSearchFilter } from '~/lib/hooks/useSearchFilter'; import { SaveProblem } from './SaveProblem'; import { useAdminStatus } from '~/lib/stores/user'; 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: ChatContents } | null; export const Menu = () => { const menuRef = useRef(null); const [list, setList] = useState([]); const [open, setOpen] = useState(false); const [dialogContent, setDialogContent] = useState(null); const [isSettingsOpen, setIsSettingsOpen] = useState(false); const { isAdmin } = useAdminStatus(); const { filteredItems: filteredList, handleSearchChange } = useSearchFilter({ items: list, searchFields: ['title'], }); const loadEntries = useCallback(() => { getAllChats() .then(setList) .catch((error) => toast.error(error.message)); }, []); const deleteItem = useCallback((event: React.UIEvent, item: ChatContents) => { event.preventDefault(); deleteById(item.id) .then(() => { loadEntries(); if (currentChatId.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 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); }; }, []); const handleDeleteClick = (event: React.UIEvent, item: ChatContents) => { event.preventDefault(); setDialogContent({ type: 'delete', item }); }; return (
{/* Spacer for top margin */}
Problems {isAdmin && } About
Your Chats
{filteredList.length === 0 && (
{list.length === 0 ? 'No previous conversations' : 'No matches found'}
)} {binDates(filteredList).map(({ category, items }) => (
{category}
{items.map((item) => ( handleDeleteClick(event, item)} /> ))}
))} {dialogContent?.type === 'delete' && ( <> Delete Chat?

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

Are you sure you want to delete this chat?

Cancel { deleteItem(event, dialogContent.item); closeDialog(); }} > Delete
)}
setIsSettingsOpen(true)} />
setIsSettingsOpen(false)} /> ); };