Skip to content

Commit 134b719

Browse files
feat(web): add previous/next turn navigation in minimap (#8531)
1 parent 7d62050 commit 134b719

3 files changed

Lines changed: 270 additions & 111 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,34 @@ export function resolveTimelineMinimapIndexFromPointer(input: {
197197
return Math.max(0, Math.min(input.itemCount - 1, Math.round(progress * (input.itemCount - 1))));
198198
}
199199

200+
export function resolveTimelineMinimapCurrentIndex(input: {
201+
readonly scrollTop: number;
202+
readonly scrollBottom: number;
203+
readonly itemBounds: ReadonlyArray<{
204+
readonly top: number | null;
205+
readonly height: number | null;
206+
}>;
207+
}): number | null {
208+
let precedingIndex: number | null = null;
209+
210+
for (const [index, item] of input.itemBounds.entries()) {
211+
if (item.top === null) {
212+
continue;
213+
}
214+
const inView =
215+
item.top < input.scrollBottom && item.top + Math.max(1, item.height ?? 1) > input.scrollTop;
216+
if (inView) {
217+
// The first visible marker is the turn at the reader's current position.
218+
return index;
219+
}
220+
if (item.top <= input.scrollTop) {
221+
precedingIndex = index;
222+
}
223+
}
224+
225+
return precedingIndex;
226+
}
227+
200228
export function resolveTimelineMinimapHasPersistentGutter(viewportWidth: number): boolean {
201229
if (!Number.isFinite(viewportWidth) || viewportWidth <= 0) {
202230
return false;

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ function buildSnapShotTimelineEntry(previewUrl?: string) {
272272
}
273273

274274
describe("MessagesTimeline", () => {
275+
it("renders previous and next controls with the minimap", () => {
276+
const first = buildUserTimelineEntry("First turn");
277+
const secondBase = buildUserTimelineEntry("Second turn");
278+
const second = {
279+
...secondBase,
280+
id: "entry-2",
281+
message: {
282+
...secondBase.message,
283+
id: MessageId.make("message-2"),
284+
},
285+
};
286+
const markup = renderToStaticMarkup(
287+
<MessagesTimeline {...buildProps()} timelineEntries={[first, second]} />,
288+
);
289+
290+
expect(markup).toContain('aria-label="Previous turn"');
291+
expect(markup).toContain('aria-label="Next turn"');
292+
});
293+
275294
// Expanding history uses this suite's existing test renderer, deprecated in
276295
// React 19. Migrate these interaction tests together when a DOM test setup is added.
277296
it.each([{}, { text: "Text-only answer", file: "Answer with a file" }])(
@@ -524,6 +543,7 @@ describe("MessagesTimeline", () => {
524543
const {
525544
resolveTimelineIsAtEnd,
526545
resolveTimelineMinimapHasPersistentGutter,
546+
resolveTimelineMinimapCurrentIndex,
527547
resolveTimelineMinimapHeightStyle,
528548
resolveTimelineMinimapHitStripWidth,
529549
resolveTimelineMinimapIndexFromPointer,
@@ -583,6 +603,35 @@ describe("MessagesTimeline", () => {
583603
pointerY: 999,
584604
}),
585605
).toBe(100);
606+
expect(
607+
resolveTimelineMinimapCurrentIndex({
608+
scrollTop: 100,
609+
scrollBottom: 500,
610+
itemBounds: [
611+
{ top: 80, height: 20 },
612+
{ top: 120, height: 20 },
613+
{ top: 220, height: 20 },
614+
],
615+
}),
616+
).toBe(1);
617+
expect(
618+
resolveTimelineMinimapCurrentIndex({
619+
scrollTop: 150,
620+
scrollBottom: 200,
621+
itemBounds: [
622+
{ top: 80, height: 20 },
623+
{ top: 120, height: 20 },
624+
{ top: 220, height: 20 },
625+
],
626+
}),
627+
).toBe(1);
628+
expect(
629+
resolveTimelineMinimapCurrentIndex({
630+
scrollTop: 0,
631+
scrollBottom: 50,
632+
itemBounds: [{ top: 80, height: 20 }],
633+
}),
634+
).toBeNull();
586635
expect(resolveTimelineMinimapHasPersistentGutter(832)).toBe(false);
587636
expect(resolveTimelineMinimapHasPersistentGutter(863)).toBe(false);
588637
expect(resolveTimelineMinimapHasPersistentGutter(864)).toBe(true);

0 commit comments

Comments
 (0)