From 9ce9fcbd6fcd854f864c41e592599ad86faa039f Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 8 Nov 2024 11:16:48 -0800 Subject: [PATCH] smooth scroll to bottom on session id change --- gui/src/pages/gui/Chat.tsx | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 6ca0cc167..27e9be2cd 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -136,7 +136,6 @@ export function Chat() { const { streamResponse } = useChatHandler(dispatch, ideMessenger); const onboardingCard = useOnboardingCard(); const { showTutorialCard, closeTutorialCard } = useTutorialCard(); - const sessionState = useSelector((state: RootState) => state.state); const defaultModel = useSelector(defaultModelSelector); const ttsActive = useSelector((state: RootState) => state.state.ttsActive); const active = useSelector((state: RootState) => state.state.active); @@ -148,7 +147,7 @@ export function Chat() { const { saveSession, getLastSessionId, loadLastSession } = useHistory(dispatch); - const scrollToBottom = useCallback(() => { + const snapToBottom = useCallback(() => { if (!topGuiDivRef.current) return; const elem = topGuiDivRef.current; elem.scrollTop = elem.scrollHeight - elem.clientHeight; @@ -156,9 +155,26 @@ export function Chat() { setIsAtBottom(true); }, [topGuiDivRef, setIsAtBottom]); + const smoothScrollToBottom = useCallback(async () => { + if (!topGuiDivRef.current) return; + const elem = topGuiDivRef.current + elem.scrollTo({ + top: elem.scrollHeight - elem.clientHeight, + behavior: 'smooth' + }) + + setIsAtBottom(true); + }, [topGuiDivRef, setIsAtBottom]); + useEffect(() => { - if (active) scrollToBottom(); - }, [active, scrollToBottom]); + if (active) snapToBottom(); + }, [active, snapToBottom]); + + useEffect(() => { + setTimeout(() => { + smoothScrollToBottom(); + }, 250) + }, [smoothScrollToBottom, state.sessionId]); useEffect(() => { // Cmd + Backspace to delete current step @@ -284,8 +300,8 @@ export function Chat() { } }, [ - sessionState.history, - sessionState.contextItems, + state.history, + state.contextItems, defaultModel, state, streamResponse, @@ -370,11 +386,11 @@ export function Chat() { : true : stepsOpen[index]! } - onToggle={() => {}} + onToggle={() => { }} > {}} + onUserInput={(input: string) => { }} item={item} - onReverse={() => {}} + onReverse={() => { }} onRetry={() => { streamResponse( state.history[index - 1].editorState, state.history[index - 1].modifiers ?? - defaultInputModifiers, + defaultInputModifiers, ideMessenger, index - 1, );