import React, { ForwardedRef, useState, useEffect, useRef } from 'react'; import Markdown from 'react-markdown'; import rehypeRaw from 'rehype-raw'; import remarkGfm from 'remark-gfm'; import { useChatHistory } from '../hooks/useChatHistory'; type Message = { role: string; content: string; }; interface ConversationProps { messages: Message[]; onStopClick: () => void; onResendClick: () => void; onEditClick: () => void; onCopyClick: () => void; isClicked: boolean; } const ConversationFrontend = React.forwardRef( ({ messages, onStopClick, onResendClick, onEditClick, onCopyClick, isClicked }, ref: ForwardedRef) => { const [isScrolling, setIsScrolling] = useState(true); const messagesEndRef = useRef(null); const [chatHistory] = useChatHistory() useEffect(() => { const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting) { setIsScrolling(true); } else { setIsScrolling(false); } }, { root: document.querySelector('.output'), threshold: 1.0, } ); const endOfMessages = messagesEndRef.current; if (endOfMessages) { observer.observe(endOfMessages); } return () => { if (endOfMessages) { observer.unobserve(endOfMessages); } }; }, [messages]); useEffect(() => { if (isScrolling) { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); } }, [messages, isScrolling]); return (
{chatHistory.chats[chatHistory.selectedIndex].messages.map((message, index) => { if (index >= 1) { return (
{message.content}
); } return null; // Ensure to return null for the first message or if condition is not met })}
Stop
Resend
Edit
{isClicked ? "Copied!" : "Copy"}
); } ); ConversationFrontend.displayName = "ConversationFrontend"; export default ConversationFrontend;