From adb7132e02b29a32f175dd7c5083dabc69fd3042 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Thu, 5 Sep 2024 02:34:15 +0530 Subject: [PATCH] conversation: refined scrolling --- frontend/src/conversation/Conversation.tsx | 41 +++++++--------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/frontend/src/conversation/Conversation.tsx b/frontend/src/conversation/Conversation.tsx index e585e784..cf477e8f 100644 --- a/frontend/src/conversation/Conversation.tsx +++ b/frontend/src/conversation/Conversation.tsx @@ -1,4 +1,4 @@ -import React, { Fragment, useEffect, useRef, useState } from 'react'; +import { Fragment, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch, useSelector } from 'react-redux'; @@ -30,7 +30,7 @@ export default function Conversation() { const status = useSelector(selectStatus); const conversationId = useSelector(selectConversationId); const dispatch = useDispatch(); - const endMessageRef = useRef(null); + const conversationRef = useRef(null); const inputRef = useRef(null); const [isDarkTheme] = useDarkTheme(); const [hasScrolledToLast, setHasScrolledToLast] = useState(true); @@ -58,26 +58,6 @@ export default function Conversation() { fetchStream.current && fetchStream.current.abort(); }, [conversationId]); - useEffect(() => { - const observerCallback: IntersectionObserverCallback = (entries) => { - entries.forEach((entry) => { - setHasScrolledToLast(entry.isIntersecting); - }); - }; - - const observer = new IntersectionObserver(observerCallback, { - root: null, - threshold: [1, 0.8], - }); - if (endMessageRef.current) { - observer.observe(endMessageRef.current); - } - - return () => { - observer.disconnect(); - }; - }, [endMessageRef.current]); - useEffect(() => { if (queries.length) { queries[queries.length - 1].error && setLastQueryReturnedErr(true); @@ -86,10 +66,16 @@ export default function Conversation() { }, [queries[queries.length - 1]]); const scrollIntoView = () => { - endMessageRef?.current?.scrollIntoView({ - behavior: 'smooth', - block: 'start', - }); + if (!conversationRef?.current || eventInterrupt) return; + + if (status === 'idle') { + conversationRef.current.scrollTo({ + behavior: 'smooth', + top: conversationRef.current.scrollHeight, + }); + } else { + conversationRef.current.scrollTop = conversationRef.current.scrollHeight; + } }; const handleQuestion = ({ @@ -143,7 +129,6 @@ export default function Conversation() { if (query.response) { responseView = ( )}