Skip to content

Commit 83a7ea4

Browse files
fix(web): read the live-follow latch when remembering a thread position
A navigation gesture releases ChatView's live-follow latch synchronously, but liveFollowEnabled only changes on the next render. A scroll event in between could still remember the thread as at the end. Read the latch directly so a switch in that window restores the reading position. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b61dd88 commit 83a7ea4

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5342,6 +5342,10 @@ export default function ChatView(props: ChatViewProps) {
53425342
settledTimelineAnchorRef.current = null;
53435343
activeTimelineAnchorIndexRef.current = null;
53445344
}, []);
5345+
const isTimelineLiveFollowLatched = useCallback(
5346+
() => liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current,
5347+
[],
5348+
);
53455349
const cancelTimelineLiveFollowForUserNavigationRef = useRef(
53465350
cancelTimelineLiveFollowForUserNavigation,
53475351
);
@@ -9994,6 +9998,7 @@ export default function ChatView(props: ChatViewProps) {
99949998
onAnchorReady={onTimelineAnchorReady}
99959999
contentInsetEndAdjustment={composerTimelineInset}
999610000
liveFollowEnabled={!paintOnlyDisplayedTimeline && timelineLiveFollowEnabled}
10001+
isLiveFollowLatched={isTimelineLiveFollowLatched}
999710002
onIsAtEndChange={onIsAtEndChange}
999810003
onContentOverflowChange={setTimelineOverflows}
999910004
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEnd}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ function buildProps() {
223223
onAnchorReady: () => {},
224224
contentInsetEndAdjustment: 0,
225225
liveFollowEnabled: true,
226+
isLiveFollowLatched: () => true,
226227
onIsAtEndChange: () => {},
227228
onManualNavigation: () => {},
228229
};
@@ -1027,7 +1028,8 @@ describe("MessagesTimeline", () => {
10271028
];
10281029
let renderer!: ReactTestRenderer;
10291030
const list = () => renderer.root.findByType(LegendList);
1030-
const props = buildProps();
1031+
let latched = true;
1032+
const props = { ...buildProps(), isLiveFollowLatched: () => latched };
10311033
// Streamed output has grown the list 500px past the viewport, and the
10321034
// follow scroll has not caught up yet.
10331035
props.listRef.current = {
@@ -1065,6 +1067,14 @@ describe("MessagesTimeline", () => {
10651067
act(() => list().props.onScroll());
10661068
expect(readTimelinePosition(threadKey)?.atEnd).toBe(true);
10671069

1070+
// A gesture releases the latch before the next render turns follow off.
1071+
latched = false;
1072+
act(() => list().props.onScroll());
1073+
expect(readTimelinePosition(threadKey)).toMatchObject({ atEnd: false, scrollOffset: 700 });
1074+
latched = true;
1075+
act(() => list().props.onScroll());
1076+
expect(readTimelinePosition(threadKey)?.atEnd).toBe(true);
1077+
10681078
// Once the user scrolls away, the same offset is a reading position.
10691079
act(() => renderer.update(renderTimeline(false)));
10701080
act(() => list().props.onScroll());

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ interface MessagesTimelineProps {
447447
* scroll-mode refs whenever the user drifts near the bottom.
448448
*/
449449
liveFollowEnabled: boolean;
450+
/**
451+
* Reads ChatView's live-follow latch. A navigation gesture releases it
452+
* synchronously, before `liveFollowEnabled` re-renders.
453+
*/
454+
isLiveFollowLatched: () => boolean;
450455
onIsAtEndChange: (isAtEnd: boolean) => void;
451456
/**
452457
* Whether the real rows extend past the viewport above the composer.
@@ -511,6 +516,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
511516
onAnchorReady,
512517
contentInsetEndAdjustment,
513518
liveFollowEnabled,
519+
isLiveFollowLatched,
514520
onIsAtEndChange,
515521
onContentOverflowChange,
516522
onToolOutputCollapsedAtEnd,
@@ -1032,8 +1038,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10321038
// While live-following, a gap to the end is the follow scroll
10331039
// trailing streamed output, not a reading position to restore.
10341040
// Anchored end space holds the first send near the top instead of
1035-
// following, so its gap is a real position.
1036-
atEnd: isAtEnd || (liveFollowEnabled && !anchoredEndSpace),
1041+
// following, so its gap is a real position. The latch covers a
1042+
// gesture that released follow before this render caught up.
1043+
atEnd: isAtEnd || (liveFollowEnabled && isLiveFollowLatched() && !anchoredEndSpace),
10371044
disclosures: {
10381045
turns: paintedExpandedTurnIds,
10391046
workGroups: paintedExpandedWorkGroupIds,
@@ -1092,6 +1099,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10921099
rows,
10931100
listIdentityKey,
10941101
liveFollowEnabled,
1102+
isLiveFollowLatched,
10951103
anchoredEndSpace,
10961104
restoringThreadPosition,
10971105
listRef,

0 commit comments

Comments
 (0)