import { useCallback, useEffect, useRef, useState } from 'react'; import { toast } from 'react-toastify'; import { db, deleteId, type ChatHistory } from '~/lib/persistence'; import { logger } from '~/utils/logger'; export function HistoryItem({ item, loadEntries }: { item: ChatHistory; loadEntries: () => void }) { const [requestingDelete, setRequestingDelete] = useState(false); const [hovering, setHovering] = useState(false); const hoverRef = useRef(null); const deleteItem = useCallback((event: React.UIEvent) => { event.preventDefault(); if (db) { deleteId(db, item.id) .then(() => loadEntries()) .catch((error) => { toast.error('Failed to delete conversation'); logger.error(error); }); } }, []); useEffect(() => { let timeout: NodeJS.Timeout | undefined; function mouseEnter() { setHovering(true); if (timeout) { clearTimeout(timeout); } } function mouseLeave() { setHovering(false); // wait for animation to finish before unsetting timeout = setTimeout(() => { setRequestingDelete(false); }, 200); } hoverRef.current?.addEventListener('mouseenter', mouseEnter); hoverRef.current?.addEventListener('mouseleave', mouseLeave); return () => { hoverRef.current?.removeEventListener('mouseenter', mouseEnter); hoverRef.current?.removeEventListener('mouseleave', mouseLeave); }; }, []); return (
{item.description}
{hovering && (
{requestingDelete ? (
)}
); }