Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion components/ChatMinimap.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const jiti = createJiti(import.meta.url, {
jsx: { runtime: "automatic" },
tsconfigPaths: true,
});
const { ChatMinimap, extractPreviewText, getBarWidth } = await jiti.import("./ChatMinimap.tsx");
const { ChatMinimap, extractPreviewText, getBarWidth, getTooltipTopPx, railIndexForPointer, railOverflows } = await jiti.import("./ChatMinimap.tsx");

test("extractPreviewText extracts clean text from user string content", () => {
const preview = extractPreviewText({
Expand Down Expand Up @@ -104,3 +104,57 @@ test("falls back to assistant messages when no user prompts exist", () => {
assert.match(html, /aria-label="Jump to: Welcome to the agent"/);
assert.match(html, /aria-label="Jump to: Ready for tasks"/);
});

test("tooltip stays aligned with its row when the rail has scrolled (#251)", () => {
// Unscrolled: the reporter's row 0 center (6px padding + half of 18px).
assert.equal(getTooltipTopPx(0, 0), 15);
assert.equal(getTooltipTopPx(20, 0), 375);
// Scrolled: a row whose unclipped center would be 1095px (outside the
// 520px rail, as in the report for tick 60) is pulled back into the
// viewport by exactly the rail's scroll offset.
assert.equal(getTooltipTopPx(60, 594), 501);
assert.equal(getTooltipTopPx(60, 0), 1095);
});

test("rail rows do not shrink below the row height (#251)", () => {
const messages = Array.from({ length: 86 }, (_, i) => ({ role: "user", content: `prompt ${i}` }));
const html = renderToStaticMarkup(
React.createElement(ChatMinimap, {
messages,
scrollContainer: { current: null },
messageRefs: { current: [] },
})
);
assert.match(html, /flex-shrink:0/);
assert.match(html, /min-height:18px/);
});

test("drag scrubber maps a pointer to the row actually under it when scrolled (#251 follow-up)", () => {
// Unscrolled: pointer at the center of row 20 (6 + 20*18 + 9 = 375).
assert.equal(railIndexForPointer(375, 0, 86), 20);
// Scrolled by 594px: a viewport offset of 15px lands at content
// 609, i.e. row 33 ((609 - 6) / 18 = 33.5, floored).
assert.equal(railIndexForPointer(15, 594, 86), 33);
// Clamped at both ends.
assert.equal(railIndexForPointer(-10, 0, 86), 0);
assert.equal(railIndexForPointer(9999, 0, 86), 85);
});

test("rail is always scrollable, even with few rows on a short viewport (#251 follow-up)", () => {
const messages = [{ role: "user", content: "only prompt" }];
const html = renderToStaticMarkup(
React.createElement(ChatMinimap, {
messages,
scrollContainer: { current: null },
messageRefs: { current: [] },
})
);
assert.match(html, /overflow-y:auto/);
});

test("active-row scroll is gated on actual rail overflow, not row count (#251 follow-up)", () => {
// 15 rows on a short viewport: content 282px > 250px viewport cap.
assert.equal(railOverflows(282, 250), true);
assert.equal(railOverflows(282, 282), false);
assert.equal(railOverflows(200, 520), false);
});
64 changes: 58 additions & 6 deletions components/ChatMinimap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,52 @@ export function getBarWidth(text: string, isActive: boolean, isHovered: boolean)

const ROW_HEIGHT = 18;

/**
* Vertical center of a rail row's tooltip, in the unclipped outer
* container's coordinates. The rail is a flex column whose rows used to
* shrink below ROW_HEIGHT once the list exceeded the rail's max-height,
* so a tooltip positioned purely from the index drifted below its row
* and eventually out of view (#251). Rows are now non-shrinking (see the
* row style below), so the rail scrolls instead of compressing; the only
* correction left is the rail's own scroll offset, which is 0 while the
* rail fits and otherwise moves the visible row up by exactly that much.
*/
export function getTooltipTopPx(displayIndex: number, railScrollTop = 0): number {
return 6 + displayIndex * ROW_HEIGHT + ROW_HEIGHT / 2 - railScrollTop;
}

/**
* Row index under a drag pointer, in a rail whose rows are fixed-height
* and may be scrolled (#251 follow-up). The pointer's viewport offset
* inside the rail plus the rail's scroll offset gives its position in
* the rail's content, where row i starts at 6 + i * ROW_HEIGHT (6px
* top padding). The old ratio-over-visible-height mapping only worked
* while every row was visible at once, which fixed-height rows no
* longer guarantee.
*/
/**
* Whether the rail actually overflows, so the active row only gets
* scrolled into view when it could sit outside the visible rail
* (#251 follow-up). A row-count guard misses short viewports: 15
* fixed-height rows already need 282px, more than a <376px viewport's
* 75vh cap, so the rail can overflow with far fewer than 16 rows.
*/
export function railOverflows(scrollHeight: number, clientHeight: number): boolean {
return scrollHeight > clientHeight;
}

export function railIndexForPointer(relY: number, railScrollTop: number, nodeCount: number): number {
if (nodeCount <= 0) return 0;
const contentY = railScrollTop + relY;
const index = Math.floor((contentY - 6) / ROW_HEIGHT);
return Math.max(0, Math.min(nodeCount - 1, index));
}

export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer, messageRefs }: Props) {
const [activeIndex, setActiveIndex] = useState(0);
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
const [isRailHovered, setIsRailHovered] = useState(false);
const [railScrollTop, setRailScrollTop] = useState(0);
const railRef = useRef<HTMLDivElement>(null);
const draggingRef = useRef(false);
const scrollRafRef = useRef<number | null>(null);
Expand Down Expand Up @@ -254,9 +296,9 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
return () => ro.disconnect();
}, [scrollContainer]);

// Keep active row visible within the rail when node count overflows
// Keep active row visible within the rail when the rail overflows
useEffect(() => {
if (railRef.current && nodes.length > 15) {
if (railRef.current && railOverflows(railRef.current.scrollHeight, railRef.current.clientHeight)) {
const activeRow = railRef.current.querySelector(`[data-index="${activeIndex}"]`) as HTMLElement | null;
activeRow?.scrollIntoView({ block: "nearest", behavior: reducedMotion ? "auto" : "smooth" });
}
Expand All @@ -272,8 +314,7 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
if (!railEl) return;
const rect = railEl.getBoundingClientRect();
const relY = clientY - rect.top;
const ratio = Math.max(0, Math.min(1, relY / rect.height));
const targetIdx = Math.min(nodes.length - 1, Math.floor(ratio * nodes.length));
const targetIdx = railIndexForPointer(relY, railEl.scrollTop, nodes.length);
if (nodes[targetIdx]) {
scrollToNodeRef.current(nodes[targetIdx], false);
setActiveIndex(targetIdx);
Expand Down Expand Up @@ -313,7 +354,7 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
const displayTooltipIndex = hoveredIndex !== null ? hoveredIndex : (isRailHovered ? activeIndex : null);
const tooltipNode = displayTooltipIndex !== null ? nodes[displayTooltipIndex] : null;
// Calculate unclipped vertical center position for the tooltip
const tooltipTopPx = displayTooltipIndex !== null ? 6 + displayTooltipIndex * ROW_HEIGHT + ROW_HEIGHT / 2 : 0;
const tooltipTopPx = displayTooltipIndex !== null ? getTooltipTopPx(displayTooltipIndex, railScrollTop) : 0;

return (
<div
Expand Down Expand Up @@ -372,6 +413,7 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
setIsRailHovered(false);
setHoveredIndex(null);
}}
onScroll={(e) => setRailScrollTop(e.currentTarget.scrollTop)}
onMouseDown={handleMouseDown}
style={{
position: "relative",
Expand All @@ -380,7 +422,11 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
alignItems: "flex-start",
padding: "6px 6px 6px 8px",
maxHeight: "min(75vh, 520px)",
overflowY: nodes.length > 25 ? "auto" : "visible",
// Always scrollable: with fixed-height rows, even 25 or fewer
// rows can exceed a short viewport's 75vh cap, and "visible"
// would then spill them out of the rail with no way to reach
// them (#251 follow-up). scrollbarWidth none keeps it clean.
overflowY: "auto",
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
scrollbarWidth: "none",
cursor: "pointer",
}}
Expand Down Expand Up @@ -431,6 +477,12 @@ export const ChatMinimap = memo(function ChatMinimap({ messages, scrollContainer
display: "flex",
alignItems: "center",
height: ROW_HEIGHT,
// Rows must not shrink: with maxHeight + overflowY auto the
// rail is meant to scroll past ~25 entries, but the flex
// default (flexShrink 1) compressed every row to ~520/N
// instead, which is what broke tooltip alignment (#251).
flexShrink: 0,
minHeight: ROW_HEIGHT,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
paddingLeft: 6,
paddingRight: 8,
cursor: "pointer",
Expand Down
Loading