Skip to content

Commit 60e3181

Browse files
refactor(web): the timeline minimap lives in its own file
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6a6c84f commit 60e3181

2 files changed

Lines changed: 337 additions & 333 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 2 additions & 333 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import {
1616
getQuestionTextPreview,
1717
hasQuestionAnswer,
1818
} from "@t3tools/client-runtime/work-log/user-input";
19-
import {
20-
deriveTimelineMinimapItems,
21-
resolveTimelineMinimapPreview,
22-
type TimelineMinimapItem,
23-
} from "./timelineMinimapItems";
19+
import { deriveTimelineMinimapItems } from "./timelineMinimapItems";
2420
import {
2521
COMPOSER_CONTEXT_KINDS,
2622
ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
@@ -127,7 +123,6 @@ import {
127123
CheckIcon,
128124
ChevronDownIcon,
129125
ChevronRightIcon,
130-
ChevronUpIcon,
131126
CircleAlertIcon,
132127
DownloadIcon,
133128
EyeIcon,
@@ -210,12 +205,7 @@ import {
210205
resolveTimelineMinimapCurrentIndex,
211206
isTimelineMinimapRowVisible,
212207
resolveTimelineMinimapVisibleRange,
213-
resolveTimelineMinimapHeightStyle,
214208
resolveTimelineMinimapHitStripWidth,
215-
resolveTimelineMinimapIndexFromPointer,
216-
resolveTimelineMinimapInteractiveWidth,
217-
resolveTimelineMinimapNavigationInteractive,
218-
resolveTimelineMinimapTopPercent,
219209
resolveWorkGroupScrollIndex,
220210
shouldCollapseUserMessage,
221211
shouldFollowWorkGroupAppend,
@@ -230,7 +220,6 @@ import {
230220
worktreeSetupAgentStarted,
231221
type StableMessagesTimelineRowsState,
232222
type MessagesTimelineRow,
233-
TIMELINE_MINIMAP_MIN_ITEMS,
234223
type TimelineLatestRun,
235224
type WorkGroupScrollAnchor,
236225
} from "./MessagesTimeline.logic";
@@ -309,6 +298,7 @@ import {
309298
formatReviewCommentFence,
310299
type ReviewCommentContext,
311300
} from "../../reviewCommentContext";
301+
import { TimelineMinimap } from "./TimelineMinimap";
312302

313303
interface TimelineRowSharedState {
314304
citationRequest: AssistantCitationTarget | null;
@@ -1630,327 +1620,6 @@ function getItemType(item: MessagesTimelineRow) {
16301620
return `message:${item.message.role}:${lengthBucket}`;
16311621
}
16321622

1633-
function timelineMinimapEventTargetsPreview(target: EventTarget): boolean {
1634-
return target instanceof Element && target.closest("[data-minimap-preview]") !== null;
1635-
}
1636-
1637-
// A release outside the window can skip `mouseup`, so the next button-free
1638-
// `mousemove` also counts.
1639-
function onMouseButtonsReleased(onRelease: () => void): () => void {
1640-
const check = (event: globalThis.MouseEvent) => {
1641-
if (event.buttons !== 0) return;
1642-
stop();
1643-
onRelease();
1644-
};
1645-
const stop = () => {
1646-
window.removeEventListener("mouseup", check);
1647-
window.removeEventListener("mousemove", check);
1648-
};
1649-
window.addEventListener("mouseup", check);
1650-
window.addEventListener("mousemove", check);
1651-
return stop;
1652-
}
1653-
1654-
function TimelineMinimap({
1655-
hasPersistentGutter,
1656-
hitStripWidth,
1657-
currentIndex,
1658-
items,
1659-
stripMap,
1660-
onWheel,
1661-
onSelect,
1662-
}: {
1663-
hasPersistentGutter: boolean;
1664-
hitStripWidth: number;
1665-
currentIndex: number | null;
1666-
items: ReadonlyArray<TimelineMinimapItem>;
1667-
stripMap: Map<string, HTMLSpanElement>;
1668-
onWheel: (event: WheelEvent) => void;
1669-
onSelect: (item: TimelineMinimapItem) => void;
1670-
}) {
1671-
const [activeIndex, setActiveIndex] = useState<number | null>(null);
1672-
const wheelTargetRef = (node: HTMLDivElement | null) => {
1673-
if (!node) return;
1674-
node.addEventListener("wheel", onWheel, { passive: false });
1675-
return () => node.removeEventListener("wheel", onWheel);
1676-
};
1677-
1678-
const pressedOnStripRef = useRef(false);
1679-
const [passThrough, setPassThrough] = useState(false);
1680-
1681-
useEffect(() => {
1682-
if (!passThrough) return;
1683-
return onMouseButtonsReleased(() => setPassThrough(false));
1684-
}, [passThrough]);
1685-
1686-
const resolvedActiveIndex =
1687-
activeIndex !== null && activeIndex < items.length ? activeIndex : null;
1688-
const activeItem = resolveTimelineMinimapPreview(
1689-
resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null),
1690-
);
1691-
const navigationInteractive =
1692-
resolveTimelineMinimapNavigationInteractive(hitStripWidth) && !passThrough;
1693-
const activeTopPercent =
1694-
resolvedActiveIndex === null
1695-
? 0
1696-
: resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length);
1697-
const activeTooltipTranslate =
1698-
resolvedActiveIndex === null
1699-
? "-50%"
1700-
: resolvedActiveIndex === 0
1701-
? "0%"
1702-
: resolvedActiveIndex === items.length - 1
1703-
? "-100%"
1704-
: "-50%";
1705-
const resolvedCurrentIndex =
1706-
currentIndex !== null && currentIndex >= 0 && currentIndex < items.length ? currentIndex : null;
1707-
const previousItem =
1708-
resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex - 1] ?? null);
1709-
const nextItem = resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex + 1] ?? null);
1710-
1711-
const resolveActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => {
1712-
const rect = event.currentTarget.getBoundingClientRect();
1713-
return resolveTimelineMinimapIndexFromPointer({
1714-
itemCount: items.length,
1715-
railTop: rect.top,
1716-
railHeight: rect.height,
1717-
pointerY: event.clientY,
1718-
});
1719-
};
1720-
1721-
const updateActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => {
1722-
const nextIndex = resolveActiveIndexFromPointer(event);
1723-
setActiveIndex(nextIndex);
1724-
};
1725-
1726-
const moveActiveIndex = (delta: number) => {
1727-
setActiveIndex((current) => {
1728-
const base = current ?? 0;
1729-
return Math.max(0, Math.min(items.length - 1, base + delta));
1730-
});
1731-
};
1732-
1733-
if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {
1734-
return null;
1735-
}
1736-
1737-
return (
1738-
<div
1739-
className={cn(
1740-
"group/minimap pointer-events-none absolute inset-y-0 left-0 z-40 hidden w-18 [@media(pointer:fine)]:block",
1741-
hasPersistentGutter
1742-
? "opacity-100"
1743-
: "opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",
1744-
)}
1745-
data-testid="timeline-minimap"
1746-
data-persistent-gutter={hasPersistentGutter ? "true" : "false"}
1747-
ref={wheelTargetRef}
1748-
>
1749-
<div className="relative h-full w-full select-none">
1750-
<div
1751-
className={cn(
1752-
"absolute top-1/2 left-3 -translate-y-1/2",
1753-
hitStripWidth > 0 && !passThrough ? "pointer-events-auto" : "pointer-events-none",
1754-
)}
1755-
onMouseDown={() => {
1756-
if (pressedOnStripRef.current) return;
1757-
pressedOnStripRef.current = true;
1758-
onMouseButtonsReleased(() => {
1759-
pressedOnStripRef.current = false;
1760-
});
1761-
}}
1762-
onMouseMove={(event) => {
1763-
if (event.buttons !== 0 && !pressedOnStripRef.current) setPassThrough(true);
1764-
}}
1765-
style={{
1766-
height: resolveTimelineMinimapHeightStyle(items.length),
1767-
width: resolveTimelineMinimapInteractiveWidth(hitStripWidth, activeItem !== null),
1768-
}}
1769-
>
1770-
<TimelineMinimapNavigationButton
1771-
direction="previous"
1772-
disabled={previousItem === null}
1773-
interactive={navigationInteractive}
1774-
onClick={() => {
1775-
if (previousItem) onSelect(previousItem);
1776-
}}
1777-
/>
1778-
<button
1779-
aria-label={`Jump to message: ${activeItem?.userText ?? "User message"}`}
1780-
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"
1781-
onBlur={() => setActiveIndex(null)}
1782-
onClick={(event) => {
1783-
if (timelineMinimapEventTargetsPreview(event.target)) {
1784-
return;
1785-
}
1786-
const nextIndex = resolveActiveIndexFromPointer(event);
1787-
const selectedItem = nextIndex === null ? null : (items[nextIndex] ?? null);
1788-
if (selectedItem) {
1789-
onSelect(selectedItem);
1790-
}
1791-
event.currentTarget.blur();
1792-
}}
1793-
onFocus={() => setActiveIndex((current) => current ?? resolvedCurrentIndex ?? 0)}
1794-
onKeyDown={(event) => {
1795-
if (event.key === "ArrowDown") {
1796-
event.preventDefault();
1797-
moveActiveIndex(1);
1798-
} else if (event.key === "ArrowUp") {
1799-
event.preventDefault();
1800-
moveActiveIndex(-1);
1801-
} else if (event.key === "Home") {
1802-
event.preventDefault();
1803-
setActiveIndex(0);
1804-
} else if (event.key === "End") {
1805-
event.preventDefault();
1806-
setActiveIndex(items.length - 1);
1807-
} else if (event.key === "Enter" || event.key === " ") {
1808-
event.preventDefault();
1809-
if (activeItem) {
1810-
onSelect(activeItem);
1811-
}
1812-
}
1813-
}}
1814-
onMouseLeave={() => setActiveIndex(null)}
1815-
onMouseMove={(event) => {
1816-
if (event.buttons !== 0 && !pressedOnStripRef.current) return;
1817-
updateActiveIndexFromPointer(event);
1818-
}}
1819-
onMouseDown={(event) => {
1820-
if (timelineMinimapEventTargetsPreview(event.target)) {
1821-
return;
1822-
}
1823-
event.preventDefault();
1824-
}}
1825-
type="button"
1826-
>
1827-
<div className="absolute top-0 left-3 h-full w-px bg-border/15" />
1828-
{items.map((item, index) => {
1829-
const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`;
1830-
const activeDistance =
1831-
resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex);
1832-
return (
1833-
// Compositor-friendly on purpose: in-view state and the hover
1834-
// fisheye animate constantly (every scroll tick and streaming
1835-
// update flips a band of strips), so the strip animates only
1836-
// transform and opacity.
1837-
<span
1838-
aria-hidden="true"
1839-
className={cn(
1840-
"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",
1841-
activeDistance === 0 ? "bg-muted-foreground/75" : "bg-muted-foreground/35",
1842-
activeDistance === 0
1843-
? "scale-x-100"
1844-
: activeDistance === 1
1845-
? "scale-x-[0.667]"
1846-
: activeDistance === 2
1847-
? "scale-x-[0.417]"
1848-
: "scale-x-[0.333]",
1849-
)}
1850-
data-in-view="false"
1851-
data-minimap-strip
1852-
key={item.id}
1853-
ref={(node) => {
1854-
if (node) {
1855-
stripMap.set(item.id, node);
1856-
} else {
1857-
stripMap.delete(item.id);
1858-
}
1859-
}}
1860-
style={{ top }}
1861-
>
1862-
<span className="absolute inset-0 rounded-full bg-foreground/90 opacity-0 transition-opacity duration-150 group-data-[in-view=true]/strip:opacity-100" />
1863-
</span>
1864-
);
1865-
})}
1866-
{activeItem ? (
1867-
<span
1868-
className="pointer-events-auto absolute left-8 w-80 cursor-text select-text"
1869-
data-minimap-preview
1870-
onMouseMove={(event) => event.stopPropagation()}
1871-
style={{
1872-
top: `${activeTopPercent}%`,
1873-
transform: `translateY(${activeTooltipTranslate})`,
1874-
}}
1875-
>
1876-
<span className="dropdown-glass block rounded-xl p-3 text-left text-popover-foreground shadow-xl shadow-black/25">
1877-
<span className="block max-w-full overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium leading-5">
1878-
{activeItem.userText ?? "User message"}
1879-
</span>
1880-
{activeItem.assistantText ? (
1881-
<span
1882-
className="mt-1 max-h-[3.75rem] overflow-hidden text-muted-foreground text-sm leading-5"
1883-
style={{
1884-
display: "-webkit-box",
1885-
WebkitBoxOrient: "vertical",
1886-
WebkitLineClamp: 3,
1887-
}}
1888-
>
1889-
{activeItem.assistantText}
1890-
</span>
1891-
) : null}
1892-
</span>
1893-
</span>
1894-
) : null}
1895-
</button>
1896-
<TimelineMinimapNavigationButton
1897-
direction="next"
1898-
disabled={nextItem === null}
1899-
interactive={navigationInteractive}
1900-
onClick={() => {
1901-
if (nextItem) onSelect(nextItem);
1902-
}}
1903-
/>
1904-
</div>
1905-
</div>
1906-
</div>
1907-
);
1908-
}
1909-
1910-
function TimelineMinimapNavigationButton({
1911-
direction,
1912-
disabled,
1913-
interactive,
1914-
onClick,
1915-
}: {
1916-
direction: "previous" | "next";
1917-
disabled: boolean;
1918-
interactive: boolean;
1919-
onClick: () => void;
1920-
}) {
1921-
const previous = direction === "previous";
1922-
const label = previous ? "Previous turn" : "Next turn";
1923-
const Icon = previous ? ChevronUpIcon : ChevronDownIcon;
1924-
1925-
return (
1926-
<Tooltip>
1927-
<TooltipTrigger
1928-
render={
1929-
<span
1930-
className={cn(
1931-
"absolute left-1 z-10 inline-flex -translate-x-1/2 opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",
1932-
interactive ? "pointer-events-auto" : "pointer-events-none",
1933-
previous ? "bottom-[calc(100%+2px)]" : "top-[calc(100%+2px)]",
1934-
)}
1935-
/>
1936-
}
1937-
>
1938-
<Button
1939-
aria-label={label}
1940-
disabled={disabled}
1941-
onClick={onClick}
1942-
size="icon-micro"
1943-
type="button"
1944-
variant="ghost-muted"
1945-
>
1946-
<Icon className="size-4 text-foreground/90" />
1947-
</Button>
1948-
</TooltipTrigger>
1949-
<TooltipPopup side={previous ? "top" : "bottom"}>{label}</TooltipPopup>
1950-
</Tooltip>
1951-
);
1952-
}
1953-
19541623
type TimelineWorkEntry = Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"][number];
19551624
type TimelineRow = MessagesTimelineRow;
19561625

0 commit comments

Comments
 (0)