Skip to content

Commit 0e54fbf

Browse files
committed
fix(web): distinguish native timeline position corrections
1 parent 1e7a638 commit 0e54fbf

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6632,6 +6632,7 @@ export default function ChatView(props: ChatViewProps) {
66326632
scrollNode,
66336633
() => timelineScrollModeRef.current === "following-end",
66346634
handleManualNavigation,
6635+
() => legendListRef.current?.getState().scroll,
66356636
);
66366637
document.addEventListener("selectionchange", handleSelectionChange);
66376638
scrollNode.addEventListener("focusin", handleFocusIn);

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,18 +2906,26 @@ describe("thread entry with the real virtualizer", () => {
29062906
// jsdom supplies no layout. Give the real virtualizer a 600px viewport
29072907
// and 180px message rows, twice its 90px estimate.
29082908
let lastRowHeight = 180;
2909+
const rowHeights = new Map<number, number>();
29092910
let pendingScrollHeight: number | undefined;
29102911
let pendingViewportHeight: number | undefined;
29112912
const rectSpy = vi
29122913
.spyOn(HTMLElement.prototype, "getBoundingClientRect")
29132914
.mockImplementation(function (this: HTMLElement) {
2915+
const rowIndex = Number(
2916+
this.querySelector("[data-message-id]")
2917+
?.getAttribute("data-message-id")
2918+
?.match(/^message-(\d+)$/)?.[1],
2919+
);
29142920
const height = this.classList.contains("messages-timeline-scroll")
29152921
? 600
29162922
: this.firstElementChild?.classList.contains("messages-timeline-row-frame")
2917-
? this.textContent?.includes("Message 39") ||
2918-
this.textContent?.includes("Thread B message 39")
2919-
? lastRowHeight
2920-
: 180
2923+
? rowHeights.has(rowIndex)
2924+
? rowHeights.get(rowIndex)!
2925+
: this.textContent?.includes("Message 39") ||
2926+
this.textContent?.includes("Thread B message 39")
2927+
? lastRowHeight
2928+
: 180
29212929
: this.style.height.endsWith("px")
29222930
? Number.parseFloat(this.style.height)
29232931
: 0;
@@ -3130,6 +3138,7 @@ describe("thread entry with the real virtualizer", () => {
31303138
cancelTimelineProgrammaticScroll(props.listRef.current);
31313139
setFollow(false);
31323140
},
3141+
() => props.listRef.current?.getState().scroll,
31333142
),
31343143
[],
31353144
);
@@ -3162,6 +3171,33 @@ describe("thread entry with the real virtualizer", () => {
31623171
readingViewport.scrollHeight - readingViewport.clientHeight - readingViewport.scrollTop,
31633172
),
31643173
).toBeLessThanOrEqual(1);
3174+
if (geometryChange === "none") {
3175+
// Native visible-position corrections must not be mistaken for navigation.
3176+
const aboveRow = Math.min(
3177+
...Array.from(readingViewport.querySelectorAll("[data-message-id]"))
3178+
.map((row) =>
3179+
Number(row.getAttribute("data-message-id")?.match(/^message-(\d+)$/)?.[1]),
3180+
)
3181+
.filter(Number.isFinite),
3182+
);
3183+
expect(aboveRow).toBeLessThan(39);
3184+
for (const tailGrowth of [60, 0, 240]) {
3185+
rowHeights.set(aboveRow, 60);
3186+
lastRowHeight = 180 + tailGrowth;
3187+
await flushLayout();
3188+
expect(unsignaledNavigation).not.toHaveBeenCalled();
3189+
expect(
3190+
Math.abs(
3191+
readingViewport.scrollHeight -
3192+
readingViewport.clientHeight -
3193+
readingViewport.scrollTop,
3194+
),
3195+
).toBeLessThanOrEqual(1);
3196+
rowHeights.clear();
3197+
lastRowHeight = 180;
3198+
await flushLayout();
3199+
}
3200+
}
31653201
await act(async () => {
31663202
// A shrink and external navigation can coalesce into one scroll event.
31673203
// The browser first clamps the old offset, then the reader moves farther up.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export function observeTimelineScrollNavigation(
190190
node: HTMLElement,
191191
isFollowingEnd: () => boolean,
192192
onManualNavigation: () => void,
193+
getManagedOffset: () => number | undefined,
193194
): () => void {
194195
let previousOffset = node.scrollTop;
195196
const handleScroll = () => {
@@ -200,7 +201,13 @@ export function observeTimelineScrollNavigation(
200201
const clampedPreviousOffset = Math.min(previousOffset, Math.max(0, height - viewport));
201202
const movedAway = offset < clampedPreviousOffset - 1 && height - viewport - offset > 40;
202203
previousOffset = offset;
203-
if (movedAway && isFollowingEnd()) onManualNavigation();
204+
if (movedAway && isFollowingEnd()) {
205+
// Legend records its own position correction before applying the DOM scroll.
206+
const managedOffset = getManagedOffset();
207+
if (managedOffset === undefined || Math.abs(offset - managedOffset) > 1) {
208+
onManualNavigation();
209+
}
210+
}
204211
};
205212
// Observe navigation before the virtualizer handles the event and schedules follow.
206213
node.addEventListener("scroll", handleScroll, { capture: true, passive: true });

0 commit comments

Comments
 (0)