diff --git a/client/src/components/Chat/Messages/MessagesView.tsx b/client/src/components/Chat/Messages/MessagesView.tsx index f41b578a51..3be5a51129 100644 --- a/client/src/components/Chat/Messages/MessagesView.tsx +++ b/client/src/components/Chat/Messages/MessagesView.tsx @@ -14,11 +14,12 @@ export default function MessagesView({ messagesTree?: TMessage[] | null; Header?: ReactNode; }) { + const timeoutIdRef = useRef(); const scrollableRef = useRef(null); const messagesEndRef = useRef(null); const [showScrollButton, setShowScrollButton] = useState(false); const [currentEditId, setCurrentEditId] = useState(-1); - const { conversation, showPopover, setAbortScroll } = useChatContext(); + const { conversation, setAbortScroll } = useChatContext(); const { conversationId } = conversation ?? {}; const { screenshotTargetRef } = useScreenshot(); @@ -36,24 +37,21 @@ export default function MessagesView({ }, [scrollableRef]); useLayoutEffect(() => { - const timeoutId = setTimeout(() => { - checkIfAtBottom(); - }, 650); - - // Add a listener on the window object - window.addEventListener('scroll', checkIfAtBottom); + const scrollableElement = scrollableRef.current; + if (!scrollableElement) { + return; + } + const timeoutId = setTimeout(checkIfAtBottom, 650); return () => { clearTimeout(timeoutId); - window.removeEventListener('scroll', checkIfAtBottom); }; - }, [_messagesTree, checkIfAtBottom]); + }, [checkIfAtBottom]); - let timeoutId: ReturnType | undefined; - const debouncedHandleScroll = () => { - clearTimeout(timeoutId); - timeoutId = setTimeout(checkIfAtBottom, 100); - }; + const debouncedHandleScroll = useCallback(() => { + clearTimeout(timeoutIdRef.current); + timeoutIdRef.current = setTimeout(checkIfAtBottom, 100); + }, [checkIfAtBottom]); const scrollCallback = () => setShowScrollButton(false); const { scrollToRef: scrollToBottom, handleSmoothToRef } = useScrollToRef({ @@ -66,13 +64,17 @@ export default function MessagesView({ }); return ( -
-
-
+
+
+
{(_messagesTree && _messagesTree?.length == 0) || _messagesTree === null ? (
@@ -90,18 +92,6 @@ export default function MessagesView({ setCurrentEditId={setCurrentEditId} currentEditId={currentEditId ?? null} /> - - {() => - showScrollButton && - !showPopover && - } -
)} @@ -111,6 +101,15 @@ export default function MessagesView({ />
+ + {() => showScrollButton && } +
); diff --git a/client/src/components/Messages/ScrollToBottom.tsx b/client/src/components/Messages/ScrollToBottom.tsx index 3c76555ff2..dc4b9a2298 100644 --- a/client/src/components/Messages/ScrollToBottom.tsx +++ b/client/src/components/Messages/ScrollToBottom.tsx @@ -6,7 +6,7 @@ export default function ScrollToBottom({ scrollHandler }: Props) { return (