Skip to content

Commit 48a3588

Browse files
t3dotggclaude
andcommitted
fix(web): a restore whose target disappears still reports the thread positioned
If the saved row or scroll node went away mid-settle, the restore never reported the thread positioned, so it stayed in its restoring state. Report positioned whenever the restore was not cancelled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b2c7d94 commit 48a3588

2 files changed

Lines changed: 35 additions & 39 deletions

File tree

‎apps/web/src/components/chat/useTimelinePositionRestore.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ describe("settleListScroll", () => {
1818
},
1919
};
2020

21-
const settled = settleListScroll(list, measuredEndOffset, () => false);
21+
let settled = false;
22+
void settleListScroll(list, measuredEndOffset, () => false).then(() => {
23+
settled = true;
24+
});
2225
await vi.advanceTimersByTimeAsync(5_000);
2326
vi.useRealTimers();
2427
vi.unstubAllGlobals();
2528

26-
expect(await Promise.race([settled, Promise.resolve("still settling")])).toBe(true);
29+
expect(settled).toBe(true);
2730
});
2831
});

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@ const SETTLE_MAX_FRAMES = 60;
1111
/**
1212
* Index and end scrolling start from estimated row sizes, so they can stop
1313
* short of a tall row. Corrects against the DOM until the viewport holds the
14-
* target for two layout frames, or the frame budget runs out; resolves false
15-
* when cancelled or the target disappears.
14+
* target for two layout frames, the frame budget runs out, the caller cancels,
15+
* or the target disappears.
1616
*/
1717
export function settleListScroll(
1818
list: Pick<LegendListRef, "getScrollableNode" | "scrollToOffset">,
1919
target: (element: HTMLElement) => number | undefined,
2020
isCancelled: () => boolean,
21-
): Promise<boolean> {
21+
): Promise<void> {
2222
return new Promise((resolve) => {
2323
let stableFrames = 0;
2424
let frames = 0;
2525
const reconcile = () => {
26-
if (isCancelled()) return resolve(false);
26+
if (isCancelled()) return resolve();
2727
const element = list.getScrollableNode();
2828
const offset = element ? target(element) : undefined;
29-
if (!element || offset === undefined) return resolve(false);
3029
frames += 1;
31-
if (frames > SETTLE_MAX_FRAMES) return resolve(true);
30+
if (!element || offset === undefined || frames > SETTLE_MAX_FRAMES) return resolve();
3231
if (Math.abs(element.scrollTop - offset) > 1) {
3332
stableFrames = 0;
3433
void list.scrollToOffset({ offset, animated: false }).then(() => {
@@ -37,7 +36,7 @@ export function settleListScroll(
3736
return;
3837
}
3938
stableFrames += 1;
40-
if (stableFrames >= 2) resolve(true);
39+
if (stableFrames >= 2) resolve();
4140
else requestAnimationFrame(reconcile);
4241
};
4342
requestAnimationFrame(reconcile);
@@ -144,37 +143,31 @@ export function useTimelinePositionRestore({
144143
void Promise.resolve(scrolling).then(async () => {
145144
if (cancelled) return;
146145
if (settlesAtEnd) {
147-
if (await settleListScroll(list, measuredEndOffset, () => cancelled)) {
148-
onPositioned(listIdentityKey);
149-
}
150-
return;
151-
}
152-
if (position?.atEnd !== false || index < 0) {
153-
onPositioned(listIdentityKey);
154-
return;
146+
await settleListScroll(list, measuredEndOffset, () => cancelled);
147+
} else if (position?.atEnd === false && index >= 0) {
148+
// Keep the saved row mounted until its measured position and the DOM agree.
149+
await settleListScroll(
150+
list,
151+
(element) => {
152+
const state = list.getState();
153+
const rowIndex = state.indexByKey(position.rowId);
154+
const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);
155+
if (!row) return undefined;
156+
return Math.max(
157+
0,
158+
Math.min(
159+
element.scrollTop +
160+
row.getBoundingClientRect().top -
161+
element.getBoundingClientRect().top +
162+
position.offsetWithinRow,
163+
measuredEndOffset(element),
164+
),
165+
);
166+
},
167+
() => cancelled,
168+
);
155169
}
156-
// Keep the saved row mounted until its measured position and the DOM agree.
157-
const settled = await settleListScroll(
158-
list,
159-
(element) => {
160-
const state = list.getState();
161-
const rowIndex = state.indexByKey(position.rowId);
162-
const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);
163-
if (!row) return undefined;
164-
return Math.max(
165-
0,
166-
Math.min(
167-
element.scrollTop +
168-
row.getBoundingClientRect().top -
169-
element.getBoundingClientRect().top +
170-
position.offsetWithinRow,
171-
measuredEndOffset(element),
172-
),
173-
);
174-
},
175-
() => cancelled,
176-
);
177-
if (settled) onPositioned(listIdentityKey);
170+
if (!cancelled) onPositioned(listIdentityKey);
178171
});
179172
return () => {
180173
cancelled = true;

0 commit comments

Comments
 (0)