Skip to content

Commit 7d4fa73

Browse files
t3dotggclaude
andcommitted
fix(web): drop stale overflow frames and treat an unmeasured viewport as fitting
Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 2f3b8b5 commit 7d4fa73

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,26 +623,29 @@ export const MessagesTimeline = memo(function MessagesTimeline({
623623
// sees the settled positions. One frame is shared across bursts of size
624624
// changes.
625625
const contentOverflowFrameRef = useRef<number | null>(null);
626+
const cancelContentOverflowFrame = useCallback(() => {
627+
if (contentOverflowFrameRef.current !== null) {
628+
cancelAnimationFrame(contentOverflowFrameRef.current);
629+
contentOverflowFrameRef.current = null;
630+
}
631+
}, []);
626632
const reportContentOverflow = useCallback(() => {
627633
if (!onContentOverflowChange || contentOverflowFrameRef.current !== null) return;
628634
contentOverflowFrameRef.current = requestAnimationFrame(() => {
629635
contentOverflowFrameRef.current = null;
630636
onContentOverflowChange(measureContentOverflow());
631637
});
632638
}, [measureContentOverflow, onContentOverflowChange]);
633-
useEffect(() => {
634-
return () => {
635-
if (contentOverflowFrameRef.current !== null) {
636-
cancelAnimationFrame(contentOverflowFrameRef.current);
637-
}
638-
};
639-
}, []);
639+
useEffect(() => cancelContentOverflowFrame, [cancelContentOverflowFrame]);
640640
// The list's own layout effects have already run here, so estimated row
641641
// positions are in place. Reporting before the first paint lets a thread
642642
// open in its final composer layout instead of correcting it a frame later.
643+
// A frame scheduled with the previous inset would overwrite this read, so
644+
// it is dropped first.
643645
useLayoutEffect(() => {
646+
cancelContentOverflowFrame();
644647
onContentOverflowChange?.(measureContentOverflow());
645-
}, [measureContentOverflow, onContentOverflowChange, rows.length]);
648+
}, [cancelContentOverflowFrame, measureContentOverflow, onContentOverflowChange, rows.length]);
646649

647650
const handleScroll = useCallback(() => {
648651
const state = listRef.current?.getState?.();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ describe("timelineContentOverflowsViewport", () => {
3838

3939
it("treats an empty or unmeasured list as fitting", () => {
4040
expect(timelineContentOverflowsViewport(undefined, inset)).toBe(false);
41+
expect(
42+
timelineContentOverflowsViewport(
43+
buildState({ positions: [0, 200], sizes: [200, 400], scrollLength: 0 }),
44+
inset,
45+
),
46+
).toBe(false);
4147
expect(timelineContentOverflowsViewport(buildState({ positions: [], sizes: [] }), inset)).toBe(
4248
false,
4349
);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function getRowBottom(state: TimelineListMeasurementState, index: number)
4141
* Whether the timeline's real rows extend past the viewport left above the
4242
* composer. The list's own content length includes the composer inset
4343
* spacer, so this measures from the last row instead. Unknown row geometry
44-
* counts as fitting.
44+
* or an unmeasured viewport counts as fitting.
4545
*/
4646
export function timelineContentOverflowsViewport(
4747
state: TimelineListMeasurementState | undefined,
@@ -50,14 +50,15 @@ export function timelineContentOverflowsViewport(
5050
if (!state || !state.data || state.data.length === 0) {
5151
return false;
5252
}
53+
const scrollLength = state.scrollLength;
54+
if (typeof scrollLength !== "number" || !Number.isFinite(scrollLength) || scrollLength <= 0) {
55+
return false;
56+
}
5357
const lastBottom = getRowBottom(state, state.data.length - 1);
5458
if (lastBottom === null) {
5559
return false;
5660
}
57-
const visibleScrollLength = Math.max(
58-
0,
59-
(state.scrollLength ?? 0) - input.composerInset - input.anchorOffset,
60-
);
61+
const visibleScrollLength = Math.max(0, scrollLength - input.composerInset - input.anchorOffset);
6162
return lastBottom > visibleScrollLength;
6263
}
6364

0 commit comments

Comments
 (0)