Skip to content

Commit 2ec3d96

Browse files
committed
fix(web): preserve timeline follow intent across navigation
1 parent d81afa0 commit 2ec3d96

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
653653
const previousLatestRunRef = useRef(latestRun);
654654
// The list stays mounted across thread switches. Its first end pins on the
655655
// new thread must snap, not glide, even if that thread is mid-turn.
656-
const [settlingListIdentity, setSettlingListIdentity] = useState<string | null>(null);
656+
const [settlingListIdentity, setSettlingListIdentity] = useState<string | null>(listIdentityKey);
657657
let paintedExpandedRunIds = expandedRunIds;
658658
let paintedExpandedWorkGroupIds = expandedWorkGroupIds;
659659
let paintedExpandedAttemptIds = expandedAttemptIds;
@@ -1185,19 +1185,23 @@ const ConversationTimeline = memo(function ConversationTimeline({
11851185
const row = index === undefined ? undefined : state.elementAtIndex(index);
11861186
const element = listRef.current?.getScrollableNode();
11871187
if (row && element) {
1188-
rememberTimelinePosition(listIdentityKey, {
1189-
...position,
1190-
// DOM geometry includes the header and the virtualizer's layout adjustment.
1191-
offsetWithinRow: element.getBoundingClientRect().top - row.getBoundingClientRect().top,
1192-
scrollOffset: element.scrollTop,
1193-
atEnd: isAtEnd,
1194-
disclosures: {
1195-
runs: paintedExpandedRunIds,
1196-
workGroups: paintedExpandedWorkGroupIds,
1197-
attempts: paintedExpandedAttemptIds,
1198-
workGroupState: workGroupViewState,
1188+
rememberTimelinePosition(
1189+
listIdentityKey,
1190+
{
1191+
...position,
1192+
// DOM geometry includes the header and the virtualizer's layout adjustment.
1193+
offsetWithinRow: element.getBoundingClientRect().top - row.getBoundingClientRect().top,
1194+
scrollOffset: element.scrollTop,
1195+
atEnd: isAtEnd,
1196+
disclosures: {
1197+
runs: paintedExpandedRunIds,
1198+
workGroups: paintedExpandedWorkGroupIds,
1199+
attempts: paintedExpandedAttemptIds,
1200+
workGroupState: workGroupViewState,
1201+
},
11991202
},
1200-
});
1203+
liveFollowEnabled,
1204+
);
12011205
}
12021206
}
12031207
if (isAtEnd !== undefined && !citationPositioning) {
@@ -1249,6 +1253,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
12491253
workGroupViewState,
12501254
rows,
12511255
listIdentityKey,
1256+
liveFollowEnabled,
12521257
restoringThreadPosition,
12531258
listRef,
12541259
minimapItems,

‎apps/web/src/components/chat/timelineScrollAnchoring.test.tsx‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,37 @@ describe("remembered timeline positions", () => {
271271
it("keeps reading positions and end-follow independent across threads and environments", () => {
272272
const reading = { rowId: "message-4", offsetWithinRow: 32, scrollOffset: 932, atEnd: false };
273273
const following = { rowId: "message-9", offsetWithinRow: 10, scrollOffset: 2010, atEnd: true };
274-
rememberTimelinePosition("scroll-test-a:thread-1", reading);
275-
rememberTimelinePosition("scroll-test-a:thread-2", following);
276-
rememberTimelinePosition("scroll-test-b:thread-1", following);
274+
rememberTimelinePosition("scroll-test-a:thread-1", reading, false);
275+
rememberTimelinePosition("scroll-test-a:thread-2", following, true);
276+
rememberTimelinePosition("scroll-test-b:thread-1", following, true);
277277
expect(readTimelinePosition("scroll-test-a:thread-1")).toEqual(reading);
278278
expect(readTimelinePosition("scroll-test-a:thread-2")).toEqual(following);
279279
expect(readTimelinePosition("scroll-test-b:thread-1")).toEqual(following);
280280
expect(readTimelinePosition("scroll-test-a:unvisited")).toBeUndefined();
281-
rememberTimelinePosition("scroll-test-a:thread-1", following);
281+
rememberTimelinePosition("scroll-test-a:thread-1", following, true);
282282
expect(readTimelinePosition("scroll-test-a:thread-1")).toEqual(following);
283283
});
284284
});
285+
286+
describe("remembered timeline follow intent", () => {
287+
const key = "follow-intent-test:thread";
288+
const position = { rowId: "message-4", offsetWithinRow: 32, scrollOffset: 932, atEnd: false };
289+
290+
it("keeps following when smooth scrolling or a layout callback reports an end gap", () => {
291+
rememberTimelinePosition(key, { ...position, atEnd: true }, true);
292+
rememberTimelinePosition(key, position, true);
293+
expect(readTimelinePosition(key)).toEqual({ ...position, atEnd: true });
294+
});
295+
296+
it("restores the reading position after manual navigation opts out of follow", () => {
297+
rememberTimelinePosition(key, position, true);
298+
rememberTimelinePosition(key, position, false);
299+
expect(readTimelinePosition(key)).toEqual(position);
300+
});
301+
302+
it("remembers returning to the end before the follow flag updates", () => {
303+
rememberTimelinePosition(key, position, false);
304+
rememberTimelinePosition(key, { ...position, atEnd: true }, false);
305+
expect(readTimelinePosition(key)?.atEnd).toBe(true);
306+
});
307+
});

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,16 @@ export function readTimelinePosition(threadKey: string) {
165165
return rememberedTimelinePositions.get(threadKey);
166166
}
167167

168-
export function rememberTimelinePosition(threadKey: string, position: RememberedTimelinePosition) {
168+
export function rememberTimelinePosition(
169+
threadKey: string,
170+
position: RememberedTimelinePosition,
171+
liveFollowEnabled: boolean,
172+
) {
169173
rememberedTimelinePositions.delete(threadKey);
170-
rememberedTimelinePositions.set(threadKey, position);
174+
rememberedTimelinePositions.set(threadKey, {
175+
...position,
176+
atEnd: liveFollowEnabled || position.atEnd,
177+
});
171178
if (rememberedTimelinePositions.size > 100) {
172179
const oldest = rememberedTimelinePositions.keys().next().value;
173180
if (oldest !== undefined) rememberedTimelinePositions.delete(oldest);

0 commit comments

Comments
 (0)