diff --git a/components/Chat/Chat.tsx b/components/Chat/Chat.tsx index 5e41ef3..ff2a746 100644 --- a/components/Chat/Chat.tsx +++ b/components/Chat/Chat.tsx @@ -22,17 +22,46 @@ interface Props { export const Chat: FC = ({ conversation, models, messageIsStreaming, modelError, messageError, loading, lightMode, onSend, onUpdateConversation, stopConversationRef }) => { const [currentMessage, setCurrentMessage] = useState(); + const [autoScrollEnabled, setAutoScrollEnabled] = useState(true); const messagesEndRef = useRef(null); + const chatContainerRef = useRef(null); const scrollToBottom = () => { - messagesEndRef.current?.scrollIntoView({ behavior: "auto" }); + if (autoScrollEnabled) { + messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); + } + }; + + const handleScroll = () => { + if (chatContainerRef.current) { + const { scrollTop, scrollHeight, clientHeight } = chatContainerRef.current; + const bottomTolerance = 50; + + if (scrollTop + clientHeight < scrollHeight - bottomTolerance) { + setAutoScrollEnabled(false); + } else { + setAutoScrollEnabled(true); + } + } }; useEffect(() => { scrollToBottom(); }, [conversation.messages]); + useEffect(() => { + const chatContainer = chatContainerRef.current; + + if (chatContainer) { + chatContainer.addEventListener("scroll", handleScroll); + + return () => { + chatContainer.removeEventListener("scroll", handleScroll); + }; + } + }, []); + return (
{modelError ? ( @@ -43,7 +72,10 @@ export const Chat: FC = ({ conversation, models, messageIsStreaming, mode
) : ( <> -
+
{conversation.messages.length === 0 ? ( <>