From 4657edf2deeae4dd7bc29edda7b48a74920eadea Mon Sep 17 00:00:00 2001 From: macodev00 Date: Thu, 22 Oct 2026 07:09:17 +0000 Subject: [PATCH 1/5] fix(web): keep timeline minimap marks separated in long threads Once the rail height cap packs user-message ticks closer than 6px they merge into one bar. Paint an even subset, and pin the hovered message and the message in view, so gaps stay visible while pointer jumps still hit the exact message. --- .../components/chat/MessagesTimeline.logic.ts | 85 +++++++++++- .../src/components/chat/TimelineMinimap.tsx | 62 ++++++++- .../chat/timelineMinimapMarks.test.ts | 121 ++++++++++++++++++ 3 files changed, 263 insertions(+), 5 deletions(-) create mode 100644 apps/web/src/components/chat/timelineMinimapMarks.test.ts diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index 45bb7936298d7..eeca8463a125d 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -275,8 +275,11 @@ export function workEntryIsVisibleInGroup( } const TIMELINE_MINIMAP_ITEM_SPACING = 8; export const TIMELINE_MINIMAP_MIN_ITEMS = 2; -const TIMELINE_MINIMAP_MAX_HEIGHT_CSS = "calc(100vh - 18rem)"; +const TIMELINE_MINIMAP_MAX_HEIGHT_REM = 18; +const TIMELINE_MINIMAP_MAX_HEIGHT_CSS = `calc(100vh - ${TIMELINE_MINIMAP_MAX_HEIGHT_REM}rem)`; 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; @@ -342,6 +345,86 @@ export function resolveTimelineMinimapHeightStyle(itemCount: number): string { return `min(${naturalHeight}px, ${TIMELINE_MINIMAP_MAX_HEIGHT_CSS})`; } +/** + * Pixel height of the rail: `(count - 1) * 8`, capped at `calc(100vh - 18rem)`. + * An unknown viewport returns the uncapped height so marks stay spaced at 8px + * until the real cap is known. + */ +export function resolveTimelineMinimapRailHeightPx( + itemCount: number, + viewportHeightPx: number, + rootFontSizePx = 16, +): number { + const naturalHeight = Math.max(1, (itemCount - 1) * TIMELINE_MINIMAP_ITEM_SPACING); + if (!Number.isFinite(viewportHeightPx) || viewportHeightPx <= 0) { + return naturalHeight; + } + const fontSize = Number.isFinite(rootFontSizePx) && rootFontSizePx > 0 ? rootFontSizePx : 16; + const cap = viewportHeightPx - TIMELINE_MINIMAP_MAX_HEIGHT_REM * fontSize; + if (cap <= 0) return naturalHeight; + return Math.min(naturalHeight, cap); +} + +/** + * 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 === pin || 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 c4cc203e8a75f..727705b412133 100644 --- a/apps/web/src/components/chat/TimelineMinimap.tsx +++ b/apps/web/src/components/chat/TimelineMinimap.tsx @@ -1,18 +1,51 @@ import { resolveTimelineMinimapPreview, type TimelineMinimapItem } from "./timelineMinimapItems"; -import { useEffect, useRef, useState, type MouseEvent } from "react"; +import { useEffect, useRef, useState, useSyncExternalStore, type MouseEvent } from "react"; import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"; import { Button } from "../ui/button"; import { resolveTimelineMinimapHeightStyle, resolveTimelineMinimapIndexFromPointer, resolveTimelineMinimapInteractiveWidth, + resolveTimelineMinimapMarkIndices, resolveTimelineMinimapNavigationInteractive, + resolveTimelineMinimapRailHeightPx, resolveTimelineMinimapTopPercent, TIMELINE_MINIMAP_MIN_ITEMS, } from "./MessagesTimeline.logic"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { cn } from "~/lib/utils"; +const SERVER_MINIMAP_VIEWPORT = { height: 0, fontSize: 16 }; +let minimapViewportSnapshot = SERVER_MINIMAP_VIEWPORT; + +function subscribeTimelineMinimapViewport(onChange: () => void) { + window.addEventListener("resize", onChange); + return () => window.removeEventListener("resize", onChange); +} + +function getServerTimelineMinimapViewport() { + return SERVER_MINIMAP_VIEWPORT; +} + +// Stable snapshot: a fresh object on every read loops useSyncExternalStore, and +// reading the root font size on each hover forces layout. +function readTimelineMinimapViewport() { + const height = window.innerHeight; + if ( + minimapViewportSnapshot !== SERVER_MINIMAP_VIEWPORT && + minimapViewportSnapshot.height === height + ) { + return minimapViewportSnapshot; + } + const parsed = Number.parseFloat(getComputedStyle(document.documentElement).fontSize); + const fontSize = Number.isFinite(parsed) && parsed > 0 ? parsed : 16; + if (minimapViewportSnapshot.height === height && minimapViewportSnapshot.fontSize === fontSize) { + return minimapViewportSnapshot; + } + minimapViewportSnapshot = { height, fontSize }; + return minimapViewportSnapshot; +} + function timelineMinimapEventTargetsPreview(target: EventTarget): boolean { return target instanceof Element && target.closest("[data-minimap-preview]") !== null; } @@ -52,6 +85,11 @@ export function TimelineMinimap({ onSelect: (item: TimelineMinimapItem) => void; }) { const [activeIndex, setActiveIndex] = useState(null); + const viewport = useSyncExternalStore( + subscribeTimelineMinimapViewport, + readTimelineMinimapViewport, + getServerTimelineMinimapViewport, + ); const wheelTargetRef = (node: HTMLDivElement | null) => { if (!node) return; node.addEventListener("wheel", onWheel, { passive: false }); @@ -113,6 +151,17 @@ export function TimelineMinimap({ }); }; + const paintedMarks = resolveTimelineMinimapMarkIndices({ + itemCount: items.length, + railHeightPx: resolveTimelineMinimapRailHeightPx( + items.length, + viewport.height, + viewport.fontSize, + ), + pinnedIndices: [resolvedActiveIndex, resolvedCurrentIndex], + }); + const paintedMarkSet = paintedMarks.length === items.length ? null : new Set(paintedMarks); + if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) { return null; } @@ -212,16 +261,19 @@ export function TimelineMinimap({ const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`; const activeDistance = resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex); + 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 // update flips a band of strips), so the strip animates only - // transform and opacity. + // transform and opacity. Unpainted strips stay mounted so the + // in-view highlight can still land on the exact message.