TypeScript
refactor(web): the timeline minimap lives in its own file (#22662)
Julius Marminge76e4aac3e9b9
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 1e591b56836..bb7f613602f 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -16,11 +16,7 @@ import {getQuestionTextPreview,hasQuestionAnswer,} from "@t3tools/client-runtime/work-log/user-input";-import {- deriveTimelineMinimapItems,- resolveTimelineMinimapPreview,- type TimelineMinimapItem,-} from "./timelineMinimapItems";+import { deriveTimelineMinimapItems } from "./timelineMinimapItems";import {COMPOSER_CONTEXT_KINDS,ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,@@ -127,7 +123,6 @@ import {CheckIcon,ChevronDownIcon,ChevronRightIcon,- ChevronUpIcon,CircleAlertIcon,DownloadIcon,EyeIcon,@@ -210,12 +205,7 @@ import {resolveTimelineMinimapCurrentIndex,isTimelineMinimapRowVisible,resolveTimelineMinimapVisibleRange,- resolveTimelineMinimapHeightStyle,resolveTimelineMinimapHitStripWidth,- resolveTimelineMinimapIndexFromPointer,- resolveTimelineMinimapInteractiveWidth,- resolveTimelineMinimapNavigationInteractive,- resolveTimelineMinimapTopPercent,resolveWorkGroupScrollIndex,shouldCollapseUserMessage,shouldFollowWorkGroupAppend,@@ -230,7 +220,6 @@ import {worktreeSetupAgentStarted,type StableMessagesTimelineRowsState,type MessagesTimelineRow,- TIMELINE_MINIMAP_MIN_ITEMS,type TimelineLatestRun,type WorkGroupScrollAnchor,} from "./MessagesTimeline.logic";@@ -309,6 +298,7 @@ import {formatReviewCommentFence,type ReviewCommentContext,} from "../../reviewCommentContext";+import { TimelineMinimap } from "./TimelineMinimap";interface TimelineRowSharedState {citationRequest: AssistantCitationTarget | null;@@ -1630,327 +1620,6 @@ function getItemType(item: MessagesTimelineRow) {return `message:${item.message.role}:${lengthBucket}`;}-function timelineMinimapEventTargetsPreview(target: EventTarget): boolean {- return target instanceof Element && target.closest("[data-minimap-preview]") !== null;-}--// A release outside the window can skip `mouseup`, so the next button-free-// `mousemove` also counts.-function onMouseButtonsReleased(onRelease: () => void): () => void {- const check = (event: globalThis.MouseEvent) => {- if (event.buttons !== 0) return;- stop();- onRelease();- };- const stop = () => {- window.removeEventListener("mouseup", check);- window.removeEventListener("mousemove", check);- };- window.addEventListener("mouseup", check);- window.addEventListener("mousemove", check);- return stop;-}--function TimelineMinimap({- hasPersistentGutter,- hitStripWidth,- currentIndex,- items,- stripMap,- onWheel,- onSelect,-}: {- hasPersistentGutter: boolean;- hitStripWidth: number;- currentIndex: number | null;- items: ReadonlyArray<TimelineMinimapItem>;- stripMap: Map<string, HTMLSpanElement>;- onWheel: (event: WheelEvent) => void;- onSelect: (item: TimelineMinimapItem) => void;-}) {- const [activeIndex, setActiveIndex] = useState<number | null>(null);- const wheelTargetRef = (node: HTMLDivElement | null) => {- if (!node) return;- node.addEventListener("wheel", onWheel, { passive: false });- return () => node.removeEventListener("wheel", onWheel);- };-- const pressedOnStripRef = useRef(false);- const [passThrough, setPassThrough] = useState(false);-- useEffect(() => {- if (!passThrough) return;- return onMouseButtonsReleased(() => setPassThrough(false));- }, [passThrough]);-- const resolvedActiveIndex =- activeIndex !== null && activeIndex < items.length ? activeIndex : null;- const activeItem = resolveTimelineMinimapPreview(- resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null),- );- const navigationInteractive =- resolveTimelineMinimapNavigationInteractive(hitStripWidth) && !passThrough;- const activeTopPercent =- resolvedActiveIndex === null- ? 0- : resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length);- const activeTooltipTranslate =- resolvedActiveIndex === null- ? "-50%"- : resolvedActiveIndex === 0- ? "0%"- : 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 = (event: MouseEvent<HTMLElement>) => {- const rect = event.currentTarget.getBoundingClientRect();- return resolveTimelineMinimapIndexFromPointer({- itemCount: items.length,- railTop: rect.top,- railHeight: rect.height,- pointerY: event.clientY,- });- };-- const updateActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => {- const nextIndex = resolveActiveIndexFromPointer(event);- setActiveIndex(nextIndex);- };-- const moveActiveIndex = (delta: number) => {- setActiveIndex((current) => {- const base = current ?? 0;- return Math.max(0, Math.min(items.length - 1, base + delta));- });- };-- if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {- return null;- }-- return (- <div- className={cn(- "group/minimap pointer-events-none absolute inset-y-0 left-0 z-40 hidden w-18 [@media(pointer:fine)]:block",- hasPersistentGutter- ? "opacity-100"- : "opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",- )}- data-testid="timeline-minimap"- data-persistent-gutter={hasPersistentGutter ? "true" : "false"}- ref={wheelTargetRef}- >- <div className="relative h-full w-full select-none">- <div- className={cn(- "absolute top-1/2 left-3 -translate-y-1/2",- hitStripWidth > 0 && !passThrough ? "pointer-events-auto" : "pointer-events-none",- )}- onMouseDown={() => {- if (pressedOnStripRef.current) return;- pressedOnStripRef.current = true;- onMouseButtonsReleased(() => {- pressedOnStripRef.current = false;- });- }}- onMouseMove={(event) => {- if (event.buttons !== 0 && !pressedOnStripRef.current) setPassThrough(true);- }}- style={{- height: resolveTimelineMinimapHeightStyle(items.length),- width: resolveTimelineMinimapInteractiveWidth(hitStripWidth, activeItem !== null),- }}- >- <TimelineMinimapNavigationButton- direction="previous"- disabled={previousItem === null}- interactive={navigationInteractive}- 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-inset 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={(event) => {- if (event.buttons !== 0 && !pressedOnStripRef.current) return;- updateActiveIndexFromPointer(event);- }}- 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 (- // 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.- <span- aria-hidden="true"- className={cn(- "group/strip pointer-events-none absolute left-0 h-0.5 w-6 origin-left -translate-y-1/2 rounded-full transition-transform duration-150",- activeDistance === 0 ? "bg-muted-foreground/75" : "bg-muted-foreground/35",- activeDistance === 0- ? "scale-x-100"- : activeDistance === 1- ? "scale-x-[0.667]"- : activeDistance === 2- ? "scale-x-[0.417]"- : "scale-x-[0.333]",- )}- 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 }}- >- <span className="absolute inset-0 rounded-full bg-foreground/90 opacity-0 transition-opacity duration-150 group-data-[in-view=true]/strip:opacity-100" />- </span>- );- })}- {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>- ) : null}- </span>- </span>- ) : null}- </button>- <TimelineMinimapNavigationButton- direction="next"- disabled={nextItem === null}- interactive={navigationInteractive}- onClick={() => {- if (nextItem) onSelect(nextItem);- }}- />- </div>- </div>- </div>- );-}--function TimelineMinimapNavigationButton({- direction,- disabled,- interactive,- onClick,-}: {- direction: "previous" | "next";- disabled: boolean;- interactive: 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 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",- interactive ? "pointer-events-auto" : "pointer-events-none",- 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>- );-}-type TimelineWorkEntry = Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"][number];type TimelineRow = MessagesTimelineRow;diff --git a/apps/web/src/components/chat/TimelineMinimap.tsx b/apps/web/src/components/chat/TimelineMinimap.tsxnew file mode 100644index 00000000000..c4cc203e8a7--- /dev/null+++ b/apps/web/src/components/chat/TimelineMinimap.tsx@@ -0,0 +1,335 @@+import { resolveTimelineMinimapPreview, type TimelineMinimapItem } from "./timelineMinimapItems";+import { useEffect, useRef, useState, type MouseEvent } from "react";+import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";+import { Button } from "../ui/button";+import {+ resolveTimelineMinimapHeightStyle,+ resolveTimelineMinimapIndexFromPointer,+ resolveTimelineMinimapInteractiveWidth,+ resolveTimelineMinimapNavigationInteractive,+ resolveTimelineMinimapTopPercent,+ TIMELINE_MINIMAP_MIN_ITEMS,+} from "./MessagesTimeline.logic";+import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";+import { cn } from "~/lib/utils";++function timelineMinimapEventTargetsPreview(target: EventTarget): boolean {+ return target instanceof Element && target.closest("[data-minimap-preview]") !== null;+}++// A release outside the window can skip `mouseup`, so the next button-free+// `mousemove` also counts.+function onMouseButtonsReleased(onRelease: () => void): () => void {+ const check = (event: globalThis.MouseEvent) => {+ if (event.buttons !== 0) return;+ stop();+ onRelease();+ };+ const stop = () => {+ window.removeEventListener("mouseup", check);+ window.removeEventListener("mousemove", check);+ };+ window.addEventListener("mouseup", check);+ window.addEventListener("mousemove", check);+ return stop;+}++export function TimelineMinimap({+ hasPersistentGutter,+ hitStripWidth,+ currentIndex,+ items,+ stripMap,+ onWheel,+ onSelect,+}: {+ hasPersistentGutter: boolean;+ hitStripWidth: number;+ currentIndex: number | null;+ items: ReadonlyArray<TimelineMinimapItem>;+ stripMap: Map<string, HTMLSpanElement>;+ onWheel: (event: WheelEvent) => void;+ onSelect: (item: TimelineMinimapItem) => void;+}) {+ const [activeIndex, setActiveIndex] = useState<number | null>(null);+ const wheelTargetRef = (node: HTMLDivElement | null) => {+ if (!node) return;+ node.addEventListener("wheel", onWheel, { passive: false });+ return () => node.removeEventListener("wheel", onWheel);+ };++ const pressedOnStripRef = useRef(false);+ const [passThrough, setPassThrough] = useState(false);++ useEffect(() => {+ if (!passThrough) return;+ return onMouseButtonsReleased(() => setPassThrough(false));+ }, [passThrough]);++ const resolvedActiveIndex =+ activeIndex !== null && activeIndex < items.length ? activeIndex : null;+ const activeItem = resolveTimelineMinimapPreview(+ resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null),+ );+ const navigationInteractive =+ resolveTimelineMinimapNavigationInteractive(hitStripWidth) && !passThrough;+ const activeTopPercent =+ resolvedActiveIndex === null+ ? 0+ : resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length);+ const activeTooltipTranslate =+ resolvedActiveIndex === null+ ? "-50%"+ : resolvedActiveIndex === 0+ ? "0%"+ : 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 = (event: MouseEvent<HTMLElement>) => {+ const rect = event.currentTarget.getBoundingClientRect();+ return resolveTimelineMinimapIndexFromPointer({+ itemCount: items.length,+ railTop: rect.top,+ railHeight: rect.height,+ pointerY: event.clientY,+ });+ };++ const updateActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => {+ const nextIndex = resolveActiveIndexFromPointer(event);+ setActiveIndex(nextIndex);+ };++ const moveActiveIndex = (delta: number) => {+ setActiveIndex((current) => {+ const base = current ?? 0;+ return Math.max(0, Math.min(items.length - 1, base + delta));+ });+ };++ if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {+ return null;+ }++ return (+ <div+ className={cn(+ "group/minimap pointer-events-none absolute inset-y-0 left-0 z-40 hidden w-18 [@media(pointer:fine)]:block",+ hasPersistentGutter+ ? "opacity-100"+ : "opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",+ )}+ data-testid="timeline-minimap"+ data-persistent-gutter={hasPersistentGutter ? "true" : "false"}+ ref={wheelTargetRef}+ >+ <div className="relative h-full w-full select-none">+ <div+ className={cn(+ "absolute top-1/2 left-3 -translate-y-1/2",+ hitStripWidth > 0 && !passThrough ? "pointer-events-auto" : "pointer-events-none",+ )}+ onMouseDown={() => {+ if (pressedOnStripRef.current) return;+ pressedOnStripRef.current = true;+ onMouseButtonsReleased(() => {+ pressedOnStripRef.current = false;+ });+ }}+ onMouseMove={(event) => {+ if (event.buttons !== 0 && !pressedOnStripRef.current) setPassThrough(true);+ }}+ style={{+ height: resolveTimelineMinimapHeightStyle(items.length),+ width: resolveTimelineMinimapInteractiveWidth(hitStripWidth, activeItem !== null),+ }}+ >+ <TimelineMinimapNavigationButton+ direction="previous"+ disabled={previousItem === null}+ interactive={navigationInteractive}+ 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-inset 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={(event) => {+ if (event.buttons !== 0 && !pressedOnStripRef.current) return;+ updateActiveIndexFromPointer(event);+ }}+ 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 (+ // 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.+ <span+ aria-hidden="true"+ className={cn(+ "group/strip pointer-events-none absolute left-0 h-0.5 w-6 origin-left -translate-y-1/2 rounded-full transition-transform duration-150",+ activeDistance === 0 ? "bg-muted-foreground/75" : "bg-muted-foreground/35",+ activeDistance === 0+ ? "scale-x-100"+ : activeDistance === 1+ ? "scale-x-[0.667]"+ : activeDistance === 2+ ? "scale-x-[0.417]"+ : "scale-x-[0.333]",+ )}+ 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 }}+ >+ <span className="absolute inset-0 rounded-full bg-foreground/90 opacity-0 transition-opacity duration-150 group-data-[in-view=true]/strip:opacity-100" />+ </span>+ );+ })}+ {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>+ ) : null}+ </span>+ </span>+ ) : null}+ </button>+ <TimelineMinimapNavigationButton+ direction="next"+ disabled={nextItem === null}+ interactive={navigationInteractive}+ onClick={() => {+ if (nextItem) onSelect(nextItem);+ }}+ />+ </div>+ </div>+ </div>+ );+}++function TimelineMinimapNavigationButton({+ direction,+ disabled,+ interactive,+ onClick,+}: {+ direction: "previous" | "next";+ disabled: boolean;+ interactive: 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 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",+ interactive ? "pointer-events-auto" : "pointer-events-none",+ 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>+ );+}