Restore chat scroll affordances and timeline minimap
- Prevent the scroll-to-end pill from flickering during thread switches - Preserve anchor scroll position during user-driven scrolling - Add timeline minimap navigation and live-edge detection
This commit is contained in:
1 parent
a9b1190a11
commit
da040f6c31
5 files changed
+534
-276
No files matched your search
@@ -1161,6 +1161,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
|
||||
const updateHeight = () => {
|
||||
const nextHeight = Math.ceil(composerOverlayElement.getBoundingClientRect().height);
|
||||
if (nextHeight <= 0) return;
|
||||
setComposerOverlayHeight((currentHeight) =>
|
||||
currentHeight === nextHeight ? currentHeight : nextHeight,
|
||||
);
|
||||
@@ -3148,18 +3149,61 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
],
|
||||
);
|
||||
|
||||
// Debounce *showing* the scroll-to-bottom pill so it doesn't flash during
|
||||
// thread switches. LegendList fires scroll events with isAtEnd=false while
|
||||
// initialScrollAtEnd is settling; hiding is always immediate.
|
||||
const showScrollDebouncer = useRef(
|
||||
new Debouncer(() => setShowScrollToBottom(true), { wait: 150 }),
|
||||
);
|
||||
|
||||
// Scrolling is explicit so streamed timeline updates never take control away
|
||||
// from the user after the newly sent row has been positioned once.
|
||||
const scrollToEnd = useCallback((animated = false) => {
|
||||
isAtEndRef.current = true;
|
||||
showScrollDebouncer.current.cancel();
|
||||
setShowScrollToBottom(false);
|
||||
void legendListRef.current?.scrollToEnd?.({ animated });
|
||||
}, []);
|
||||
const positionedTimelineAnchorRef = useRef<MessageId | null>(null);
|
||||
const settledTimelineAnchorRef = useRef<MessageId | null>(null);
|
||||
const anchorUserScrollGenerationRef = useRef(0);
|
||||
const pendingAnchorScrollRestoreRef = useRef<{
|
||||
readonly messageId: MessageId;
|
||||
readonly offset: number;
|
||||
readonly userScrollGeneration: number;
|
||||
} | null>(null);
|
||||
const anchorScrollRestoreFrameRef = useRef<number | null>(null);
|
||||
useEffect(() => {
|
||||
let removeListeners: (() => void) | null = null;
|
||||
const frame = requestAnimationFrame(() => {
|
||||
const scrollNode = legendListRef.current?.getScrollableNode();
|
||||
if (!scrollNode) {
|
||||
return;
|
||||
}
|
||||
const markUserScrollIntent = () => {
|
||||
anchorUserScrollGenerationRef.current += 1;
|
||||
pendingAnchorScrollRestoreRef.current = null;
|
||||
if (anchorScrollRestoreFrameRef.current !== null) {
|
||||
cancelAnimationFrame(anchorScrollRestoreFrameRef.current);
|
||||
anchorScrollRestoreFrameRef.current = null;
|
||||
}
|
||||
};
|
||||
scrollNode.addEventListener("wheel", markUserScrollIntent, { passive: true });
|
||||
scrollNode.addEventListener("touchmove", markUserScrollIntent, { passive: true });
|
||||
scrollNode.addEventListener("pointerdown", markUserScrollIntent, { passive: true });
|
||||
removeListeners = () => {
|
||||
scrollNode.removeEventListener("wheel", markUserScrollIntent);
|
||||
scrollNode.removeEventListener("touchmove", markUserScrollIntent);
|
||||
scrollNode.removeEventListener("pointerdown", markUserScrollIntent);
|
||||
};
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(frame);
|
||||
removeListeners?.();
|
||||
};
|
||||
}, [activeThread?.id]);
|
||||
|
||||
const onTimelineAnchorReady = useCallback((messageId: MessageId, anchorIndex: number) => {
|
||||
if (positionedTimelineAnchorRef.current === messageId) {
|
||||
return;
|
||||
@@ -3211,7 +3255,11 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
return;
|
||||
}
|
||||
if (pendingAnchorScrollRestoreRef.current === null) {
|
||||
pendingAnchorScrollRestoreRef.current = { messageId, offset: scrollOffset };
|
||||
pendingAnchorScrollRestoreRef.current = {
|
||||
messageId,
|
||||
offset: scrollOffset,
|
||||
userScrollGeneration: anchorUserScrollGenerationRef.current,
|
||||
};
|
||||
}
|
||||
if (anchorScrollRestoreFrameRef.current !== null) {
|
||||
return;
|
||||
@@ -3220,18 +3268,23 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
anchorScrollRestoreFrameRef.current = null;
|
||||
const pending = pendingAnchorScrollRestoreRef.current;
|
||||
pendingAnchorScrollRestoreRef.current = null;
|
||||
if (pending && settledTimelineAnchorRef.current === pending.messageId) {
|
||||
void legendListRef.current?.scrollToOffset({ offset: pending.offset, animated: false });
|
||||
if (
|
||||
pending &&
|
||||
settledTimelineAnchorRef.current === pending.messageId &&
|
||||
pending.userScrollGeneration === anchorUserScrollGenerationRef.current
|
||||
) {
|
||||
const list = legendListRef.current;
|
||||
const currentScrollOffset = list?.getState().scroll;
|
||||
if (
|
||||
typeof currentScrollOffset === "number" &&
|
||||
Math.abs(currentScrollOffset - pending.offset) <= 2
|
||||
) {
|
||||
void list?.scrollToOffset({ offset: pending.offset, animated: false });
|
||||
}
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
// Debounce *showing* the scroll-to-bottom pill so it doesn't flash during
|
||||
// thread switches. LegendList fires scroll events with isAtEnd=false while
|
||||
// initialScrollAtEnd is settling; hiding is always immediate.
|
||||
const showScrollDebouncer = useRef(
|
||||
new Debouncer(() => setShowScrollToBottom(true), { wait: 150 }),
|
||||
);
|
||||
const onIsAtEndChange = useCallback((isAtEnd: boolean) => {
|
||||
if (isAtEndRef.current === isAtEnd) return;
|
||||
isAtEndRef.current = isAtEnd;
|
||||
@@ -4870,7 +4923,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
onIsAtEndChange={onIsAtEndChange}
|
||||
/>
|
||||
|
||||
{/* scroll to bottom pill — shown when user has scrolled away from the bottom */}
|
||||
{/* scroll to end pill — shown when user has scrolled away from the live edge */}
|
||||
{showScrollToBottom && (
|
||||
<div
|
||||
className="pointer-events-none absolute left-1/2 z-30 flex -translate-x-1/2 justify-center py-1.5"
|
||||
@@ -4878,11 +4931,13 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Scroll to end"
|
||||
title="Scroll to end"
|
||||
onClick={() => scrollToEnd(true)}
|
||||
className="pointer-events-auto flex items-center gap-1.5 rounded-full border border-border/60 bg-card px-3 py-1 text-muted-foreground text-xs shadow-sm transition-colors hover:border-border hover:text-foreground hover:cursor-pointer"
|
||||
>
|
||||
<ChevronDownIcon className="size-3.5" />
|
||||
Scroll to bottom
|
||||
Scroll to end
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -10,6 +10,17 @@ import { type ChatMessage, type ProposedPlan, type TurnDiffSummary } from "../..
|
||||
import { type MessageId, type OrchestrationLatestTurn, type TurnId } from "@t3tools/contracts";
|
||||
|
||||
export const MAX_VISIBLE_WORK_LOG_ENTRIES = 1;
|
||||
export const TIMELINE_MINIMAP_HEIGHT = 180;
|
||||
export const TIMELINE_MINIMAP_MIN_ITEMS = 2;
|
||||
|
||||
export interface TimelineEndState {
|
||||
readonly isAtEnd?: boolean;
|
||||
readonly isNearEnd?: boolean;
|
||||
}
|
||||
|
||||
export function resolveTimelineIsAtEnd(state: TimelineEndState | undefined): boolean | undefined {
|
||||
return state?.isNearEnd ?? state?.isAtEnd;
|
||||
}
|
||||
|
||||
function computeElapsedMs(startIso: string, endIso: string): number | null {
|
||||
const start = Date.parse(startIso);
|
||||
|
||||
@@ -189,6 +189,15 @@ 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");
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
it("anchors a sent attachment message using its measured height", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const onAnchorReady = vi.fn();
|
||||
|
||||
@@ -69,8 +69,11 @@ import {
|
||||
deriveMessagesTimelineRows,
|
||||
normalizeCompactToolLabel,
|
||||
resolveAssistantMessageCopyState,
|
||||
resolveTimelineIsAtEnd,
|
||||
type StableMessagesTimelineRowsState,
|
||||
type MessagesTimelineRow,
|
||||
TIMELINE_MINIMAP_HEIGHT,
|
||||
TIMELINE_MINIMAP_MIN_ITEMS,
|
||||
type TimelineLatestTurn,
|
||||
} from "./MessagesTimeline.logic";
|
||||
import { TerminalContextInlineChip } from "./TerminalContextInlineChip";
|
||||
@@ -206,6 +209,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
}: MessagesTimelineProps) {
|
||||
const [expandedTurnIds, setExpandedTurnIds] = useState<ReadonlySet<TurnId>>(new Set());
|
||||
const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState<ReadonlySet<string>>(new Set());
|
||||
const minimapStripRefs = useRef(new Map<string, HTMLButtonElement>());
|
||||
|
||||
const onToggleTurnFold = useCallback((turnId: TurnId) => {
|
||||
setExpandedTurnIds((existing) => {
|
||||
@@ -307,6 +311,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
],
|
||||
);
|
||||
const rows = useStableRows(rawRows);
|
||||
const minimapItems = useMemo(() => deriveTimelineMinimapItems(rows), [rows]);
|
||||
const handleAnchorReady = useCallback(
|
||||
(info: { anchorIndex: number | undefined }) => {
|
||||
if (anchorMessageId !== null && info.anchorIndex !== undefined) {
|
||||
@@ -334,10 +339,49 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
|
||||
const handleScroll = useCallback(() => {
|
||||
const state = listRef.current?.getState?.();
|
||||
if (state) {
|
||||
onIsAtEndChange(state.isAtEnd);
|
||||
const isAtEnd = resolveTimelineIsAtEnd(state);
|
||||
if (isAtEnd !== undefined) {
|
||||
onIsAtEndChange(isAtEnd);
|
||||
}
|
||||
}, [listRef, onIsAtEndChange]);
|
||||
if (!state || minimapItems.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const contentLength = Math.max(1, state.contentLength ?? 0);
|
||||
const scrollTop = state.scroll ?? 0;
|
||||
const scrollBottom = scrollTop + (state.scrollLength ?? 0);
|
||||
const fallbackStep =
|
||||
minimapItems.length > 1 ? TIMELINE_MINIMAP_HEIGHT / (minimapItems.length - 1) : 0;
|
||||
|
||||
for (const [fallbackIndex, item] of minimapItems.entries()) {
|
||||
const strip = minimapStripRefs.current.get(item.id);
|
||||
if (!strip) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const rowTop = resolveTimelineRowTop(state, item.rowIndex);
|
||||
const rowHeight = resolveTimelineRowHeight(state, item.rowIndex);
|
||||
const top =
|
||||
rowTop === null
|
||||
? fallbackIndex * fallbackStep
|
||||
: Math.min(
|
||||
TIMELINE_MINIMAP_HEIGHT,
|
||||
Math.max(0, (rowTop / contentLength) * TIMELINE_MINIMAP_HEIGHT),
|
||||
);
|
||||
const inView =
|
||||
rowTop !== null &&
|
||||
rowTop < scrollBottom &&
|
||||
rowTop + Math.max(1, rowHeight ?? 1) > scrollTop;
|
||||
|
||||
strip.style.top = `${top}px`;
|
||||
strip.dataset.inView = inView ? "true" : "false";
|
||||
}
|
||||
}, [listRef, minimapItems, onIsAtEndChange]);
|
||||
|
||||
useEffect(() => {
|
||||
const frame = requestAnimationFrame(handleScroll);
|
||||
return () => cancelAnimationFrame(frame);
|
||||
}, [handleScroll, rows.length]);
|
||||
|
||||
const sharedState = useMemo<TimelineRowSharedState>(
|
||||
() => ({
|
||||
@@ -422,6 +466,17 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
ListHeaderComponent={TIMELINE_LIST_HEADER}
|
||||
ListFooterComponent={TIMELINE_LIST_FOOTER}
|
||||
/>
|
||||
<TimelineMinimap
|
||||
items={minimapItems}
|
||||
stripRefs={minimapStripRefs}
|
||||
onSelect={(item) => {
|
||||
void listRef.current?.scrollToIndex({
|
||||
index: item.rowIndex,
|
||||
animated: true,
|
||||
viewOffset: 24,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</TimelineRowActivityCtx>
|
||||
</TimelineRowCtx>
|
||||
);
|
||||
@@ -435,6 +490,136 @@ function getItemType(item: MessagesTimelineRow) {
|
||||
return item.kind === "message" ? `message:${item.message.role}` : item.kind;
|
||||
}
|
||||
|
||||
interface TimelineMinimapItem {
|
||||
readonly id: string;
|
||||
readonly rowIndex: number;
|
||||
readonly userText: string | null;
|
||||
readonly assistantText: string | null;
|
||||
}
|
||||
|
||||
interface TimelinePositionState {
|
||||
readonly contentLength?: number;
|
||||
readonly scroll?: number;
|
||||
readonly scrollLength?: number;
|
||||
readonly positionAtIndex?: (index: number) => number | undefined;
|
||||
readonly sizeAtIndex?: (index: number) => number | undefined;
|
||||
}
|
||||
|
||||
function deriveTimelineMinimapItems(
|
||||
rows: ReadonlyArray<MessagesTimelineRow>,
|
||||
): TimelineMinimapItem[] {
|
||||
const items: TimelineMinimapItem[] = [];
|
||||
for (let index = 0; index < rows.length; index += 1) {
|
||||
const row = rows[index];
|
||||
if (row?.kind !== "message" || row.message.role !== "user") {
|
||||
continue;
|
||||
}
|
||||
|
||||
items.push({
|
||||
id: row.id,
|
||||
rowIndex: index,
|
||||
userText: compactMinimapPreview(row.message.text),
|
||||
assistantText: compactMinimapPreview(resolveFinalAssistantTextForTurn(rows, index)),
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
function resolveFinalAssistantTextForTurn(
|
||||
rows: ReadonlyArray<MessagesTimelineRow>,
|
||||
userRowIndex: number,
|
||||
) {
|
||||
let finalAssistantText: string | null = null;
|
||||
for (let index = userRowIndex + 1; index < rows.length; index += 1) {
|
||||
const row = rows[index];
|
||||
if (row?.kind !== "message") {
|
||||
continue;
|
||||
}
|
||||
if (row.message.role === "user") {
|
||||
break;
|
||||
}
|
||||
if (row.message.role === "assistant") {
|
||||
finalAssistantText = row.message.text ?? null;
|
||||
}
|
||||
}
|
||||
return finalAssistantText;
|
||||
}
|
||||
|
||||
function compactMinimapPreview(text: string | null | undefined) {
|
||||
const compact = text?.replace(/\s+/g, " ").trim() ?? "";
|
||||
return compact.length > 0 ? compact : null;
|
||||
}
|
||||
|
||||
function resolveTimelineRowTop(state: TimelinePositionState, rowIndex: number) {
|
||||
const top = state.positionAtIndex?.(rowIndex);
|
||||
return typeof top === "number" && Number.isFinite(top) ? top : null;
|
||||
}
|
||||
|
||||
function resolveTimelineRowHeight(state: TimelinePositionState, rowIndex: number) {
|
||||
const height = state.sizeAtIndex?.(rowIndex);
|
||||
return typeof height === "number" && Number.isFinite(height) ? height : null;
|
||||
}
|
||||
|
||||
function TimelineMinimap({
|
||||
items,
|
||||
stripRefs,
|
||||
onSelect,
|
||||
}: {
|
||||
items: ReadonlyArray<TimelineMinimapItem>;
|
||||
stripRefs: React.RefObject<Map<string, HTMLButtonElement>>;
|
||||
onSelect: (item: TimelineMinimapItem) => void;
|
||||
}) {
|
||||
if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fallbackStep = items.length > 1 ? TIMELINE_MINIMAP_HEIGHT / (items.length - 1) : 0;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="pointer-events-none absolute top-1/2 left-[max(0.5rem,calc(50%-28rem))] z-20 hidden -translate-y-1/2 md:block"
|
||||
data-testid="timeline-minimap"
|
||||
>
|
||||
<div className="relative w-12 select-none" style={{ height: TIMELINE_MINIMAP_HEIGHT }}>
|
||||
<div className="absolute top-0 left-3 h-full w-px bg-border/15" />
|
||||
{items.map((item, index) => {
|
||||
const top = index * fallbackStep;
|
||||
return (
|
||||
<button
|
||||
aria-label={`Jump to message: ${item.userText ?? "User message"}`}
|
||||
className="pointer-events-auto group absolute left-0 flex h-4 w-12 -translate-y-1/2 cursor-pointer items-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/70"
|
||||
data-in-view="false"
|
||||
key={item.id}
|
||||
onClick={() => onSelect(item)}
|
||||
ref={(node) => {
|
||||
if (node) {
|
||||
stripRefs.current.set(item.id, node);
|
||||
} else {
|
||||
stripRefs.current.delete(item.id);
|
||||
}
|
||||
}}
|
||||
style={{ top }}
|
||||
type="button"
|
||||
>
|
||||
<span className="ml-0 h-px w-2.5 rounded-full bg-muted-foreground/35 transition-[background-color,width] duration-150 group-hover:w-7 group-hover:bg-muted-foreground/65 group-data-[in-view=true]:bg-foreground/90" />
|
||||
<span className="pointer-events-none absolute top-1/2 left-8 hidden w-80 -translate-y-1/2 rounded-xl border border-border/70 bg-popover/95 p-3 text-left text-popover-foreground shadow-xl shadow-black/25 backdrop-blur group-hover:block group-focus-visible:block">
|
||||
<span className="block truncate text-sm font-medium">
|
||||
{item.userText ?? "User message"}
|
||||
</span>
|
||||
{item.assistantText ? (
|
||||
<span className="mt-1 block line-clamp-3 text-muted-foreground text-sm">
|
||||
{item.assistantText}
|
||||
</span>
|
||||
) : null}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// TimelineRowContent — the actual row component
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Generated
+260
-262
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user