import React, { useEffect, useState, useRef, useMemo } from 'react'; import Spinner from '../svg/Spinner'; import { CSSTransition } from 'react-transition-group'; import ScrollToBottom from './ScrollToBottom'; import MultiMessage from './MultiMessage'; import { useSelector } from 'react-redux'; const Messages = ({ messages, messageTree }) => { const [currentEditId, setCurrentEditId] = useState(-1); const { conversationId } = useSelector((state) => state.convo); const { model, customModel, chatGptLabel } = useSelector((state) => state.submit); const { models } = useSelector((state) => state.models); const [showScrollButton, setShowScrollButton] = useState(false); const scrollableRef = useRef(null); const messagesEndRef = useRef(null); const modelName = models.find(element => element.model==model)?.name useEffect(() => { const timeoutId = setTimeout(() => { const scrollable = scrollableRef.current; const hasScrollbar = scrollable.scrollHeight > scrollable.clientHeight; setShowScrollButton(hasScrollbar); }, 650); return () => { clearTimeout(timeoutId); }; }, [messages]); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); setShowScrollButton(false); }; const handleScroll = () => { const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current; const diff = Math.abs(scrollHeight - scrollTop); const bottom = diff === clientHeight || (diff <= clientHeight + 25 && diff >= clientHeight - 25); if (bottom) { setShowScrollButton(false); } else { setShowScrollButton(true); } }; let timeoutId = null; const debouncedHandleScroll = () => { clearTimeout(timeoutId); timeoutId = setTimeout(handleScroll, 100); }; const scrollHandler = (e) => { e.preventDefault(); scrollToBottom(); }; return (
{/*
*/}
Model: {modelName} {customModel?`(${customModel})`:null}
{messageTree.length === 0 ? ( ) : ( <> {() => showScrollButton && } )}
{/*
*/}
); }; export default React.memo(Messages);