diff --git a/gui/package-lock.json b/gui/package-lock.json index 91f4adfe0..e8471ee68 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -37,6 +37,7 @@ "react-dom": "^18.2.0", "react-error-boundary": "^4.0.11", "react-hook-form": "^7.47.0", + "react-intersection-observer": "^9.10.3", "react-markdown": "^9.0.1", "react-redux": "^8.0.5", "react-remark": "^2.1.0", @@ -6078,6 +6079,20 @@ "react": "^16.8.0 || ^17 || ^18" } }, + "node_modules/react-intersection-observer": { + "version": "9.10.3", + "resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.10.3.tgz", + "integrity": "sha512-9NYfKwPZRovB6QJee7fDg0zz/SyYrqXtn5xTZU0vwLtLVBtfu9aZt1pVmr825REE49VPDZ7Lm5SNHjJBOTZHpA==", + "peerDependencies": { + "react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", + "react-dom": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" + }, + "peerDependenciesMeta": { + "react-dom": { + "optional": true + } + } + }, "node_modules/react-is": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", diff --git a/gui/package.json b/gui/package.json index 3b9842d82..b2a4426e8 100644 --- a/gui/package.json +++ b/gui/package.json @@ -39,6 +39,7 @@ "react-dom": "^18.2.0", "react-error-boundary": "^4.0.11", "react-hook-form": "^7.47.0", + "react-intersection-observer": "^9.10.3", "react-markdown": "^9.0.1", "react-redux": "^8.0.5", "react-remark": "^2.1.0", diff --git a/gui/src/components/ChatScrollAnchor.tsx b/gui/src/components/ChatScrollAnchor.tsx new file mode 100644 index 000000000..59e59ab9b --- /dev/null +++ b/gui/src/components/ChatScrollAnchor.tsx @@ -0,0 +1,32 @@ +import { useEffect } from "react"; +import { useInView } from "react-intersection-observer"; + +interface ChatScrollAnchorProps { + trackVisibility: boolean; + isAtBottom: boolean; + scrollAreaRef: React.RefObject; +} + +export function ChatScrollAnchor({ + trackVisibility, + isAtBottom, + scrollAreaRef, +}: ChatScrollAnchorProps) { + const { ref, inView, entry } = useInView({ + trackVisibility, + delay: 100, + }); + + useEffect(() => { + if (isAtBottom && trackVisibility && !inView) { + if (!scrollAreaRef.current) return; + + const scrollAreaElement = scrollAreaRef.current; + + scrollAreaElement.scrollTop = + scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight; + } + }, [inView, entry, isAtBottom, trackVisibility]); + + return
; +} diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index 7fffbcece..c1f07e72c 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -55,6 +55,7 @@ import { } from "../util"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial"; +import { ChatScrollAnchor } from "../components/ChatScrollAnchor"; const TopGuiDiv = styled.div` overflow-y: scroll; @@ -148,74 +149,48 @@ interface GUIProps { } function GUI(props: GUIProps) { - // #region Hooks const posthog = usePostHog(); const dispatch = useDispatch(); const navigate = useNavigate(); const ideMessenger = useContext(IdeMessengerContext); - // #endregion - - // #region Selectors const sessionState = useSelector((state: RootState) => state.state); const defaultModel = useSelector(defaultModelSelector); const active = useSelector((state: RootState) => state.state.active); - // #endregion - - // #region State const [stepsOpen, setStepsOpen] = useState<(boolean | undefined)[]>([]); - const [showLoading, setShowLoading] = useState(false); - - useEffect(() => { - setTimeout(() => { - setShowLoading(true); - }, 5000); - }, []); - - // #endregion const mainTextInputRef = useRef(null); const topGuiDivRef = useRef(null); - // #region Effects - const [userScrolledAwayFromBottom, setUserScrolledAwayFromBottom] = - useState(false); + const [isAtBottom, setIsAtBottom] = useState(false); const state = useSelector((state: RootState) => state.state); + const handleScroll = () => { + // Temporary fix to account for additional height when code blocks are added + const OFFSET_HERUISTIC = 300; + if (!topGuiDivRef.current) return; + + const { scrollTop, scrollHeight, clientHeight } = topGuiDivRef.current; + const atBottom = + scrollHeight - clientHeight <= scrollTop + OFFSET_HERUISTIC; + + setIsAtBottom(atBottom); + }; + useEffect(() => { - const handleScroll = () => { - // Scroll only if user is within 200 pixels of the bottom of the window. - const edgeOffset = -25; - const scrollPosition = topGuiDivRef.current?.scrollTop || 0; - const scrollHeight = topGuiDivRef.current?.scrollHeight || 0; - const clientHeight = window.innerHeight || 0; + if (!active || !topGuiDivRef.current) return; - if (scrollPosition + clientHeight + edgeOffset >= scrollHeight) { - setUserScrolledAwayFromBottom(false); - } else { - setUserScrolledAwayFromBottom(true); - } - }; + const scrollAreaElement = topGuiDivRef.current; - topGuiDivRef.current?.addEventListener("scroll", handleScroll); + scrollAreaElement.scrollTop = + scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight; - return () => { - window.removeEventListener("scroll", handleScroll); - }; - }, [topGuiDivRef.current]); - - useLayoutEffect(() => { - if (userScrolledAwayFromBottom) return; - - topGuiDivRef.current?.scrollTo({ - top: topGuiDivRef.current?.scrollHeight, - behavior: "instant" as any, - }); - }, [topGuiDivRef.current?.scrollHeight, sessionState.history]); + setIsAtBottom(true); + }, [active]); useEffect(() => { // Cmd + Backspace to delete current step @@ -365,7 +340,7 @@ function GUI(props: GUIProps) { return ( <> - +
{state.history.map((item, index: number) => { @@ -463,7 +438,6 @@ function GUI(props: GUIProps) { ); })} - { sendInput(editorContent, modifiers); @@ -499,6 +473,12 @@ function GUI(props: GUIProps) { ) : null}
+ +
{active && (