feat(web): add previous/next turn navigation in minimap (#8531)

This commit is contained in:
Utkarsh Patil authored and GitHub committed 2026-09-08 14:26:42 +05:30
1 parent 7d620506a1
commit 134b7194b1
3 files changed
+270 -111

No files matched your search

@@ -197,6 +197,34 @@ export function resolveTimelineMinimapIndexFromPointer(input: {
return Math.max(0, Math.min(input.itemCount - 1, Math.round(progress * (input.itemCount - 1))));
}
export function resolveTimelineMinimapCurrentIndex(input: {
readonly scrollTop: number;
readonly scrollBottom: number;
readonly itemBounds: ReadonlyArray<{
readonly top: number | null;
readonly height: number | null;
}>;
}): number | null {
let precedingIndex: number | null = null;
for (const [index, item] of input.itemBounds.entries()) {
if (item.top === null) {
continue;
}
const inView =
item.top < input.scrollBottom && item.top + Math.max(1, item.height ?? 1) > input.scrollTop;
if (inView) {
// The first visible marker is the turn at the reader's current position.
return index;
}
if (item.top <= input.scrollTop) {
precedingIndex = index;
}
}
return precedingIndex;
}
export function resolveTimelineMinimapHasPersistentGutter(viewportWidth: number): boolean {
if (!Number.isFinite(viewportWidth) || viewportWidth <= 0) {
return false;
@@ -272,6 +272,25 @@ function buildSnapShotTimelineEntry(previewUrl?: string) {
}
describe("MessagesTimeline", () => {
it("renders previous and next controls with the minimap", () => {
const first = buildUserTimelineEntry("First turn");
const secondBase = buildUserTimelineEntry("Second turn");
const second = {
...secondBase,
id: "entry-2",
message: {
...secondBase.message,
id: MessageId.make("message-2"),
},
};
const markup = renderToStaticMarkup(
<MessagesTimeline {...buildProps()} timelineEntries={[first, second]} />,
);
expect(markup).toContain('aria-label="Previous turn"');
expect(markup).toContain('aria-label="Next turn"');
});
// Expanding history uses this suite's existing test renderer, deprecated in
// React 19. Migrate these interaction tests together when a DOM test setup is added.
it.each([{}, { text: "Text-only answer", file: "Answer with a file" }])(
@@ -524,6 +543,7 @@ describe("MessagesTimeline", () => {
const {
resolveTimelineIsAtEnd,
resolveTimelineMinimapHasPersistentGutter,
resolveTimelineMinimapCurrentIndex,
resolveTimelineMinimapHeightStyle,
resolveTimelineMinimapHitStripWidth,
resolveTimelineMinimapIndexFromPointer,
@@ -583,6 +603,35 @@ describe("MessagesTimeline", () => {
pointerY: 999,
}),
).toBe(100);
expect(
resolveTimelineMinimapCurrentIndex({
scrollTop: 100,
scrollBottom: 500,
itemBounds: [
{ top: 80, height: 20 },
{ top: 120, height: 20 },
{ top: 220, height: 20 },
],
}),
).toBe(1);
expect(
resolveTimelineMinimapCurrentIndex({
scrollTop: 150,
scrollBottom: 200,
itemBounds: [
{ top: 80, height: 20 },
{ top: 120, height: 20 },
{ top: 220, height: 20 },
],
}),
).toBe(1);
expect(
resolveTimelineMinimapCurrentIndex({
scrollTop: 0,
scrollBottom: 50,
itemBounds: [{ top: 80, height: 20 }],
}),
).toBeNull();
expect(resolveTimelineMinimapHasPersistentGutter(832)).toBe(false);
expect(resolveTimelineMinimapHasPersistentGutter(863)).toBe(false);
expect(resolveTimelineMinimapHasPersistentGutter(864)).toBe(true);
+193 -111
View File
@@ -85,6 +85,7 @@ import {
CheckIcon,
ChevronDownIcon,
ChevronRightIcon,
ChevronUpIcon,
CircleAlertIcon,
DownloadIcon,
EyeIcon,
@@ -139,6 +140,7 @@ import {
resolveAssistantMessageCopyState,
resolveTimelineIsAtEnd,
resolveTimelineMinimapHasPersistentGutter,
resolveTimelineMinimapCurrentIndex,
resolveTimelineMinimapHeightStyle,
resolveTimelineMinimapHitStripWidth,
resolveTimelineMinimapIndexFromPointer,
@@ -593,6 +595,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
});
const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false);
const [minimapHitStripWidth, setMinimapHitStripWidth] = useState(0);
const [minimapCurrentIndex, setMinimapCurrentIndex] = useState<number | null>(null);
const handleAnchorReady = useCallback(
(info: { anchorIndex: number | undefined }) => {
if (anchorMessageId !== null && info.anchorIndex !== undefined) {
@@ -665,21 +668,33 @@ export const MessagesTimeline = memo(function MessagesTimeline({
const scrollTop = state.scroll ?? 0;
const scrollBottom = scrollTop + (state.scrollLength ?? 0);
for (const item of minimapItems) {
const strip = minimapStripMap.get(item.id);
if (!strip) {
continue;
}
const itemBounds = minimapItems.map((item) => ({
top: resolveTimelineRowTop(state, item.rowIndex),
height: resolveTimelineRowHeight(state, item.rowIndex),
}));
const rowTop = resolveTimelineRowTop(state, item.rowIndex);
const rowHeight = resolveTimelineRowHeight(state, item.rowIndex);
for (const [index, item] of minimapItems.entries()) {
const strip = minimapStripMap.get(item.id);
const bounds = itemBounds[index];
const rowTop = bounds?.top ?? null;
const rowHeight = bounds?.height ?? null;
const inView =
rowTop !== null &&
rowTop < scrollBottom &&
rowTop + Math.max(1, rowHeight ?? 1) > scrollTop;
strip.dataset.inView = inView ? "true" : "false";
if (strip) {
strip.dataset.inView = inView ? "true" : "false";
}
}
const nextCurrentIndex = resolveTimelineMinimapCurrentIndex({
scrollTop,
scrollBottom,
itemBounds,
});
setMinimapCurrentIndex((current) =>
current === nextCurrentIndex ? current : nextCurrentIndex,
);
}, [
citationPositioning,
listRef,
@@ -871,6 +886,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
items={minimapItems}
hasPersistentGutter={minimapHasPersistentGutter}
hitStripWidth={minimapHitStripWidth}
currentIndex={minimapCurrentIndex}
stripMap={minimapStripMap}
onSelect={(item) => {
onManualNavigation();
@@ -972,12 +988,14 @@ function timelineMinimapEventTargetsPreview(target: EventTarget): boolean {
function TimelineMinimap({
hasPersistentGutter,
hitStripWidth,
currentIndex,
items,
stripMap,
onSelect,
}: {
hasPersistentGutter: boolean;
hitStripWidth: number;
currentIndex: number | null;
items: ReadonlyArray<TimelineMinimapItem>;
stripMap: Map<string, HTMLSpanElement>;
onSelect: (item: TimelineMinimapItem) => void;
@@ -999,6 +1017,11 @@ function TimelineMinimap({
: resolvedActiveIndex === items.length - 1
? "-100%"
: "-50%";
const resolvedCurrentIndex =
currentIndex !== null && currentIndex >= 0 && currentIndex < items.length ? currentIndex : null;
const previousItem =
resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex - 1] ?? null);
const nextItem = resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex + 1] ?? null);
const resolveActiveIndexFromPointer = useCallback(
(event: MouseEvent<HTMLElement>) => {
@@ -1047,128 +1070,187 @@ function TimelineMinimap({
data-persistent-gutter={hasPersistentGutter ? "true" : "false"}
>
<div className="relative h-full w-full select-none">
<button
aria-label={`Jump to message: ${activeItem?.userText ?? "User message"}`}
<div
className={cn(
"absolute top-1/2 left-3 -translate-y-1/2 cursor-pointer bg-transparent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/70",
"absolute top-1/2 left-3 -translate-y-1/2",
// The strip is width-capped to the side gutter so it never overlays
// the centered content column; with no usable gutter it goes inert.
hitStripWidth > 0 ? "pointer-events-auto" : "pointer-events-none",
)}
onBlur={() => setActiveIndex(null)}
onClick={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
const nextIndex = resolveActiveIndexFromPointer(event);
const nextItem = nextIndex === null ? null : (items[nextIndex] ?? null);
if (nextItem) {
onSelect(nextItem);
}
event.currentTarget.blur();
}}
onFocus={() => setActiveIndex((current) => current ?? 0)}
onKeyDown={(event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
moveActiveIndex(1);
} else if (event.key === "ArrowUp") {
event.preventDefault();
moveActiveIndex(-1);
} else if (event.key === "Home") {
event.preventDefault();
setActiveIndex(0);
} else if (event.key === "End") {
event.preventDefault();
setActiveIndex(items.length - 1);
} else if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
if (activeItem) {
onSelect(activeItem);
}
}
}}
onMouseLeave={() => setActiveIndex(null)}
onMouseMove={updateActiveIndexFromPointer}
onMouseDown={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
event.preventDefault();
}}
style={{
height: resolveTimelineMinimapHeightStyle(items.length),
width: resolveTimelineMinimapInteractiveWidth(hitStripWidth, activeItem !== null),
}}
type="button"
>
<div className="absolute top-0 left-3 h-full w-px bg-border/15" />
{items.map((item, index) => {
const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`;
const activeDistance =
resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex);
return (
<TimelineMinimapNavigationButton
direction="previous"
disabled={previousItem === null}
onClick={() => {
if (previousItem) onSelect(previousItem);
}}
/>
<button
aria-label={`Jump to message: ${activeItem?.userText ?? "User message"}`}
className="absolute inset-y-0 left-0 w-full cursor-pointer bg-transparent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/70"
onBlur={() => setActiveIndex(null)}
onClick={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
const nextIndex = resolveActiveIndexFromPointer(event);
const selectedItem = nextIndex === null ? null : (items[nextIndex] ?? null);
if (selectedItem) {
onSelect(selectedItem);
}
event.currentTarget.blur();
}}
onFocus={() => setActiveIndex((current) => current ?? resolvedCurrentIndex ?? 0)}
onKeyDown={(event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
moveActiveIndex(1);
} else if (event.key === "ArrowUp") {
event.preventDefault();
moveActiveIndex(-1);
} else if (event.key === "Home") {
event.preventDefault();
setActiveIndex(0);
} else if (event.key === "End") {
event.preventDefault();
setActiveIndex(items.length - 1);
} else if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
if (activeItem) {
onSelect(activeItem);
}
}
}}
onMouseLeave={() => setActiveIndex(null)}
onMouseMove={updateActiveIndexFromPointer}
onMouseDown={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
event.preventDefault();
}}
type="button"
>
<div className="absolute top-0 left-3 h-full w-px bg-border/15" />
{items.map((item, index) => {
const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`;
const activeDistance =
resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex);
return (
<span
aria-hidden="true"
className={cn(
"pointer-events-none absolute left-0 h-0.5 -translate-y-1/2 rounded-full bg-muted-foreground/35 transition-[background-color,width] duration-150 data-[in-view=true]:bg-foreground/90",
activeDistance === 0
? "w-6 bg-muted-foreground/75"
: activeDistance === 1
? "w-4"
: activeDistance === 2
? "w-2.5"
: "w-2",
)}
data-in-view="false"
data-minimap-strip
key={item.id}
ref={(node) => {
if (node) {
stripMap.set(item.id, node);
} else {
stripMap.delete(item.id);
}
}}
style={{ top }}
/>
);
})}
{activeItem ? (
<span
aria-hidden="true"
className={cn(
"pointer-events-none absolute left-0 h-0.5 -translate-y-1/2 rounded-full bg-muted-foreground/35 transition-[background-color,width] duration-150 data-[in-view=true]:bg-foreground/90",
activeDistance === 0
? "w-6 bg-muted-foreground/75"
: activeDistance === 1
? "w-4"
: activeDistance === 2
? "w-2.5"
: "w-2",
)}
data-in-view="false"
data-minimap-strip
key={item.id}
ref={(node) => {
if (node) {
stripMap.set(item.id, node);
} else {
stripMap.delete(item.id);
}
className="pointer-events-auto absolute left-8 w-80 cursor-text select-text"
data-minimap-preview
onMouseMove={(event) => event.stopPropagation()}
style={{
top: `${activeTopPercent}%`,
transform: `translateY(${activeTooltipTranslate})`,
}}
style={{ top }}
/>
);
})}
{activeItem ? (
<span
className="pointer-events-auto absolute left-8 w-80 cursor-text select-text"
data-minimap-preview
onMouseMove={(event) => event.stopPropagation()}
style={{
top: `${activeTopPercent}%`,
transform: `translateY(${activeTooltipTranslate})`,
}}
>
<span className="dropdown-glass block rounded-xl p-3 text-left text-popover-foreground shadow-xl shadow-black/25">
<span className="block max-w-full overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium leading-5">
{activeItem.userText ?? "User message"}
</span>
{activeItem.assistantText ? (
<span
className="mt-1 max-h-[3.75rem] overflow-hidden text-muted-foreground text-sm leading-5"
style={{
display: "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: 3,
}}
>
{activeItem.assistantText}
>
<span className="dropdown-glass block rounded-xl p-3 text-left text-popover-foreground shadow-xl shadow-black/25">
<span className="block max-w-full overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium leading-5">
{activeItem.userText ?? "User message"}
</span>
) : null}
{activeItem.assistantText ? (
<span
className="mt-1 max-h-[3.75rem] overflow-hidden text-muted-foreground text-sm leading-5"
style={{
display: "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: 3,
}}
>
{activeItem.assistantText}
</span>
) : null}
</span>
</span>
</span>
) : null}
</button>
) : null}
</button>
<TimelineMinimapNavigationButton
direction="next"
disabled={nextItem === null}
onClick={() => {
if (nextItem) onSelect(nextItem);
}}
/>
</div>
</div>
</div>
);
}
function TimelineMinimapNavigationButton({
direction,
disabled,
onClick,
}: {
direction: "previous" | "next";
disabled: boolean;
onClick: () => void;
}) {
const previous = direction === "previous";
const label = previous ? "Previous turn" : "Next turn";
const Icon = previous ? ChevronUpIcon : ChevronDownIcon;
return (
<Tooltip>
<TooltipTrigger
render={
<span
className={cn(
"absolute left-1 z-10 inline-flex -translate-x-1/2 opacity-0 pointer-events-auto transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",
previous ? "bottom-[calc(100%+2px)]" : "top-[calc(100%+2px)]",
)}
/>
}
>
<Button
aria-label={label}
disabled={disabled}
onClick={onClick}
size="icon-micro"
type="button"
variant="ghost-muted"
>
<Icon className="size-4 text-foreground/90" />
</Button>
</TooltipTrigger>
<TooltipPopup side={previous ? "top" : "bottom"}>{label}</TooltipPopup>
</Tooltip>
);
}
// ---------------------------------------------------------------------------
// TimelineRowContent — the actual row component
// ---------------------------------------------------------------------------