import ReactMarkdown from 'react-markdown'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import rehypeRaw from 'rehype-raw'; import { getChatMarkdownComponents, getTitleMarkdownComponents } from '../utils/markdownComponents.jsx'; import SimpleChat from './SimpleChat.jsx'; import ChunkLoadingTips from './ChunkLoadingTips.jsx'; import React, { useState, useEffect } from 'react'; const ChunkPanel = ({ documentData, currentChunkIndex, showChat, isTransitioning, updateGlobalChatHistory, getGlobalChatHistory, addMessageToChunk, getCurrentChunkMessages, hasChunkMessages, isChunkCompleted, canEditChunk, setWaitingForFirstResponse, markChunkUnderstood, skipChunk, goToPrevChunk }) => { const chatMarkdownComponents = getChatMarkdownComponents(); const titleMarkdownComponents = getTitleMarkdownComponents(); const [isLoading, setIsLoading] = useState(false); // Generate greeting for chunks that don't have messages yet // Only for initial chunk (0) and when not transitioning useEffect(() => { if (documentData && showChat && !hasChunkMessages(currentChunkIndex) && currentChunkIndex === 0 && !isTransitioning) { generateGreeting(); } }, [currentChunkIndex, documentData, showChat, isTransitioning]); const generateGreeting = async () => { setIsLoading(true); if (setWaitingForFirstResponse) { setWaitingForFirstResponse(true); } try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ messages: [], currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '', document: documentData ? JSON.stringify(documentData) : '' }) }); const data = await response.json(); addMessageToChunk( { role: 'assistant', content: data.content || 'Hi! Welcome to your learning session. Let\'s explore this document together!' }, currentChunkIndex ); } catch (error) { console.error('Error generating greeting:', error); addMessageToChunk( { role: 'assistant', content: 'Hi! Welcome to your learning session. Let\'s explore this document together!' }, currentChunkIndex ); } finally { setIsLoading(false); if (setWaitingForFirstResponse) { setWaitingForFirstResponse(false); } } }; const handleSend = async (text) => { const userMessage = { role: 'user', content: text, chunkIndex: currentChunkIndex }; addMessageToChunk(userMessage, currentChunkIndex); setIsLoading(true); try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ messages: getGlobalChatHistory(), currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '', document: documentData ? JSON.stringify(documentData) : '' }) }); const data = await response.json(); addMessageToChunk( { role: 'assistant', content: data.content || 'Sorry, no response received.' }, currentChunkIndex ); } catch (error) { console.error('Error:', error); addMessageToChunk( { role: 'assistant', content: 'Sorry, something went wrong. Please try again.' }, currentChunkIndex ); } finally { setIsLoading(false); } }; return ( <> {/* Chunk Header */}
{/* Previous Chunk Button */} {/* Chunk Title */}
{documentData?.chunks?.[currentChunkIndex]?.topic || "Loading..."}
{/* Skip Button */} {/* Understood Button */}
{/* Chat Interface - Only shown when showChat is true and not transitioning */} {showChat && !isLoading && !isTransitioning && ( )} {/* Loading Tips - Shown when generating greeting */} {showChat && isLoading && !hasChunkMessages(currentChunkIndex) && ( )} {/* Transition Loading - Shown when moving between chunks */} {showChat && isTransitioning && ( )} ); }; export default ChunkPanel;