From dfd46286800f290562d0ac7ed4073ae1bd61a74b Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 11 Mar 2025 08:30:37 -0700 Subject: [PATCH] improv: clean up code --- gui/src/pages/gui/Chat.tsx | 53 +----------------------------- gui/src/pages/gui/useAutoScroll.ts | 53 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 gui/src/pages/gui/useAutoScroll.ts diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 9a50285d3..9fe39d41e 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -81,6 +81,7 @@ import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; import { ToolCallDiv } from "./ToolCallDiv"; import { ToolCallButtons } from "./ToolCallDiv/ToolCallButtonsDiv"; import ToolOutput from "./ToolCallDiv/ToolOutput"; +import { useAutoScroll } from "./useAutoScroll"; const StopButton = styled.div` background-color: ${vscBackground}; @@ -138,58 +139,6 @@ function fallbackRender({ error, resetErrorBoundary }: any) { ); } -const useAutoScroll = ( - ref: React.RefObject, - history: unknown[], -) => { - const [userHasScrolled, setUserHasScrolled] = useState(false); - - useEffect(() => { - if (history.length) { - setUserHasScrolled(false); - } - }, [history.length]); - - useEffect(() => { - if (!ref.current || history.length === 0) return; - - const handleScroll = () => { - const elem = ref.current; - if (!elem) return; - - const isAtBottom = - Math.abs(elem.scrollHeight - elem.scrollTop - elem.clientHeight) < 1; - - /** - * We stop auto scrolling if a user manually scrolled up. - * We resume auto scrolling if a user manually scrolled to the bottom. - */ - setUserHasScrolled(!isAtBottom); - }; - - const resizeObserver = new ResizeObserver(() => { - const elem = ref.current; - if (!elem || userHasScrolled) return; - elem.scrollTop = elem.scrollHeight; - }); - - ref.current.addEventListener("scroll", handleScroll); - - // Observe the container - resizeObserver.observe(ref.current); - - // Observe all immediate children - Array.from(ref.current.children).forEach((child) => { - resizeObserver.observe(child); - }); - - return () => { - resizeObserver.disconnect(); - ref.current?.removeEventListener("scroll", handleScroll); - }; - }, [ref, history.length, userHasScrolled]); -}; - export function Chat() { const posthog = usePostHog(); const dispatch = useAppDispatch(); diff --git a/gui/src/pages/gui/useAutoScroll.ts b/gui/src/pages/gui/useAutoScroll.ts new file mode 100644 index 000000000..a787e7ec9 --- /dev/null +++ b/gui/src/pages/gui/useAutoScroll.ts @@ -0,0 +1,53 @@ +import { useEffect, useState } from "react"; + +export const useAutoScroll = ( + ref: React.RefObject, + history: unknown[], +) => { + const [userHasScrolled, setUserHasScrolled] = useState(false); + + useEffect(() => { + if (history.length) { + setUserHasScrolled(false); + } + }, [history.length]); + + useEffect(() => { + if (!ref.current || history.length === 0) return; + + const handleScroll = () => { + const elem = ref.current; + if (!elem) return; + + const isAtBottom = + Math.abs(elem.scrollHeight - elem.scrollTop - elem.clientHeight) < 1; + + /** + * We stop auto scrolling if a user manually scrolled up. + * We resume auto scrolling if a user manually scrolled to the bottom. + */ + setUserHasScrolled(!isAtBottom); + }; + + const resizeObserver = new ResizeObserver(() => { + const elem = ref.current; + if (!elem || userHasScrolled) return; + elem.scrollTop = elem.scrollHeight; + }); + + ref.current.addEventListener("scroll", handleScroll); + + // Observe the container + resizeObserver.observe(ref.current); + + // Observe all immediate children + Array.from(ref.current.children).forEach((child) => { + resizeObserver.observe(child); + }); + + return () => { + resizeObserver.disconnect(); + ref.current?.removeEventListener("scroll", handleScroll); + }; + }, [ref, history.length, userHasScrolled]); +};