fix(web): keep following the stream after scrolling back to the live edge (#6519)

This commit is contained in:
Lars Nieuwenhuis authored and GitHub committed 2026-08-20 22:11:16 -07:00
1 parent ce8ca5bb3d
commit e2697d63ec
2 files changed
+70 -3

No files matched your search

+20 -3
View File
@@ -1205,6 +1205,20 @@ function chatActionErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : "An error occurred.";
}
/**
* Drops the send-time anchored end space. That space is what holds a sent
* message near the top while its turn streams, and it keeps LegendList's
* maintainScrollAtEnd switched off for as long as it is installed — ChatView
* drives the streaming scrolls itself, but only in "anchoring-new-turn" mode.
* So every return to the live edge has to release the anchor too, otherwise the
* timeline settles into "following-end" with nothing following anything.
*/
function releaseChatTimelineAnchor<T extends { readonly messageId: MessageId | null }>(
current: T,
): T {
return current.messageId === null ? current : { ...current, messageId: null };
}
function ChatViewContent(props: ChatViewProps) {
const {
environmentId,
@@ -3833,9 +3847,7 @@ function ChatViewContent(props: ChatViewProps) {
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setTimelineAnchor((current) =>
current.messageId === null ? current : { ...current, messageId: null },
);
setTimelineAnchor(releaseChatTimelineAnchor);
requestAnimationFrame(() => {
void legendListRef.current?.scrollToEnd?.({ animated });
});
@@ -4010,6 +4022,11 @@ function ChatViewContent(props: ChatViewProps) {
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
// Reachable only once manual navigation has already broken follow, so
// the anchored turn framing is over: the user scrolled back to the live
// edge and expects the stream to stick to it again, exactly like the
// scroll-to-bottom pill.
setTimelineAnchor(releaseChatTimelineAnchor);
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
} else {
@@ -480,6 +480,56 @@ describe("MessagesTimeline", () => {
expect(onAnchorReady).toHaveBeenCalledWith(secondEntry.message.id, 1);
});
it("hands end-following back to the list once the send anchor is released", () => {
const firstEntry = buildUserTimelineEntry("First prompt.");
const secondEntry = {
...buildUserTimelineEntry("Newest prompt."),
id: "entry-2",
message: {
...buildUserTimelineEntry("Newest prompt.").message,
id: MessageId.make("message-2"),
},
};
const timelineEntries = [firstEntry, secondEntry];
// While the send anchor holds the end space open, ChatView owns streaming
// scrolls and LegendList must not re-pin behind it.
expect(
renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
anchorMessageId={secondEntry.message.id}
timelineEntries={timelineEntries}
/>,
),
).not.toContain('data-maintain-scroll-at-end="enabled"');
// Dropping the anchor is what actually gives end-following back, so
// returning to the live edge has to release it — re-enabling live follow
// alone leaves nothing pinned to the stream.
expect(
renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
anchorMessageId={null}
timelineEntries={timelineEntries}
/>,
),
).toContain('data-maintain-scroll-at-end="enabled"');
// Reading history still wins over both.
expect(
renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
anchorMessageId={null}
liveFollowEnabled={false}
timelineEntries={timelineEntries}
/>,
),
).not.toContain('data-maintain-scroll-at-end="enabled"');
});
it("renders collapse controls for long user messages", () => {
const markup = renderToStaticMarkup(
<MessagesTimeline