Skip to content

Commit 0b4d5fe

Browse files
committed
fix(web): keep a manual reading position when leaving before the next frame
1 parent 2ec3d96 commit 0b4d5fe

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
act,
1414
createRef,
1515
useLayoutEffect,
16+
useState,
1617
type ReactNode,
1718
type Ref,
1819
type ReactElement,
@@ -87,6 +88,11 @@ beforeEach(() => {
8788
activityTestState.expandedRuns = false;
8889
});
8990

91+
const legendListTestState = vi.hoisted(() => ({
92+
data: undefined as unknown,
93+
onScroll: undefined as (() => void) | undefined,
94+
}));
95+
9096
vi.mock("@legendapp/list/react", async () => {
9197
const legendListTestId = "legend-list";
9298

@@ -122,8 +128,11 @@ vi.mock("@legendapp/list/react", async () => {
122128
};
123129
className?: string;
124130
contentInsetEndAdjustment?: number;
131+
onScroll?: () => void;
125132
ref?: Ref<LegendListRef>;
126133
}) => {
134+
legendListTestState.data = props.data;
135+
legendListTestState.onScroll = props.onScroll;
127136
if (props.anchoredEndSpace) {
128137
props.anchoredEndSpace.onSizeChanged?.(240);
129138
props.anchoredEndSpace.onReady?.({ anchorIndex: props.anchoredEndSpace.anchorIndex });
@@ -669,6 +678,82 @@ describe("MessagesTimeline", () => {
669678
},
670679
);
671680

681+
it("remembers a manual reading position when the chat closes before the next frame", async () => {
682+
const frames = new Map<number, FrameRequestCallback>();
683+
let nextFrame = 0;
684+
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
685+
frames.set(++nextFrame, callback);
686+
return nextFrame;
687+
});
688+
vi.stubGlobal("cancelAnimationFrame", (frame: number) => frames.delete(frame));
689+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
690+
const flushFrame = () =>
691+
act(() => {
692+
const callbacks = [...frames.values()];
693+
frames.clear();
694+
callbacks.forEach((callback) => callback(0));
695+
});
696+
const { readTimelinePosition } = await import("./timelineScrollAnchoring");
697+
const props = { ...buildProps(), routeThreadKey: "environment-local:manual-departure" };
698+
let scrollTop = 1400;
699+
const viewport = {
700+
get scrollTop() {
701+
return scrollTop;
702+
},
703+
getBoundingClientRect: () => ({ top: 0 }),
704+
};
705+
props.listRef.current = {
706+
getScrollableNode: () => viewport,
707+
getState: () => ({
708+
data: legendListTestState.data,
709+
scroll: scrollTop,
710+
scrollLength: 600,
711+
contentLength: 2000,
712+
indexByKey: () => 0,
713+
positionAtIndex: () => 0,
714+
sizeAtIndex: () => 2000,
715+
elementAtIndex: () => ({ getBoundingClientRect: () => ({ top: -scrollTop }) }),
716+
}),
717+
} as unknown as LegendListRef;
718+
let navigateManually = () => {};
719+
function ChatProbe() {
720+
const [liveFollowEnabled, setLiveFollowEnabled] = useState(true);
721+
navigateManually = () => setLiveFollowEnabled(false);
722+
return (
723+
<MessagesTimeline
724+
{...props}
725+
timelineEntries={[buildUserTimelineEntry("First prompt")]}
726+
liveFollowEnabled={liveFollowEnabled}
727+
onManualNavigation={navigateManually}
728+
/>
729+
);
730+
}
731+
let renderer: ReactTestRenderer | undefined;
732+
try {
733+
await act(() => {
734+
renderer = create(<ChatProbe />);
735+
});
736+
await flushFrame();
737+
expect(readTimelinePosition(props.routeThreadKey)?.atEnd).toBe(true);
738+
739+
// The scroll event lands before the follow opt-out renders.
740+
scrollTop = 1300;
741+
await act(() => {
742+
navigateManually();
743+
legendListTestState.onScroll?.();
744+
});
745+
await act(() => renderer?.unmount());
746+
renderer = undefined;
747+
748+
expect(readTimelinePosition(props.routeThreadKey)).toMatchObject({
749+
atEnd: false,
750+
scrollOffset: 1300,
751+
});
752+
} finally {
753+
await act(() => renderer?.unmount());
754+
}
755+
});
756+
672757
it("preserves arbitrary XML-like tags and comparisons in rendered user messages", async () => {
673758
const { MessagesTimeline } = await import("./MessagesTimeline");
674759
const markup = renderToStaticMarkup(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,16 @@ const ConversationTimeline = memo(function ConversationTimeline({
12671267
return () => cancelAnimationFrame(frame);
12681268
}, [handleScroll, rows.length]);
12691269

1270+
// A scroll saved while follow was still on marks the position as at the end.
1271+
// Re-save in the commit that turns follow off, since leaving the chat cancels
1272+
// the frame above.
1273+
const savedLiveFollowEnabledRef = useRef(liveFollowEnabled);
1274+
useLayoutEffect(() => {
1275+
const wasFollowing = savedLiveFollowEnabledRef.current;
1276+
savedLiveFollowEnabledRef.current = liveFollowEnabled;
1277+
if (wasFollowing && !liveFollowEnabled) handleScroll();
1278+
}, [handleScroll, liveFollowEnabled]);
1279+
12701280
useEffect(() => {
12711281
if (!timelineViewportElement) {
12721282
return;

0 commit comments

Comments
 (0)