diff --git a/extension/src/components/Room.tsx b/extension/src/components/Room.tsx index 583f603..461abd7 100644 --- a/extension/src/components/Room.tsx +++ b/extension/src/components/Room.tsx @@ -1,5 +1,5 @@ import { io, Socket } from "socket.io-client"; -import { useEffect, useRef, useState } from "react"; +import { CSSProperties, useEffect, useRef, useState } from "react"; import Question from "./Question"; import { QuestionInterface, SubmissionStatus } from "../types/Question"; import CopyIcon from "../icons/CopyIcon"; @@ -49,6 +49,8 @@ export default function Room({ duration: number | undefined | null; }) { const isLoadingGlobal = useIsMutating(); + const [isAutoScrollEnabled, setIsAutoScrollEnabled] = useState(true); + let inputRef = useRef(null); let messagesRef = useRef(null); let [messages, setMessages] = useState([ @@ -238,9 +240,16 @@ export default function Room({ }; }, [roomId]); + function handleScroll(e: React.UIEvent) { + const target = e.target as HTMLDivElement; + const threshold = 50; + const isAtBottom = target.scrollHeight - target.scrollTop - threshold <= target.clientHeight; + setIsAutoScrollEnabled(isAtBottom); + } + useEffect(() => { function autoScrollToLatestMessage() { - if (!messagesRef.current) { + if (!messagesRef.current || !isAutoScrollEnabled) { return; } @@ -249,9 +258,9 @@ export default function Room({ behavior: "auto", }); } - - autoScrollToLatestMessage(); - }, [messages]); + if(isAutoScrollEnabled) + autoScrollToLatestMessage(); + }, [messages, isAutoScrollEnabled]); return (
@@ -307,15 +316,22 @@ export default function Room({ )}
+
    {messages.map((message, index) => ( ))}
+ {!isAutoScrollEnabled && ( +
+ Auto-scroll paused. Scroll down to resume. +
+ )}