diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 09cbfe4e7775a..c76d8b0204f5f 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -29,6 +29,7 @@ import { liveAnswerPreview, deriveMessagesTimelineRowsWithState, resolveAssistantMessageCopyState, + resolveTimelineMinimapMarkIndices, type MessagesTimelineRow, workEntryDisplayLabel, } from "./MessagesTimeline.logic"; @@ -4777,3 +4778,43 @@ describe("subagent continued by a later turn", () => { ]); }); }); + +describe("resolveTimelineMinimapMarkIndices", () => { + const railHeightPx = 512; + const itemCount = 600; + + function expectSeparated(indices: ReadonlyArray, exempt: ReadonlySet) { + 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])); + }); +}); diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index c422656647863..86d5b6d3e87e3 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -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; @@ -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; +}): ReadonlyArray { + 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(); + 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; diff --git a/apps/web/src/components/chat/TimelineMinimap.tsx b/apps/web/src/components/chat/TimelineMinimap.tsx index 3a32ec20400d9..3fcc4ebd34e33 100644 --- a/apps/web/src/components/chat/TimelineMinimap.tsx +++ b/apps/web/src/components/chat/TimelineMinimap.tsx @@ -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. @@ -49,6 +51,14 @@ export function TimelineMinimap({ onSelect: (item: TimelineMinimapItem) => void; }) { const [activeIndex, setActiveIndex] = useState(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 }); @@ -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; } @@ -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; @@ -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 @@ -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