Skip to content

Commit 557085a

Browse files
authored
Scroll chat view to the latest message (#478)
- Add a bottom anchor to keep the conversation pinned to the newest message - Fall back to the scroll container when scrolling programmatically
1 parent b585230 commit 557085a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

apps/web/src/components/ChatView.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ export default function ChatView({
606606
LastInvokedScriptByProjectSchema,
607607
);
608608
const messagesScrollRef = useRef<HTMLDivElement>(null);
609+
const messagesBottomRef = useRef<HTMLDivElement>(null);
609610
const [messagesScrollElement, setMessagesScrollElement] = useState<HTMLDivElement | null>(null);
610611
const shouldAutoScrollRef = useRef(true);
611612
const lastKnownScrollTopRef = useRef(0);
@@ -2318,9 +2319,13 @@ export default function ChatView({
23182319
const messageCount = timelineMessages.length;
23192320
const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
23202321
const scrollContainer = messagesScrollRef.current;
2321-
if (!scrollContainer) return;
2322-
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
2323-
lastKnownScrollTopRef.current = scrollContainer.scrollTop;
2322+
const bottomAnchor = messagesBottomRef.current;
2323+
if (!scrollContainer && !bottomAnchor) return;
2324+
bottomAnchor?.scrollIntoView({ block: "end", behavior });
2325+
scrollContainer?.scrollTo({ top: scrollContainer.scrollHeight, behavior });
2326+
if (scrollContainer) {
2327+
lastKnownScrollTopRef.current = scrollContainer.scrollTop;
2328+
}
23242329
shouldAutoScrollRef.current = true;
23252330
}, []);
23262331
const cancelPendingStickToBottom = useCallback(() => {
@@ -5095,6 +5100,7 @@ export default function ChatView({
50955100
onOpenSettings={() => void navigate({ to: "/settings" })}
50965101
onOpenTurnDiff={handleOpenTurnDiff}
50975102
/>
5103+
<div ref={messagesBottomRef} aria-hidden="true" className="h-px w-full" />
50985104
</div>
50995105

51005106
{/* scroll to bottom pill — shown when user has scrolled away from the bottom */}

0 commit comments

Comments
 (0)