Skip to content

Commit e1e4b35

Browse files
ECutericlaude
andcommitted
fix(web): switched threads and Scroll to end land on the real end
LegendList's scrollToEnd targets estimated row sizes. When a reused timeline switches to another thread, or Scroll to end is pressed, a final reply far taller than the estimate left the viewport hundreds of pixels above the end, with the end state already reported. Both paths now settle against the DOM, the same way a remembered row restore already did, until the viewport holds the measured end for two layout frames. A manual scroll still stops the correction. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5ffe045 commit e1e4b35

2 files changed

Lines changed: 79 additions & 42 deletions

File tree

‎apps/web/src/components/chat/useTimelineFollow.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { type ChatComposerHandle } from "./ChatComposer";
2828
import { createPageScrollController, type PageScrollKey } from "./pageScrollController";
2929
import { createUpwardScrollDetector, isTimelineScrollTarget } from "./timelineScrollTarget";
3030
import { resolveTimelineIsAtEnd } from "./MessagesTimeline.logic";
31+
import { measuredEndOffset, settleListScroll } from "./useTimelinePositionRestore";
3132
import { shouldReleaseTimelineAnchorForToolActivity } from "../ChatView.logic";
3233
import { releaseChatTimelineAnchor } from "../ChatView.logic";
3334
import type { Dispatch, RefObject } from "react";
@@ -272,9 +273,19 @@ export function useTimelineFollow(input: TimelineFollowInput) {
272273
// The anchored end space must be gone before the scroll measures, or the
273274
// list lands short of the real end (#6519).
274275
requestAnimationFrame(() => {
275-
void legendListRef.current?.scrollToEnd?.({
276-
animated: animated && !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
277-
});
276+
const list = legendListRef.current;
277+
if (!list) return;
278+
void Promise.resolve(
279+
list.scrollToEnd?.({
280+
animated: animated && !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
281+
}),
282+
).then(() =>
283+
settleListScroll(
284+
list,
285+
measuredEndOffset,
286+
() => timelineScrollModeRef.current !== "following-end" || legendListRef.current !== list,
287+
),
288+
);
278289
});
279290
};
280291
const scrollToEndStable = useStableCallback(scrollToEnd);

‎apps/web/src/components/chat/useTimelinePositionRestore.ts‎

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ import type { AssistantCitationRequest } from "./AssistantCitationSource";
44
import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
55
import type { RememberedTimelinePosition } from "./timelineScrollAnchoring";
66

7+
/**
8+
* Index and end scrolling start from estimated row sizes, so they can stop
9+
* short of a tall row. Corrects against the DOM until the viewport holds the
10+
* target for two layout frames; resolves false when the target disappears.
11+
*/
12+
export function settleListScroll(
13+
list: LegendListRef,
14+
target: (element: HTMLElement) => number | undefined,
15+
isCancelled: () => boolean,
16+
): Promise<boolean> {
17+
return new Promise((resolve) => {
18+
let stableFrames = 0;
19+
const reconcile = () => {
20+
if (isCancelled()) return resolve(false);
21+
const element = list.getScrollableNode();
22+
const offset = element ? target(element) : undefined;
23+
if (!element || offset === undefined) return resolve(false);
24+
if (Math.abs(element.scrollTop - offset) > 1) {
25+
stableFrames = 0;
26+
void list.scrollToOffset({ offset, animated: false }).then(() => {
27+
requestAnimationFrame(reconcile);
28+
});
29+
return;
30+
}
31+
stableFrames += 1;
32+
if (stableFrames >= 2) resolve(true);
33+
else requestAnimationFrame(reconcile);
34+
};
35+
requestAnimationFrame(reconcile);
36+
});
37+
}
38+
39+
export const measuredEndOffset = (element: HTMLElement) =>
40+
element.scrollHeight - element.clientHeight;
41+
742
/**
843
* Scrolls a newly shown thread back to where the user left it, or to its end,
944
* and reports the thread positioned once the saved row settles.
@@ -55,12 +90,10 @@ export function useTimelinePositionRestore({
5590
return;
5691
}
5792
let cancelled = false;
58-
let settleFrame: number | null = null;
5993
const viewport: HTMLElement | null = list.getScrollableNode();
6094
const cancelRestoration = () => {
6195
if (cancelled) return;
6296
cancelled = true;
63-
if (settleFrame !== null) cancelAnimationFrame(settleFrame);
6497
if (viewport) void list.scrollToOffset({ offset: viewport.scrollTop, animated: false });
6598
onPositioned(listIdentityKey);
6699
};
@@ -99,54 +132,47 @@ export function useTimelinePositionRestore({
99132
: listOpenedOnRef.current?.identity === listIdentityKey
100133
? undefined
101134
: list.scrollToEnd({ animated: false });
102-
void Promise.resolve(scrolling).then(() => {
135+
const settlesAtEnd = position?.atEnd !== false && scrolling !== undefined;
136+
void Promise.resolve(scrolling).then(async () => {
103137
if (cancelled) return;
138+
if (settlesAtEnd) {
139+
if (await settleListScroll(list, measuredEndOffset, () => cancelled)) {
140+
onPositioned(listIdentityKey);
141+
}
142+
return;
143+
}
104144
if (position?.atEnd !== false || index < 0) {
105145
onPositioned(listIdentityKey);
106146
return;
107147
}
108-
// Index scrolling starts from estimates. Keep the saved row mounted
109-
// until its measured position and the DOM agree for two layout frames.
110-
let stableFrames = 0;
111-
const reconcile = () => {
112-
if (cancelled) return;
113-
const state = list.getState();
114-
const rowIndex = state.indexByKey(position.rowId);
115-
const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);
116-
const element = list.getScrollableNode();
117-
if (!row || !element) return;
118-
const offset = Math.max(
119-
0,
120-
Math.min(
121-
element.scrollTop +
122-
row.getBoundingClientRect().top -
123-
element.getBoundingClientRect().top +
124-
position.offsetWithinRow,
125-
element.scrollHeight - element.clientHeight,
126-
),
127-
);
128-
if (Math.abs(element.scrollTop - offset) > 1) {
129-
stableFrames = 0;
130-
void list.scrollToOffset({ offset, animated: false }).then(() => {
131-
if (!cancelled) settleFrame = requestAnimationFrame(reconcile);
132-
});
133-
return;
134-
}
135-
stableFrames += 1;
136-
if (stableFrames >= 2) {
137-
onPositioned(listIdentityKey);
138-
} else {
139-
settleFrame = requestAnimationFrame(reconcile);
140-
}
141-
};
142-
settleFrame = requestAnimationFrame(reconcile);
148+
// Keep the saved row mounted until its measured position and the DOM agree.
149+
const settled = await settleListScroll(
150+
list,
151+
(element) => {
152+
const state = list.getState();
153+
const rowIndex = state.indexByKey(position.rowId);
154+
const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);
155+
if (!row) return undefined;
156+
return Math.max(
157+
0,
158+
Math.min(
159+
element.scrollTop +
160+
row.getBoundingClientRect().top -
161+
element.getBoundingClientRect().top +
162+
position.offsetWithinRow,
163+
measuredEndOffset(element),
164+
),
165+
);
166+
},
167+
() => cancelled,
168+
);
169+
if (settled) onPositioned(listIdentityKey);
143170
});
144171
return () => {
145172
cancelled = true;
146173
if (cancelPositionRestoreRef?.current === cancelRestoration) {
147174
cancelPositionRestoreRef.current = null;
148175
}
149-
if (settleFrame !== null) cancelAnimationFrame(settleFrame);
150176
viewport?.removeEventListener("wheel", cancelForNavigation);
151177
viewport?.removeEventListener("touchmove", cancelForNavigation);
152178
viewport?.removeEventListener("pointerdown", cancelForNavigation);

0 commit comments

Comments
 (0)