import { useNavigate, useLoaderData } from '@remix-run/react'; import { useEffect, useState } from 'react'; import { IconButton } from '~/components/ui/IconButton'; import { getAll } from '~/lib/persistence/db'; import type { ChatHistoryItem } from '~/lib/persistence/useChatHistory'; import { db } from '~/lib/persistence/useChatHistory'; import { classNames } from '~/utils/classNames'; import { createScopedLogger } from '~/utils/logger'; const logger = createScopedLogger('ChatHistory'); export function ChatHistory() { const [history, setHistory] = useState([]); const [isOpen, setIsOpen] = useState(false); const navigate = useNavigate(); const { id: currentId } = useLoaderData<{ id?: string }>(); useEffect(() => { if (!db) { return; } const loadHistory = async () => { try { const items = await getAll(db!); // filter items to only show history for the current chat const filteredItems = items .filter((item) => item.urlId === currentId || item.id === currentId) .sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()); setHistory(filteredItems); } catch (error) { logger.error('Failed to load chat history', error); } }; loadHistory(); }, [currentId]); const handleRestore = (item: ChatHistoryItem) => { navigate(`/chat/${item.id}`); setIsOpen(false); }; if (!db || history.length === 0) { return null; } return (
setIsOpen(!isOpen)} className={classNames({ 'text-bolt-elements-item-contentAccent!': isOpen, })} >
{isOpen && (

Chat History

{history.map((item) => ( ))}
)}
); }