import React, { ForwardedRef, useState, useEffect, useRef } from 'react'; import Markdown from 'react-markdown' import rehypeRaw from 'rehype-raw'; import remarkGfm from 'remark-gfm'; 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); useEffect(() => { const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting) { console.log('End of messages reached'); setIsScrolling(true); } else { console.log('End of messages not reached'); setIsScrolling(false); } }, { root: document.querySelector('.output'), threshold: 1.0, // Ensure the whole element is visible } ); const endOfMessages = messagesEndRef.current; if (endOfMessages) { observer.observe(endOfMessages); } return () => { if (endOfMessages) { observer.unobserve(endOfMessages); } }; }, [messages]); // Scroll to bottom when new messages arrive useEffect(() => { if (isScrolling) { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); } }, [messages, isScrolling]); return (
{messages.map((message, index) => { if (index >= 1) { return (
{message.content}
); } })}
Stop
Resend
Edit
{isClicked?"Copied!": "Copy" }
); } ); export default ConversationFrontend;