import { useState, useEffect, useRef } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import rehypeRaw from 'rehype-raw'; import { getChatMarkdownComponents } from '../utils/markdownComponents.jsx'; const SimpleChat = ({ messages, currentChunkIndex, canEdit, onSend, isLoading }) => { const [input, setInput] = useState(''); const messagesEndRef = useRef(null); const currentChunkStartRef = useRef(null); const handleSubmit = (e) => { e.preventDefault(); if (!input.trim() || isLoading || !canEdit) return; onSend(input.trim()); setInput(''); }; // Scroll to current chunk's first message when chunk changes useEffect(() => { if (currentChunkStartRef.current) { currentChunkStartRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }, [currentChunkIndex]); // Find the first message of the current chunk const currentChunkFirstMessageIndex = messages.findIndex(msg => msg.chunkIndex === currentChunkIndex); return (
{/* Messages */}
{messages.map((message, idx) => { const isCurrentChunk = message.chunkIndex === currentChunkIndex; const isFirstOfCurrentChunk = idx === currentChunkFirstMessageIndex; return (
{message.content}
); })} {isLoading && (
)}
{/* Input */}
setInput(e.target.value)} placeholder={canEdit ? "Type your message..." : "This chunk is completed - navigation only"} disabled={isLoading || !canEdit} className="flex-1 px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:text-gray-500" />
); }; export default SimpleChat;