Skip to content

Commit 37275a9

Browse files
fix(web): refresh minimap visibility within the rendered buffer
1 parent 084e4f8 commit 37275a9

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,39 @@ export function resolveTimelineMinimapIndexFromPointer(input: {
339339
return Math.max(0, Math.min(input.itemCount - 1, Math.round(progress * (input.itemCount - 1))));
340340
}
341341

342+
/** Recheck the rendered buffer: LegendList's unbuffered range can lag during scrolling. */
343+
export function resolveTimelineMinimapVisibleRange(state: {
344+
readonly startBuffered: number | null | undefined;
345+
readonly endBuffered: number | null | undefined;
346+
readonly scroll: number;
347+
readonly scrollLength: number;
348+
readonly positionAtIndex: (index: number) => number | undefined;
349+
readonly sizeAtIndex: (index: number) => number | undefined;
350+
}) {
351+
let start: number | null = null;
352+
let end: number | null = null;
353+
if (state.startBuffered == null || state.endBuffered == null || state.startBuffered < 0) {
354+
return { start, end };
355+
}
356+
const bottom = state.scroll + state.scrollLength;
357+
for (let index = state.startBuffered; index <= state.endBuffered; index += 1) {
358+
const top = state.positionAtIndex(index);
359+
const height = state.sizeAtIndex(index);
360+
if (
361+
top != null &&
362+
Number.isFinite(top) &&
363+
height != null &&
364+
Number.isFinite(height) &&
365+
top < bottom &&
366+
top + height > state.scroll
367+
) {
368+
start ??= index;
369+
end = index;
370+
}
371+
}
372+
return { start, end };
373+
}
374+
342375
export function isTimelineMinimapRowVisible(
343376
rowIndex: number,
344377
visibleStart: number | null | undefined,

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ import {
198198
resolveTimelineMinimapHasPersistentGutter,
199199
resolveTimelineMinimapCurrentIndex,
200200
isTimelineMinimapRowVisible,
201+
resolveTimelineMinimapVisibleRange,
201202
resolveTimelineMinimapHeightStyle,
202203
resolveTimelineMinimapHitStripWidth,
203204
resolveTimelineMinimapIndexFromPointer,
@@ -1060,18 +1061,23 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10601061
return;
10611062
}
10621063

1064+
const visibleRange = resolveTimelineMinimapVisibleRange(state);
10631065
for (const item of minimapItems) {
10641066
const strip = minimapStripMap.get(item.id);
10651067
// Offscreen position caches can lag behind row size changes.
1066-
const inView = isTimelineMinimapRowVisible(item.rowIndex, state.start, state.end);
1068+
const inView = isTimelineMinimapRowVisible(
1069+
item.rowIndex,
1070+
visibleRange.start,
1071+
visibleRange.end,
1072+
);
10671073
const next = inView ? "true" : "false";
10681074
if (strip && strip.dataset.inView !== next) {
10691075
strip.dataset.inView = next;
10701076
}
10711077
}
10721078
const nextCurrentIndex = resolveTimelineMinimapCurrentIndex({
1073-
visibleStart: state.start,
1074-
visibleEnd: state.end,
1079+
visibleStart: visibleRange.start,
1080+
visibleEnd: visibleRange.end,
10751081
rowIndices: minimapItems.map((item) => item.rowIndex),
10761082
});
10771083
setMinimapCurrentIndex((current) =>

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { it, expect } from "vite-plus/test";
22
import {
33
isTimelineMinimapRowVisible,
4+
resolveTimelineMinimapVisibleRange,
45
resolveTimelineMinimapCurrentIndex,
56
} from "./MessagesTimeline.logic";
67

@@ -28,3 +29,30 @@ it("resolves minimap navigation from the visible rows", () => {
2829
resolveTimelineMinimapCurrentIndex({ visibleStart: 0, visibleEnd: 5, rowIndices: [] }),
2930
).toBeNull();
3031
});
32+
33+
it("refreshes visibility while scrolling within the same rendered buffer", () => {
34+
const state = {
35+
startBuffered: 0,
36+
endBuffered: 4,
37+
scroll: 200,
38+
scrollLength: 150,
39+
positionAtIndex: (index: number) => index * 100,
40+
sizeAtIndex: () => 100,
41+
};
42+
expect(resolveTimelineMinimapVisibleRange(state)).toEqual({ start: 2, end: 3 });
43+
expect(resolveTimelineMinimapVisibleRange({ ...state, scroll: 0 })).toEqual({ start: 0, end: 1 });
44+
});
45+
46+
it("ignores stale offscreen positions outside the rendered buffer", () => {
47+
const range = resolveTimelineMinimapVisibleRange({
48+
startBuffered: 10,
49+
endBuffered: 14,
50+
scroll: 1000,
51+
scrollLength: 200,
52+
// Earlier rows have stale coordinates that overlap the viewport.
53+
positionAtIndex: (index) => (index < 10 ? 1000 : index * 100),
54+
sizeAtIndex: () => 100,
55+
});
56+
expect(range).toEqual({ start: 10, end: 11 });
57+
expect(isTimelineMinimapRowVisible(0, range.start, range.end)).toBe(false);
58+
});

0 commit comments

Comments
 (0)