Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): settle scroll to bottom after loading chat history (#802)

Hamm01011de795cace86
diff --git a/web/app/(workspace)/home/[[...sessionId]]/page.tsx b/web/app/(workspace)/home/[[...sessionId]]/page.tsx
index a25bb55f4..611efe857 100644
--- a/web/app/(workspace)/home/[[...sessionId]]/page.tsx
+++ b/web/app/(workspace)/home/[[...sessionId]]/page.tsx
@@ -982,6 +982,26 @@ export default function ChatPage() {
if (!ctrl.signal.aborted) {
loadAbortRef.current = null;
setSessionLoading(false);
+ // Settle at the bottom once the transcript is really laid out.
+ // The layout-effect pin runs as the messages first render, when
+ // lazily-loaded images (ChatMessages `loading="lazy"`) and the
+ // `next/dynamic` capability viewers have not contributed their
+ // heights yet, so its `scrollHeight` is short and the viewport
+ // stops above the true bottom. One frame later those are in.
+ //
+ // Only on a cold open. A cached session is already painted at
+ // the bottom and this resolves after a background revalidate —
+ // re-arming there would yank a reader who had scrolled up.
+ if (!cached) {
+ shouldAutoScrollRef.current = true;
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ // A newer session may have superseded this one while the
+ // two frames elapsed; that load owns the viewport now.
+ if (!ctrl.signal.aborted) scrollToBottom("instant");
+ });
+ });
+ }
}
})
.catch(() => {
@@ -994,7 +1014,7 @@ export default function ChatPage() {
}
});
},
- [loadSession, navigateToHome, showCachedSession],
+ [loadSession, navigateToHome, showCachedSession, scrollToBottom, shouldAutoScrollRef],
);
// Initial mount — load the session from the URL.
diff --git a/web/hooks/useChatAutoScroll.ts b/web/hooks/useChatAutoScroll.ts
index d9410171b..4d66212d5 100644
--- a/web/hooks/useChatAutoScroll.ts
+++ b/web/hooks/useChatAutoScroll.ts
@@ -201,14 +201,24 @@ export function useChatAutoScroll({
const mo = new MutationObserver(check);
mo.observe(container, { childList: true, subtree: true });
+ // An image or iframe finishing its network load grows the content
+ // without mutating the DOM, so the MutationObserver above never sees
+ // it — a turn that ends with a generated image would settle just
+ // above the bottom. The streaming branch already listens for this;
+ // mirror it here for the window right after the stream stops.
+ // (Opening a history session is not this path: that effect does not
+ // re-run on a session switch, so the page re-pins there itself.)
+ container.addEventListener("load", check, true);
const stopTimer = window.setTimeout(() => {
mo.disconnect();
+ container.removeEventListener("load", check, true);
if (rafId) cancelAnimationFrame(rafId);
}, POST_STREAM_AUTOSCROLL_WINDOW_MS);
return () => {
window.clearTimeout(stopTimer);
mo.disconnect();
+ container.removeEventListener("load", check, true);
if (rafId) cancelAnimationFrame(rafId);
};
}, [hasMessages, isStreaming, pinToBottom]);
diff --git a/web/node_modules b/web/node_modules
new file mode 120000
index 000000000..1b1a9aa1b
--- /dev/null
+++ b/web/node_modules
@@ -0,0 +1 @@
+/Users/frank/HKUDS/DeepTutor/web/node_modules
\ No newline at end of file