Skip to content
41 changes: 41 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
liveAnswerPreview,
deriveMessagesTimelineRowsWithState,
resolveAssistantMessageCopyState,
resolveTimelineMinimapMarkIndices,
type MessagesTimelineRow,
workEntryDisplayLabel,
} from "./MessagesTimeline.logic";
Expand Down Expand Up @@ -4777,3 +4778,43 @@ describe("subagent continued by a later turn", () => {
]);
});
});

describe("resolveTimelineMinimapMarkIndices", () => {
const railHeightPx = 512;
const itemCount = 600;

function expectSeparated(indices: ReadonlyArray<number>, exempt: ReadonlySet<number>) {
for (let slot = 1; slot < indices.length; slot += 1) {
const previous = indices[slot - 1]!;
const index = indices[slot]!;
expect(index).toBeGreaterThan(previous);
if (exempt.has(previous) && exempt.has(index)) continue;
expect(((index - previous) / (itemCount - 1)) * railHeightPx).toBeGreaterThanOrEqual(
6 - 1e-6,
);
}
}

it("draws every mark while they stay at least 6px apart", () => {
expect(resolveTimelineMinimapMarkIndices({ itemCount: 75, railHeightPx })).toHaveLength(75);
});

it("thins a capped rail so marks stay separate and both ends stay drawn", () => {
const indices = resolveTimelineMinimapMarkIndices({ itemCount, railHeightPx });
expect(indices.length).toBeLessThan(itemCount);
expect(indices[0]).toBe(0);
expect(indices.at(-1)).toBe(itemCount - 1);
expectSeparated(indices, new Set());
});

it("keeps the hovered and current marks and clears marks crowding them", () => {
const pins = [3, 42, 43, 596];
const indices = resolveTimelineMinimapMarkIndices({
itemCount,
railHeightPx,
pinnedIndices: [null, -1, 1.5, itemCount, ...pins],
});
expect(indices).toEqual(expect.arrayContaining([0, ...pins, itemCount - 1]));
expectSeparated(indices, new Set([0, itemCount - 1, ...pins]));
});
});
62 changes: 62 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ export const TIMELINE_MINIMAP_MIN_ITEMS = 2;
// endpoint previews (centered on their strip) still fit above and below it.
const TIMELINE_MINIMAP_MAX_HEIGHT_CSS = "max(4rem, calc(100% - 14.75rem))";
const TIMELINE_MINIMAP_PERSISTENT_GUTTER = 48;
// Idle ticks are `h-0.5` (2px). Centers closer than this read as one bar.
const TIMELINE_MINIMAP_MARK_MIN_SPACING_PX = 6;

export interface WorkGroupScrollAnchor {
readonly entryId: string;
Expand Down Expand Up @@ -340,6 +342,66 @@ export function resolveTimelineMinimapHeightStyle(itemCount: number): string {
return `min(${naturalHeight}px, ${TIMELINE_MINIMAP_MAX_HEIGHT_CSS})`;
}

/**
* Tick indices to draw once the height cap packs centers closer than 6px.
* The hovered message and the message in view are pinned so those marks stay
* on the exact row. Pointer hit-testing still walks every message.
*/
export function resolveTimelineMinimapMarkIndices(input: {
readonly itemCount: number;
readonly railHeightPx: number;
readonly pinnedIndices?: ReadonlyArray<number | null | undefined>;
}): ReadonlyArray<number> {
const count = input.itemCount;
if (!Number.isFinite(count) || count <= 0) return [];
if (count === 1) return [0];

const height =
Number.isFinite(input.railHeightPx) && input.railHeightPx > 0
? input.railHeightPx
: (count - 1) * TIMELINE_MINIMAP_ITEM_SPACING;
const spacing = height / (count - 1);
if (spacing >= TIMELINE_MINIMAP_MARK_MIN_SPACING_PX) {
return Array.from({ length: count }, (_, index) => index);
}

const minIndexGap = TIMELINE_MINIMAP_MARK_MIN_SPACING_PX / spacing;
const indices: number[] = [0];
let cursor = 0;
while (cursor + minIndexGap < count - 1) {
const next = Math.ceil(cursor + minIndexGap);
if (next <= cursor || next >= count - 1) break;
indices.push(next);
cursor = next;
}
if (count - 1 - cursor >= minIndexGap || indices.length === 1) {
indices.push(count - 1);
} else {
indices[indices.length - 1] = count - 1;
}

const pinned = new Set<number>();
for (const index of input.pinnedIndices ?? []) {
if (typeof index !== "number" || !Number.isInteger(index)) continue;
if (index < 0 || index >= count) continue;
pinned.add(index);
}
if (pinned.size === 0) return indices;

const painted = new Set(indices);
for (const pin of pinned) {
const crowded: number[] = [];
for (const index of painted) {
if (index === 0 || index === count - 1 || pinned.has(index)) continue;
const gap = (Math.abs(index - pin) / (count - 1)) * height;
if (gap < TIMELINE_MINIMAP_MARK_MIN_SPACING_PX - 1e-6) crowded.push(index);
}
for (const index of crowded) painted.delete(index);
painted.add(pin);
}
return Array.from(painted).sort((left, right) => left - right);
}

export function resolveTimelineMinimapTopPercent(index: number, itemCount: number): number {
if (itemCount <= 1) {
return 0;
Expand Down
23 changes: 22 additions & 1 deletion apps/web/src/components/chat/TimelineMinimap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Button } from "../ui/button";
import {
resolveTimelineMinimapHeightStyle,
resolveTimelineMinimapIndexFromPointer,
resolveTimelineMinimapMarkIndices,
resolveTimelineMinimapNavigationInteractive,
resolveTimelineMinimapTopPercent,
TIMELINE_MINIMAP_MIN_ITEMS,
} from "./MessagesTimeline.logic";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { cn } from "~/lib/utils";
import { observeResize } from "~/lib/observeResize";

// A release outside the window can skip `mouseup`, so the next button-free
// `mousemove` also counts.
Expand Down Expand Up @@ -49,6 +51,14 @@ export function TimelineMinimap({
onSelect: (item: TimelineMinimapItem) => void;
}) {
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const [railHeight, setRailHeight] = useState(0);
const railRef = (node: HTMLDivElement | null) => {
if (!node) return;
setRailHeight(node.getBoundingClientRect().height);
return observeResize(node, ([entry]) => {
if (entry) setRailHeight(entry.contentRect.height);
});
};
const wheelTargetRef = (node: HTMLDivElement | null) => {
if (!node) return;
node.addEventListener("wheel", onWheel, { passive: false });
Expand Down Expand Up @@ -98,6 +108,13 @@ export function TimelineMinimap({
});
};

const paintedMarks = resolveTimelineMinimapMarkIndices({
itemCount: items.length,
railHeightPx: railHeight,
pinnedIndices: [resolvedActiveIndex, resolvedCurrentIndex],
});
const paintedMarkSet = paintedMarks.length === items.length ? null : new Set(paintedMarks);

if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {
return null;
}
Expand All @@ -120,6 +137,7 @@ export function TimelineMinimap({
"absolute top-1/2 left-3 -translate-y-1/2",
hitStripWidth > 0 && !passThrough ? "pointer-events-auto" : "pointer-events-none",
)}
ref={railRef}
onMouseDown={() => {
if (pressedOnStripRef.current) return;
pressedOnStripRef.current = true;
Expand Down Expand Up @@ -191,6 +209,8 @@ export function TimelineMinimap({
const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`;
const activeDistance =
resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex);
// Thinned strips stay mounted so hover and in-view highlights keep their exact row.
const painted = paintedMarkSet === null || paintedMarkSet.has(index);
return (
// Compositor-friendly on purpose: in-view state and the hover
// fisheye animate constantly (every scroll tick and streaming
Expand All @@ -200,7 +220,8 @@ export function TimelineMinimap({
aria-hidden="true"
className={cn(
"pointer-events-none absolute left-0 h-0.5 w-6 origin-left -translate-y-1/2 rounded-full transition-transform duration-150 after:absolute after:inset-0 after:rounded-full after:bg-foreground/55 after:opacity-0 after:transition-opacity after:duration-150 data-[in-view=true]:after:opacity-100",
activeDistance === 0 ? "bg-muted-foreground/60" : "bg-muted-foreground/25",
painted &&
(activeDistance === 0 ? "bg-muted-foreground/60" : "bg-muted-foreground/25"),
activeDistance === 0
? "scale-x-100"
: activeDistance === 1
Expand Down
Loading