'use client'; import clsx from 'clsx'; import dayjs from 'dayjs'; import React, { useEffect, useState, useCallback } from 'react'; import { LuMessageSquare, LuTrash2, LuPencil, LuCheck, LuX, LuPlus } from 'react-icons/lu'; import { useTranslation } from '@/hooks/useTranslation'; import { useBookDataStore } from '@/store/bookDataStore'; import { useAIChatStore } from '@/store/aiChatStore'; import { useNotebookStore } from '@/store/notebookStore'; import type { AIConversation } from '@/services/ai/types'; import { useEnv } from '@/context/EnvContext'; interface ChatHistoryViewProps { bookKey: string; } const ChatHistoryView: React.FC = ({ bookKey }) => { const _ = useTranslation(); const { appService } = useEnv(); const { getBookData } = useBookDataStore(); const { conversations, isLoadingHistory, loadConversations, setActiveConversation, deleteConversation, renameConversation, createConversation, } = useAIChatStore(); const { setNotebookVisible, setNotebookActiveTab } = useNotebookStore(); const [editingId, setEditingId] = useState(null); const [editTitle, setEditTitle] = useState(''); const bookData = getBookData(bookKey); const bookHash = bookKey.split('-')[0] || ''; const bookTitle = bookData?.book?.title || 'Unknown'; // Load conversations for this book useEffect(() => { if (bookHash) { loadConversations(bookHash); } }, [bookHash, loadConversations]); const handleSelectConversation = useCallback( async (conversation: AIConversation) => { await setActiveConversation(conversation.id); setNotebookVisible(true); setNotebookActiveTab('ai'); }, [setActiveConversation, setNotebookVisible, setNotebookActiveTab], ); const handleNewConversation = useCallback(async () => { await createConversation(bookHash, `Chat about ${bookTitle}`); setNotebookVisible(true); setNotebookActiveTab('ai'); }, [bookHash, bookTitle, createConversation, setNotebookVisible, setNotebookActiveTab]); const handleDeleteConversation = useCallback( async (e: React.MouseEvent, id: string) => { e.stopPropagation(); if (!appService) return; if (await appService.ask(_('Delete this conversation?'))) { await deleteConversation(id); } }, [deleteConversation, _, appService], ); const handleStartRename = useCallback((e: React.MouseEvent, conversation: AIConversation) => { e.stopPropagation(); setEditingId(conversation.id); setEditTitle(conversation.title); }, []); const handleSaveRename = useCallback( async (e: React.MouseEvent | React.KeyboardEvent) => { e.stopPropagation(); if (editingId && editTitle.trim()) { await renameConversation(editingId, editTitle.trim()); } setEditingId(null); setEditTitle(''); }, [editingId, editTitle, renameConversation], ); const handleCancelRename = useCallback((e: React.MouseEvent) => { e.stopPropagation(); setEditingId(null); setEditTitle(''); }, []); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSaveRename(e); } else if (e.key === 'Escape') { setEditingId(null); setEditTitle(''); } }, [handleSaveRename], ); if (isLoadingHistory) { return (
); } return (
{/* Conversation list */}
{conversations.length === 0 ? (

{_('No conversations yet')}

{_('Start a new chat to ask questions about this book')}

) : (
    {conversations.map((conversation) => (
  • handleSelectConversation(conversation)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleSelectConversation(conversation); } }} >
    {editingId === conversation.id ? (
    e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} > setEditTitle(e.target.value)} onKeyDown={handleKeyDown} className={clsx( 'input input-xs input-bordered w-full', 'bg-base-100 text-base-content', )} // eslint-disable-next-line jsx-a11y/no-autofocus autoFocus />
    ) : ( <>

    {conversation.title}

    {dayjs(conversation.updatedAt).format('MMM D, YYYY h:mm A')}

    )}
    {editingId !== conversation.id && (
    )}
  • ))}
)}
{/* Floating New Chat button at bottom right */}
); }; export default ChatHistoryView;