fix(webui): show latest button for hidden content (#6605)

* fix(webui): show latest button for hidden content

* fix(webui): preserve latest navigation after focus scroll
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-09 10:56:26 +08:00
1 parent c5f2ce5089
commit f62dfe0ee8
7 files changed
+243 -23

No files matched your search

+56 -20
View File
@@ -313,6 +313,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const composerLayoutChangeRef = useRef(onComposerLayoutChange);
composerLayoutChangeRef.current = onComposerLayoutChange;
const bottomRef = useRef<HTMLDivElement>(null);
const endGapRef = useRef<HTMLDivElement>(null);
const lastConversationKeyRef = useRef<string | null>(conversationKey);
const conversationHandoffPendingRef = useRef(false);
const conversationHandoffAnimationRef = useRef<Animation | null>(null);
@@ -323,7 +324,27 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const historyScrollAnchorRef = useRef<HistoryScrollAnchor | null>(null);
const previousLoadingOlderRef = useRef(loadingOlder);
const composerDockHeightRef = useRef(0);
const [atBottom, setAtBottom] = useState(true);
const [contentBelowViewport, setContentBelowViewport] = useState(false);
const [navigatingToBottom, setNavigatingToBottom] = useState(false);
const lastMessageRowRef = useRef<HTMLElement | null>(null);
const refreshLastMessageRow = useCallback(() => {
const rows = messageContentRef.current?.querySelectorAll<HTMLElement>(".thread-message-row");
lastMessageRowRef.current = null;
if (!rows) return;
for (let index = rows.length - 1; index >= 0; index--) {
if (rows[index].getBoundingClientRect().height > 0) {
lastMessageRowRef.current = rows[index];
break;
}
}
}, []);
const updateContentVisibility = useCallback(() => {
const viewport = messageRegionRef.current;
const lastRow = lastMessageRowRef.current;
setContentBelowViewport(Boolean(viewport && lastRow
&& lastRow.getBoundingClientRect().bottom > viewport.getBoundingClientRect().bottom
- (endGapRef.current?.getBoundingClientRect().height ?? 0)));
}, []);
const [atHistoryTop, setAtHistoryTop] = useState(false);
const [historyPhase, setHistoryPhase] = useState<HistoryRevealPhase>("hidden");
const historyPhaseRef = useRef(historyPhase);
@@ -380,7 +401,9 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
useState(INITIAL_HISTORY_WINDOW);
const threadMotionRef = useRef<ThreadMotionCoordinator | null>(null);
if (threadMotionRef.current === null) {
const camera = new ThreadCameraController(() => scrollRef.current);
const camera = new ThreadCameraController(() => scrollRef.current, {
onSettled: () => threadMotionRef.current?.cameraSettled(),
});
threadMotionRef.current = new ThreadMotionCoordinator({
camera,
measure: (promptId) => {
@@ -408,6 +431,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
: null,
};
},
onLatestNavigationSettled: () => {
setNavigatingToBottom(false);
updateContentVisibility();
},
onGeometry: (geometry: ThreadMotionGeometry) => {
composerLayoutChangeRef.current?.();
if (Math.abs(composerDockHeightRef.current - geometry.composerHeight) >= 1) {
@@ -419,7 +446,6 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
current === nextOverflow ? current : nextOverflow,
);
},
onAutoFollow: () => setAtBottom(true),
});
}
const hasTaskResults = useHasSubagentResults();
@@ -461,7 +487,9 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const yieldCameraToUser = useCallback(() => {
threadMotionRef.current?.takeUserControl();
}, []);
setNavigatingToBottom(false);
updateContentVisibility();
}, [updateContentVisibility]);
const captureHistoryScrollAnchor = useCallback(() => {
const scroller = scrollRef.current;
@@ -542,14 +570,14 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
if (el) {
const top = Math.max(0, el.scrollHeight - el.clientHeight);
if (smooth) {
threadMotionRef.current?.navigateLatestTo(top);
const result = threadMotionRef.current?.navigateLatestTo(top);
setNavigatingToBottom(result === "started" || result === "retargeted");
} else {
threadMotionRef.current?.jumpTo(top);
}
} else if (marker) {
marker.scrollIntoView({ block: "end", behavior });
}
setAtBottom(true);
}, [resetHistoryHeader]);
const scrollToBottom = useCallback(
@@ -576,7 +604,6 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
}
}
threadMotionRef.current?.takeUserControl();
setAtBottom(false);
if (hiddenMessageCount > 0) {
setVisibleMessageCount((count) =>
Math.min(messages.length, count + HISTORY_WINDOW_INCREMENT),
@@ -615,7 +642,6 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
if (!scrollEl || !prompt) return false;
historyScrollAnchorRef.current = null;
resetHistoryHeader();
setAtBottom(false);
threadMotionRef.current?.navigateHistoryToPrompt(promptId);
return true;
}, [resetHistoryHeader]);
@@ -636,7 +662,6 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
if (historyWindow) navigation?.latest();
threadMotionRef.current?.takeUserControl();
pendingPromptJumpRef.current = promptId;
setAtBottom(false);
setVisibleMessageCount((count) => Math.max(count, localMessages.length - index));
}, [historyWindow, latestMessages, messages, navigateToVisiblePrompt, navigation]);
@@ -647,7 +672,6 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
pendingConversationScrollRef.current = false;
pendingPromptJumpRef.current = navigation.target.id;
threadMotionRef.current?.takeUserControl();
setAtBottom(false);
const index = messages.findIndex((message) => message.id === navigation.target!.id);
setVisibleMessageCount(Math.max(INITIAL_HISTORY_WINDOW, messages.length - Math.max(0, index)));
}, [navigation?.target]);
@@ -658,7 +682,9 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const cancelAutoScroll = useCallback(() => {
threadMotionRef.current?.takeUserControl();
}, []);
setNavigatingToBottom(false);
updateContentVisibility();
}, [updateContentVisibility]);
useImperativeHandle(
ref,
@@ -724,7 +750,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
historyScrollAnchorRef.current = null;
restoreScrollAfterPrependRef.current = null;
threadMotionRef.current?.reset();
setAtBottom(true);
setNavigatingToBottom(false);
setShowHistoryExhausted(false);
setVisibleMessageCount(INITIAL_HISTORY_WINDOW);
@@ -878,8 +904,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
}, [conversationReady, hasMessages]);
useLayoutEffect(() => {
refreshLastMessageRow();
updateContentVisibility();
threadMotionRef.current?.invalidateGeometry();
}, [composer, hasMessages, visibleMessages.length]);
}, [composer, hasMessages, visibleMessages, refreshLastMessageRow, updateContentVisibility]);
useEffect(() => () => {
conversationHandoffAnimationRef.current?.cancel();
@@ -897,11 +925,15 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
if (!el) return;
const invalidateGeometry = () => {
refreshLastMessageRow();
threadMotionRef.current?.invalidateGeometry();
updateContentVisibility();
};
const reconcileObservedGeometry = () => {
refreshLastMessageRow();
reconcileHistoryScrollAnchor();
threadMotionRef.current?.reconcileObservedGeometry();
updateContentVisibility();
};
reconcileObservedGeometry();
const observer = typeof ResizeObserver === "undefined"
@@ -911,29 +943,28 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
if (content) observer?.observe(content);
if (contentRegion) observer?.observe(contentRegion);
if (messageContent) observer?.observe(messageContent);
if (endGapRef.current) observer?.observe(endGapRef.current);
if (composerDock) observer?.observe(composerDock);
window.addEventListener("resize", invalidateGeometry);
return () => {
observer?.disconnect();
window.removeEventListener("resize", invalidateGeometry);
};
}, [hasMessages, reconcileHistoryScrollAnchor]);
}, [hasMessages, reconcileHistoryScrollAnchor, refreshLastMessageRow, updateContentVisibility]);
useEffect(() => {
const el = scrollRef.current;
if (!el) return;
const onScroll = (allowHistoryLoad = true) => {
updateContentVisibility();
setAtHistoryTop(el.scrollTop <= 1);
if (el.scrollTop > 1) historySpringRef.current?.leaveBoundary();
const distance = el.scrollHeight - el.scrollTop - el.clientHeight;
const near = distance < NEAR_BOTTOM_PX;
// A detached history page's bottom is not the live tail.
const owner = threadMotionRef.current?.observeScroll(near && !historyWindow) ?? "automatic";
const logicallyAtBottom = !historyWindow && (owner === "automatic" || (owner === "navigation" && near));
setAtBottom((current) =>
current === logicallyAtBottom ? current : logicallyAtBottom,
);
if (threadMotionRef.current?.snapshot().mode !== "navigating-latest") setNavigatingToBottom(false);
if (owner === "user") {
// Restoring the anchor also emits scroll. Keep that reading position
// through lazy content resizing instead of selecting a prepended row.
@@ -978,6 +1009,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
canScrollInDirection(el, direction),
direction === "forward" && !historyWindow,
);
if (threadMotionRef.current?.snapshot().mode !== "navigating-latest") {
setNavigatingToBottom(false);
updateContentVisibility();
}
};
const prepareHistoryPull = () => {
if (loadingOlder || navigation?.loading) return;
@@ -1149,6 +1184,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
messages.length,
olderError,
yieldCameraToUser,
updateContentVisibility,
historySlotKey,
historyWindow,
navigation,
@@ -1244,7 +1280,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
</DiagramScope>
</DiffViewportContext.Provider>
</div>
<div aria-hidden className="thread-message-end-gap shrink-0" />
<div ref={endGapRef} aria-hidden className="thread-message-end-gap shrink-0" />
<div ref={bottomRef} aria-hidden className="h-px shrink-0" />
</div>
) : (
@@ -1319,7 +1355,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
/>
) : null}
{showScrollToBottomButton && (!atBottom || historyWindow) && (
{showScrollToBottomButton && (historyWindow || (contentBelowViewport && !navigatingToBottom)) && (
<div
className="absolute left-1/2 z-20 -translate-x-1/2"
style={{ bottom: scrollButtonBottom }}
@@ -18,6 +18,7 @@ export type ThreadCameraFollowResult = "started" | "retargeted" | "settled";
interface ThreadCameraOptions {
scheduler?: ThreadCameraScheduler;
prefersReducedMotion?: () => boolean;
onSettled?: () => void;
}
function defaultScheduler(): ThreadCameraScheduler {
@@ -38,6 +39,7 @@ export class ThreadCameraController {
private readonly getViewport: () => ThreadCameraViewport | null;
private readonly scheduler: ThreadCameraScheduler;
private readonly prefersReducedMotion: () => boolean;
private readonly onSettled?: () => void;
private frameId: number | null = null;
private phase: "idle" | "following" = "idle";
private target = 0;
@@ -50,6 +52,7 @@ export class ThreadCameraController {
) {
this.getViewport = getViewport;
this.scheduler = options.scheduler ?? defaultScheduler();
this.onSettled = options.onSettled;
this.prefersReducedMotion = options.prefersReducedMotion ?? defaultPrefersReducedMotion;
}
@@ -132,6 +135,7 @@ export class ThreadCameraController {
if (remainingMs === 0) {
this.write(viewport, this.target);
this.cancel();
this.onSettled?.();
return;
}
@@ -44,6 +44,7 @@ const THREAD_MOTION_TRANSITIONS: Readonly<
"resume-follow": "current-automatic-mode",
},
"follow-latest": {
"navigate-latest": "navigating-latest",
"navigate-history": "navigating-history",
"user-scroll": "browsing-history",
},
@@ -134,6 +135,7 @@ interface ThreadMotionCoordinatorOptions {
measure: (promptId: string | null) => ThreadMotionGeometry | null;
onGeometry?: (geometry: ThreadMotionGeometry) => void;
onAutoFollow?: () => void;
onLatestNavigationSettled?: () => void;
scheduler?: ThreadMotionScheduler;
}
@@ -159,6 +161,7 @@ export class ThreadMotionCoordinator {
private readonly measure: ThreadMotionCoordinatorOptions["measure"];
private readonly onGeometry?: ThreadMotionCoordinatorOptions["onGeometry"];
private readonly onAutoFollow?: ThreadMotionCoordinatorOptions["onAutoFollow"];
private readonly onLatestNavigationSettled?: () => void;
private readonly scheduler: ThreadMotionScheduler;
private turn: ThreadMotionTurn = {
id: null,
@@ -180,6 +183,7 @@ export class ThreadMotionCoordinator {
this.measure = options.measure;
this.onGeometry = options.onGeometry;
this.onAutoFollow = options.onAutoFollow;
this.onLatestNavigationSettled = options.onLatestNavigationSettled;
this.scheduler = options.scheduler ?? defaultScheduler();
}
@@ -330,6 +334,13 @@ export class ThreadMotionCoordinator {
return result;
}
cameraSettled(): void {
if (this.mode === "navigating-latest") {
this.settleLatestNavigation();
this.onLatestNavigationSettled?.();
}
}
isAutoFollowPaused(): boolean {
return this.isHistoryMode();
}