diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index a34db0e1cf..314ff66ba1 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -12,6 +12,9 @@ import { type MessageId, type OrchestrationLatestTurn, type TurnId } from "@t3to export const MAX_VISIBLE_WORK_LOG_ENTRIES = 1; export const TIMELINE_MINIMAP_ITEM_SPACING = 8; export const TIMELINE_MINIMAP_MIN_ITEMS = 2; +export const TIMELINE_MINIMAP_MAX_HEIGHT_CSS = "calc(100vh - 18rem)"; +export const TIMELINE_CONTENT_MAX_WIDTH = 768; +export const TIMELINE_MINIMAP_PERSISTENT_GUTTER = 72; export interface TimelineEndState { readonly isAtEnd?: boolean; @@ -22,6 +25,45 @@ export function resolveTimelineIsAtEnd(state: TimelineEndState | undefined): boo return state?.isNearEnd ?? state?.isAtEnd; } +export function resolveTimelineMinimapHeightStyle(itemCount: number): string { + const naturalHeight = Math.max(1, (itemCount - 1) * TIMELINE_MINIMAP_ITEM_SPACING); + return `min(${naturalHeight}px, ${TIMELINE_MINIMAP_MAX_HEIGHT_CSS})`; +} + +export function resolveTimelineMinimapTopPercent(index: number, itemCount: number): number { + if (itemCount <= 1) { + return 0; + } + return (Math.max(0, Math.min(index, itemCount - 1)) / (itemCount - 1)) * 100; +} + +export function resolveTimelineMinimapIndexFromPointer(input: { + readonly itemCount: number; + readonly railTop: number; + readonly railHeight: number; + readonly pointerY: number; +}): number | null { + if (input.itemCount <= 0 || input.railHeight <= 0) { + return null; + } + if (input.itemCount === 1) { + return 0; + } + + const progress = Math.max(0, Math.min(1, (input.pointerY - input.railTop) / input.railHeight)); + return Math.max(0, Math.min(input.itemCount - 1, Math.round(progress * (input.itemCount - 1)))); +} + +export function resolveTimelineMinimapHasPersistentGutter(viewportWidth: number): boolean { + if (!Number.isFinite(viewportWidth) || viewportWidth <= 0) { + return false; + } + + const contentWidth = Math.min(viewportWidth, TIMELINE_CONTENT_MAX_WIDTH); + const sideGutter = Math.max(0, (viewportWidth - contentWidth) / 2); + return sideGutter >= TIMELINE_MINIMAP_PERSISTENT_GUTTER; +} + function computeElapsedMs(startIso: string, endIso: string): number | null { const start = Date.parse(startIso); const end = Date.parse(endIso); diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index 070f7c39a1..e12887f384 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -190,12 +190,40 @@ function buildUserTimelineEntry(text: string) { describe("MessagesTimeline", () => { it("uses LegendList isNearEnd when deciding whether the live edge is visible", async () => { - const { resolveTimelineIsAtEnd } = await import("./MessagesTimeline.logic"); + const { + resolveTimelineIsAtEnd, + resolveTimelineMinimapHasPersistentGutter, + resolveTimelineMinimapHeightStyle, + resolveTimelineMinimapIndexFromPointer, + resolveTimelineMinimapTopPercent, + } = await import("./MessagesTimeline.logic"); expect(resolveTimelineIsAtEnd({ isNearEnd: true, isAtEnd: false })).toBe(true); expect(resolveTimelineIsAtEnd({ isNearEnd: false, isAtEnd: true })).toBe(false); expect(resolveTimelineIsAtEnd({ isAtEnd: true })).toBe(true); expect(resolveTimelineIsAtEnd(undefined)).toBeUndefined(); + + expect(resolveTimelineMinimapHeightStyle(5)).toBe("min(32px, calc(100vh - 18rem))"); + expect(resolveTimelineMinimapTopPercent(2, 5)).toBe(50); + expect( + resolveTimelineMinimapIndexFromPointer({ + itemCount: 101, + railTop: 100, + railHeight: 500, + pointerY: 350, + }), + ).toBe(50); + expect( + resolveTimelineMinimapIndexFromPointer({ + itemCount: 101, + railTop: 100, + railHeight: 500, + pointerY: 999, + }), + ).toBe(100); + expect(resolveTimelineMinimapHasPersistentGutter(832)).toBe(false); + expect(resolveTimelineMinimapHasPersistentGutter(911)).toBe(false); + expect(resolveTimelineMinimapHasPersistentGutter(912)).toBe(true); }); it("anchors a sent attachment message using its measured height", async () => { diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 0ccb619ef4..905d9bf81f 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -18,6 +18,7 @@ import { useRef, useState, type KeyboardEvent, + type MouseEvent, type ReactNode, } from "react"; import { flushSync } from "react-dom"; @@ -70,9 +71,12 @@ import { normalizeCompactToolLabel, resolveAssistantMessageCopyState, resolveTimelineIsAtEnd, + resolveTimelineMinimapHasPersistentGutter, + resolveTimelineMinimapHeightStyle, + resolveTimelineMinimapIndexFromPointer, + resolveTimelineMinimapTopPercent, type StableMessagesTimelineRowsState, type MessagesTimelineRow, - TIMELINE_MINIMAP_ITEM_SPACING, TIMELINE_MINIMAP_MIN_ITEMS, type TimelineLatestTurn, } from "./MessagesTimeline.logic"; @@ -209,7 +213,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ }: MessagesTimelineProps) { const [expandedTurnIds, setExpandedTurnIds] = useState>(new Set()); const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState>(new Set()); - const minimapStripRefs = useRef(new Map()); + const [minimapStripMap] = useState(() => new Map()); const onToggleTurnFold = useCallback((turnId: TurnId) => { setExpandedTurnIds((existing) => { @@ -312,6 +316,10 @@ export const MessagesTimeline = memo(function MessagesTimeline({ ); const rows = useStableRows(rawRows); const minimapItems = useMemo(() => deriveTimelineMinimapItems(rows), [rows]); + const [timelineViewportElement, setTimelineViewportElement] = useState( + null, + ); + const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false); const handleAnchorReady = useCallback( (info: { anchorIndex: number | undefined }) => { if (anchorMessageId !== null && info.anchorIndex !== undefined) { @@ -351,7 +359,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ const scrollBottom = scrollTop + (state.scrollLength ?? 0); for (const item of minimapItems) { - const strip = minimapStripRefs.current.get(item.id); + const strip = minimapStripMap.get(item.id); if (!strip) { continue; } @@ -365,13 +373,37 @@ export const MessagesTimeline = memo(function MessagesTimeline({ strip.dataset.inView = inView ? "true" : "false"; } - }, [listRef, minimapItems, onIsAtEndChange]); + }, [listRef, minimapItems, minimapStripMap, onIsAtEndChange]); useEffect(() => { const frame = requestAnimationFrame(handleScroll); return () => cancelAnimationFrame(frame); }, [handleScroll, rows.length]); + useEffect(() => { + if (!timelineViewportElement) { + return; + } + + const measure = () => { + const viewportWidth = timelineViewportElement.getBoundingClientRect().width; + const nextHasPersistentGutter = resolveTimelineMinimapHasPersistentGutter(viewportWidth); + setMinimapHasPersistentGutter((current) => + current === nextHasPersistentGutter ? current : nextHasPersistentGutter, + ); + }; + + const frame = requestAnimationFrame(measure); + + const observer = new ResizeObserver(measure); + observer.observe(timelineViewportElement); + + return () => { + cancelAnimationFrame(frame); + observer.disconnect(); + }; + }, [timelineViewportElement, rows.length]); + const sharedState = useMemo( () => ({ timestampFormat, @@ -436,36 +468,40 @@ export const MessagesTimeline = memo(function MessagesTimeline({ return ( - - ref={listRef} - data={rows} - keyExtractor={keyExtractor} - getItemType={getItemType} - renderItem={renderItem} - estimatedItemSize={90} - initialScrollAtEnd - {...(anchoredEndSpace ? { anchoredEndSpace } : {})} - contentInsetEndAdjustment={contentInsetEndAdjustment} - maintainVisibleContentPosition={{ - data: true, - size: false, - }} - onScroll={handleScroll} - className="scrollbar-gutter-both h-full min-h-0 overflow-x-hidden overscroll-y-contain px-3 [overflow-anchor:none] sm:px-5" - ListHeaderComponent={TIMELINE_LIST_HEADER} - ListFooterComponent={TIMELINE_LIST_FOOTER} - /> - { - void listRef.current?.scrollToIndex({ - index: item.rowIndex, - animated: true, - viewOffset: 24, - }); - }} - /> +
+ + ref={listRef} + data={rows} + keyExtractor={keyExtractor} + getItemType={getItemType} + renderItem={renderItem} + estimatedItemSize={90} + initialScrollAtEnd + {...(anchoredEndSpace ? { anchoredEndSpace } : {})} + contentInsetEndAdjustment={contentInsetEndAdjustment} + maintainVisibleContentPosition={{ + data: true, + size: false, + }} + onScroll={handleScroll} + className="scrollbar-gutter-both h-full min-h-0 overflow-x-hidden overscroll-y-contain px-3 [overflow-anchor:none] sm:px-5" + ListHeaderComponent={TIMELINE_LIST_HEADER} + ListFooterComponent={TIMELINE_LIST_FOOTER} + /> + { + void listRef.current?.scrollToIndex({ + index: item.rowIndex, + animated: true, + viewOffset: 24, + }); + }} + /> +
); @@ -550,83 +586,173 @@ function resolveTimelineRowHeight(state: TimelinePositionState, rowIndex: number } function TimelineMinimap({ + bottomInset, + hasPersistentGutter, items, - stripRefs, + stripMap, onSelect, }: { + bottomInset: number; + hasPersistentGutter: boolean; items: ReadonlyArray; - stripRefs: React.RefObject>; + stripMap: Map; onSelect: (item: TimelineMinimapItem) => void; }) { + const [activeIndex, setActiveIndex] = useState(null); + + const resolvedActiveIndex = + activeIndex !== null && activeIndex < items.length ? activeIndex : null; + const activeItem = resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null); + const activeTopPercent = + resolvedActiveIndex === null + ? 0 + : resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length); + const activeTooltipTranslate = + resolvedActiveIndex === null + ? "-50%" + : resolvedActiveIndex === 0 + ? "0%" + : resolvedActiveIndex === items.length - 1 + ? "-100%" + : "-50%"; + + const updateActiveIndexFromPointer = useCallback( + (event: MouseEvent) => { + const rect = event.currentTarget.getBoundingClientRect(); + const nextIndex = resolveTimelineMinimapIndexFromPointer({ + itemCount: items.length, + railTop: rect.top, + railHeight: rect.height, + pointerY: event.clientY, + }); + setActiveIndex(nextIndex); + }, + [items.length], + ); + + const moveActiveIndex = useCallback( + (delta: number) => { + setActiveIndex((current) => { + const base = current ?? 0; + return Math.max(0, Math.min(items.length - 1, base + delta)); + }); + }, + [items.length], + ); + if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) { return null; } - const minimapHeight = Math.max(1, (items.length - 1) * TIMELINE_MINIMAP_ITEM_SPACING); + const safeBottomInset = Math.max(0, Math.ceil(bottomInset)); return (