From 27ef6fe24f4b750099ef23c2d178a1462c3a259d Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sun, 18 Oct 2026 16:58:17 +0800 Subject: [PATCH] fix(webui): center prompt navigation in the complete chat frame --- webui/browser/fixture.tsx | 18 +++++++-- webui/browser/prompt-rail.spec.ts | 30 +++++++++++++-- webui/src/App.tsx | 1 + webui/src/components/thread/PromptRail.tsx | 37 +++++++++++++++---- webui/src/components/thread/ThreadShell.tsx | 3 ++ .../src/components/thread/ThreadViewport.tsx | 4 +- .../components/workbench/PaneWorkbench.tsx | 6 +++ 7 files changed, 84 insertions(+), 15 deletions(-) diff --git a/webui/browser/fixture.tsx b/webui/browser/fixture.tsx index 184aee344..aa1132e9f 100644 --- a/webui/browser/fixture.tsx +++ b/webui/browser/fixture.tsx @@ -1,8 +1,10 @@ import { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; +import { createPortal } from "react-dom"; import { MessageBubble } from "@/components/MessageBubble"; import { ThreadMessages } from "@/components/thread/ThreadMessages"; import { ThreadViewport } from "@/components/thread/ThreadViewport"; +import { PaneWorkbench } from "@/components/workbench/PaneWorkbench"; import { usePromptHistory } from "@/hooks/usePromptHistory"; import { ClientProvider } from "@/providers/ClientProvider"; import { NanobotClient } from "@/lib/nanobot-client"; @@ -82,14 +84,24 @@ function RailFixture() { return () => window.clearInterval(timer); }, [streaming]); const navigation = usePromptHistory(messages.length ? "websocket:rail" : null, version, messages); + const composer =
Message nanobot
; + const viewport = (external: boolean, navigationFrame?: HTMLElement | null) => ; return
{parameters.has("real") &&
} - Message nanobot} /> + {parameters.has("workbench") ? {}} onAddPane={() => {}} onLayoutChange={() => {}} onPaneOrderChange={() => {}} + renderPane={(_pane, context) => <> + {viewport(true, context.navigationFrame)} + {context.composerPortalTarget && createPortal(composer, context.composerPortalTarget)} + } + /> : viewport(false)}
; } await initializeI18n(); diff --git a/webui/browser/prompt-rail.spec.ts b/webui/browser/prompt-rail.spec.ts index eef12f9ae..f32fcb648 100644 --- a/webui/browser/prompt-rail.spec.ts +++ b/webui/browser/prompt-rail.spec.ts @@ -16,10 +16,17 @@ test("complete rail scrolls independently, jumps to unloaded history and returns has_more_before: start > 0, has_more_after: start + 10 < 2000 } } }); }); await page.setViewportSize({ width: 1440, height: 900 }); - await page.goto("/browser/fixture.html?case=rail"); + await page.goto("/browser/fixture.html?case=rail&workbench=1"); const rail = page.getByTestId("prompt-rail-scroll"); const body = page.getByTestId("thread-message-region"); await expect(rail).toBeVisible(); + // Center against the complete chat pane, not the rail's own (possibly shifted) box. + const expectRailCentered = async () => { + const pane = await page.getByRole("region", { name: "Workbench", exact: true }).boundingBox(); + const bounds = await rail.boundingBox(); + expect(Math.abs(bounds!.y + bounds!.height / 2 - (pane!.y + pane!.height / 2))).toBeLessThan(2); + }; + await expectRailCentered(); await expect.poll(() => rail.evaluate((node) => node.scrollHeight)).toBe(32024); expect(reads).toHaveLength(0); // Lazy markdown and content-visibility can change the scroll range during startup. @@ -64,11 +71,28 @@ test("complete rail scrolls independently, jumps to unloaded history and returns expect(await rail.evaluate((node) => getComputedStyle(node).maskImage)).toContain("linear-gradient"); await page.screenshot({ path: "test-results/prompt-rail-long.png" }); // Short lists remain centered in the same viewport-relative space. - await page.goto("/browser/fixture.html?case=rail&short=1"); + await page.goto("/browser/fixture.html?case=rail&short=1&workbench=1"); const markers = page.getByTestId("prompt-rail-marker"); await expect(markers).toHaveCount(5); - const bounds = await rail.boundingBox(); + await expectRailCentered(); + const bounds = await page.getByRole("region", { name: "Workbench", exact: true }).boundingBox(); const middle = await markers.nth(2).boundingBox(); expect(Math.abs(middle!.y + middle!.height / 2 - (bounds!.y + bounds!.height / 2))).toBeLessThan(2); await page.screenshot({ path: "test-results/prompt-rail-short.png" }); + // A taller composer in a shorter window must not push navigation above center. + await page.setViewportSize({ width: 1440, height: 640 }); + await page.goto("/browser/fixture.html?case=rail&short=1&tall-composer=1&workbench=1"); + await expect(markers).toHaveCount(5); + await expectRailCentered(); + const shortBounds = await page.getByRole("region", { name: "Workbench", exact: true }).boundingBox(); + const shortMiddle = await markers.nth(2).boundingBox(); + expect(Math.abs(shortMiddle!.y + shortMiddle!.height / 2 - (shortBounds!.y + shortBounds!.height / 2))).toBeLessThan(2); + await expect(markers.first()).toBeInViewport(); + await expect(markers.last()).toBeInViewport(); + await page.setViewportSize({ width: 800, height: 900 }); + await page.goto("/browser/fixture.html?case=rail&short=1&workbench=1"); + await expect(rail).not.toBeVisible(); + await page.setViewportSize({ width: 1440, height: 900 }); + await expect(rail).toBeVisible(); + await expectRailCentered(); }); diff --git a/webui/src/App.tsx b/webui/src/App.tsx index d8ac957e3..52c12822f 100644 --- a/webui/src/App.tsx +++ b/webui/src/App.tsx @@ -3066,6 +3066,7 @@ function Shell({ headerPortalTarget={context.headerPortalTarget} headerActive={context.active} composerPortalTarget={context.composerPortalTarget} + navigationFrame={context.navigationFrame} composerActive={context.active} composerInputAriaLabel={t("workbench.composerAria", { title: pane.title, diff --git a/webui/src/components/thread/PromptRail.tsx b/webui/src/components/thread/PromptRail.tsx index 901b3e15f..dc1333719 100644 --- a/webui/src/components/thread/PromptRail.tsx +++ b/webui/src/components/thread/PromptRail.tsx @@ -1,4 +1,4 @@ -import { memo, type RefObject, useEffect, useMemo, useRef, useState } from "react"; +import { memo, type RefObject, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { MarkdownText } from "@/components/MarkdownText"; import { floatingSurfaceElevationClassName } from "@/components/ui/floating-surface"; @@ -7,11 +7,11 @@ import type { UIMessage } from "@/lib/types"; import { type PromptAnchor, userPromptAnchors } from "./promptNavigation"; interface PromptRailProps { - bottomOffset: number; messages: UIMessage[]; prompts?: PromptAnchor[]; onJumpToPrompt: (promptId: string) => void; scrollRef: RefObject; + navigationFrame?: HTMLElement | null; } const GAP = 16; @@ -19,13 +19,15 @@ const EDGE = 20; const WIDTHS = [28, 22, 16, 11]; /** Complete logical list; only the visible markers and one preview are mounted. */ -export const PromptRail = memo(function PromptRail({ bottomOffset, messages, prompts, onJumpToPrompt, scrollRef }: PromptRailProps) { +export const PromptRail = memo(function PromptRail({ messages, prompts, onJumpToPrompt, scrollRef, navigationFrame }: PromptRailProps) { const { t } = useTranslation(); const anchors = useMemo(() => prompts ?? userPromptAnchors(messages), [prompts, messages]); const anchorPositions = useMemo(() => new Map(anchors.map((anchor, index) => [anchor.id, index])), [anchors]); // Assistant token updates do not change which user elements need observation. const userIds = messages.filter((message) => message.role === "user").map((message) => message.id).join("\n"); const railRef = useRef(null); + const frameRef = useRef(null); + const [frame, setFrame] = useState<{ center: number; height: number } | null>(null); const inside = useRef(false); const [height, setHeight] = useState(300); const [scrollTop, setScrollTop] = useState(0); @@ -36,15 +38,33 @@ export const PromptRail = memo(function PromptRail({ bottomOffset, messages, pro const first = Math.max(0, Math.floor((scrollTop - padding) / GAP) - 4); const end = Math.min(anchors.length, Math.ceil((scrollTop + height - padding) / GAP) + 5); - useEffect(() => { + useLayoutEffect(() => { const rail = railRef.current; if (!rail) return; - const measure = () => setHeight(rail.clientHeight || 300); + // The rail is a direct child of ThreadViewport. Unlike offsetParent this + // remains available while the narrow-pane CSS hides the rail. + const pane = frameRef.current?.parentElement; + const measure = () => { + if (navigationFrame && pane) { + const target = navigationFrame.getBoundingClientRect(); + const bounds = pane.getBoundingClientRect(); + const center = target.top + target.height / 2 - bounds.top; + // Keep the centered rail inside the transcript, even when a shared + // composer outside this viewport grows. It must not be clipped below. + const height = Math.max(0, Math.min(target.height * 0.8, 2 * (Math.min(center, bounds.height - center) - 12))); + setFrame((previous) => previous?.center === center && previous.height === height ? previous : { center, height }); + } else setFrame(null); + setHeight(rail.clientHeight || 300); + }; measure(); const observer = new ResizeObserver(measure); observer.observe(rail); + if (navigationFrame && pane) { + observer.observe(navigationFrame); + observer.observe(pane); + } return () => observer.disconnect(); - }, [anchors.length >= 2, bottomOffset]); + }, [anchors.length >= 2, navigationFrame]); useEffect(() => { const scroller = scrollRef.current; @@ -117,9 +137,10 @@ export const PromptRail = memo(function PromptRail({ bottomOffset, messages, pro }; return
{ inside.current = true; }} onPointerLeave={() => { inside.current = false; setFocused(null); }} > diff --git a/webui/src/components/thread/ThreadShell.tsx b/webui/src/components/thread/ThreadShell.tsx index 239bbf053..9448f0684 100644 --- a/webui/src/components/thread/ThreadShell.tsx +++ b/webui/src/components/thread/ThreadShell.tsx @@ -538,6 +538,7 @@ interface ThreadShellProps { headerPortalTarget?: HTMLElement | null; headerActive?: boolean; composerPortalTarget?: HTMLElement | null; + navigationFrame?: HTMLElement | null; composerActive?: boolean; composerInputAriaLabel?: string; emptyComposerVariant?: "hero" | "thread"; @@ -756,6 +757,7 @@ export function ThreadShell({ headerPortalTarget, headerActive = true, composerPortalTarget, + navigationFrame, composerActive = true, composerInputAriaLabel, emptyComposerVariant = "hero", @@ -2077,6 +2079,7 @@ export function ThreadShell({ ref={viewportRef} messages={displayMessages} navigation={promptNavigation} + navigationFrame={navigationFrame} temporary={temporary} isStreaming={turnActive} runStartedAt={currentRunStartedAt} diff --git a/webui/src/components/thread/ThreadViewport.tsx b/webui/src/components/thread/ThreadViewport.tsx index 90f87d6f3..fcafa9fd1 100644 --- a/webui/src/components/thread/ThreadViewport.tsx +++ b/webui/src/components/thread/ThreadViewport.tsx @@ -44,6 +44,7 @@ interface ThreadViewportProps { runStartedAt?: number | null; retryStatus?: RetryStatus | null; composer?: ReactNode; + navigationFrame?: HTMLElement | null; emptyState?: ReactNode; scrollToBottomSignal?: number; activeTurnId?: string | null; @@ -241,6 +242,7 @@ export const ThreadViewport = forwardRef ) : null} diff --git a/webui/src/components/workbench/PaneWorkbench.tsx b/webui/src/components/workbench/PaneWorkbench.tsx index c7ba6fffd..d75f236a0 100644 --- a/webui/src/components/workbench/PaneWorkbench.tsx +++ b/webui/src/components/workbench/PaneWorkbench.tsx @@ -53,6 +53,7 @@ interface PaneRenderContext { active: boolean; headerPortalTarget: HTMLElement | null | undefined; composerPortalTarget: HTMLElement | null | undefined; + navigationFrame: HTMLElement | null | undefined; headerActions: ReactNode; } @@ -208,6 +209,7 @@ export function PaneWorkbench({ const effectiveLayout = layout; const [headerPortalTarget, setHeaderPortalTarget] = useState(null); const [composerPortalTarget, setComposerPortalTarget] = useState(null); + const [navigationFrame, setNavigationFrame] = useState(null); const gridRef = useRef(null); const paneRefs = useRef(new Map()); const lastRectsRef = useRef(new Map()); @@ -675,6 +677,7 @@ export function PaneWorkbench({ return (
1 ? headerPortalTarget : undefined, composerPortalTarget: chrome ? composerPortalTarget : undefined, + // A single chat includes the shared composer in its visual frame. + // Split chats keep navigation centered within their own pane. + navigationFrame: displayedPanes.length === 1 ? navigationFrame : undefined, headerActions, })} {chrome && panes.length > 1 && !compact ? (