Skip to content

Commit f17165a

Browse files
authored
fix(web): preserve thread reading positions (#12144)
Co-authored-by: maria-rcks <[email protected]>
1 parent 71d12d8 commit f17165a

4 files changed

Lines changed: 265 additions & 38 deletions

File tree

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ import { type LegendListRef } from "@legendapp/list/react";
136136
import {
137137
CHAT_TIMELINE_ANCHOR_OFFSET,
138138
getAnchoredTurnMetrics,
139+
readTimelinePosition,
139140
timelineContentOverflowsViewport,
140141
type TimelineScrollMode,
141142
} from "./chat/timelineScrollAnchoring";
@@ -5260,10 +5261,12 @@ export default function ChatView(props: ChatViewProps) {
52605261
const settledTimelineAnchorRef = useRef<MessageId | null>(null);
52615262
const activeTimelineAnchorIndexRef = useRef<number | null>(null);
52625263
const anchorUserScrollGenerationRef = useRef(0);
5264+
const cancelPositionRestoreRef = useRef<(() => void) | null>(null);
52635265
const liveFollowUserScrollGenerationRef = useRef<number | null>(0);
52645266
// Manual navigation stops live-follow without removing anchored end space.
52655267
// Collapsing that space during a gesture clamps the viewport back to the end.
52665268
const cancelTimelineLiveFollowForUserNavigation = useCallback(() => {
5269+
cancelPositionRestoreRef.current?.();
52675270
anchorUserScrollGenerationRef.current += 1;
52685271
timelineScrollModeRef.current = "free-scrolling";
52695272
liveFollowUserScrollGenerationRef.current = null;
@@ -5343,6 +5346,7 @@ export default function ChatView(props: ChatViewProps) {
53435346
// Live-follow stays active after send/thread-open until an actual list scroll
53445347
// gesture opts out.
53455348
const scrollToEnd = useCallback((animated = false) => {
5349+
cancelPositionRestoreRef.current?.();
53465350
isAtEndRef.current = true;
53475351
timelineScrollModeRef.current = "following-end";
53485352
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
@@ -5358,21 +5362,6 @@ export default function ChatView(props: ChatViewProps) {
53585362
void legendListRef.current?.scrollToEnd?.({ animated });
53595363
});
53605364
}, []);
5361-
const displayedTimelineKeyRef = useRef(displayedTimeline.displayThreadKey);
5362-
useLayoutEffect(() => {
5363-
const displayKey = displayedTimeline.displayThreadKey;
5364-
if (displayKey === null || displayKey !== activeThreadKey) {
5365-
displayedTimelineKeyRef.current = displayKey;
5366-
return;
5367-
}
5368-
if (displayedTimelineKeyRef.current === displayKey) {
5369-
return;
5370-
}
5371-
displayedTimelineKeyRef.current = displayKey;
5372-
// Keep the list mounted across jumps; pin the newly displayed thread to
5373-
// its end the way a remount used to via initialScrollAtEnd.
5374-
scrollToEnd();
5375-
}, [activeThreadKey, displayedTimeline.displayThreadKey, scrollToEnd]);
53765365
useLayoutEffect(() => {
53775366
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
53785367
return;
@@ -5682,19 +5671,22 @@ export default function ChatView(props: ChatViewProps) {
56825671

56835672
useEffect(() => {
56845673
setPullRequestDialogState(null);
5685-
isAtEndRef.current = true;
5674+
const followEnd = readTimelinePosition(routeThreadKey)?.atEnd !== false;
5675+
isAtEndRef.current = followEnd;
56865676
timelineScrollIntentRef.current = null;
5687-
timelineScrollModeRef.current = "following-end";
5688-
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
5689-
setTimelineLiveFollowEnabled(true);
5677+
timelineScrollModeRef.current = followEnd ? "following-end" : "free-scrolling";
5678+
liveFollowUserScrollGenerationRef.current = followEnd
5679+
? anchorUserScrollGenerationRef.current
5680+
: null;
5681+
setTimelineLiveFollowEnabled(followEnd);
56905682
pendingTimelineAnchorRef.current = null;
56915683
positionedTimelineAnchorRef.current = null;
56925684
settledTimelineAnchorRef.current = null;
56935685
activeTimelineAnchorIndexRef.current = null;
56945686
showScrollDebouncer.current.cancel();
5695-
setShowScrollToBottom(false);
5687+
setShowScrollToBottom(!followEnd);
56965688
// activeThreadRef resets transitively with the active thread.
5697-
}, [activeThread?.id]);
5689+
}, [activeThread?.id, routeThreadKey]);
56985690

56995691
useEffect(() => {
57005692
if (!activeThread?.id || terminalUiState.terminalOpen) return;
@@ -9551,6 +9543,7 @@ export default function ChatView(props: ChatViewProps) {
95519543
onContentOverflowChange={setTimelineOverflows}
95529544
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEnd}
95539545
onManualNavigation={cancelTimelineLiveFollowForUserNavigation}
9546+
cancelPositionRestoreRef={cancelPositionRestoreRef}
95549547
hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}
95559548
topFadeEnabled={!hasTimelineTopBanner}
95569549
loadEarlier={paintOnlyDisplayedTimeline ? null : loadEarlierTurns}

0 commit comments

Comments
 (0)