fix(webui): center prompt navigation in the complete chat frame
This commit is contained in:
1 parent
f230409b5a
commit
27ef6fe24f
7 files changed
+84
-15
No files matched your search
@@ -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 = <div style={{ height: parameters.has("tall-composer") ? 240 : 100, border: "1px solid #ddd", borderRadius: 20, padding: 20 }}>Message nanobot</div>;
|
||||
const viewport = (external: boolean, navigationFrame?: HTMLElement | null) => <ThreadViewport
|
||||
messages={messages} isStreaming={streaming} conversationKey="websocket:rail"
|
||||
navigation={parameters.has("short") ? undefined : navigation}
|
||||
navigationFrame={navigationFrame} composer={external ? null : composer} />;
|
||||
return <main style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
|
||||
{parameters.has("real") && <div>
|
||||
<button onClick={() => setVersion((value) => value + 1)}>Refresh index</button>
|
||||
<button onClick={() => setStreaming(true)} disabled={streaming}>{streaming ? "Streaming" : "Simulate stream"}</button>
|
||||
</div>}
|
||||
<ThreadViewport messages={messages} isStreaming={streaming} conversationKey="websocket:rail"
|
||||
navigation={parameters.has("short") ? undefined : navigation}
|
||||
composer={<div style={{ height: 100, border: "1px solid #ddd", borderRadius: 20, padding: 20 }}>Message nanobot</div>} />
|
||||
{parameters.has("workbench") ? <PaneWorkbench
|
||||
panes={[{ key: "rail", title: "Rail conversation" }]} activePaneKey="rail" layout="columns" showLayoutControl={false}
|
||||
onActivatePane={() => {}} onAddPane={() => {}} onLayoutChange={() => {}} onPaneOrderChange={() => {}}
|
||||
renderPane={(_pane, context) => <>
|
||||
{viewport(true, context.navigationFrame)}
|
||||
{context.composerPortalTarget && createPortal(composer, context.composerPortalTarget)}
|
||||
</>}
|
||||
/> : viewport(false)}
|
||||
</main>;
|
||||
}
|
||||
await initializeI18n();
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
@@ -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,
|
||||
|
||||
@@ -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<HTMLDivElement>;
|
||||
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<HTMLDivElement>(null);
|
||||
const frameRef = useRef<HTMLDivElement>(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 <div
|
||||
ref={frameRef}
|
||||
aria-label={t("thread.promptNavigator.railAria")}
|
||||
className="thread-prompt-rail pointer-events-auto absolute top-3 z-20 w-9"
|
||||
style={{ bottom: Math.max(16, bottomOffset) }}
|
||||
className="thread-prompt-rail pointer-events-auto absolute inset-y-[10%] z-20 w-9"
|
||||
style={frame ? { top: frame.center, bottom: "auto", height: frame.height, transform: "translateY(-50%)" } : undefined}
|
||||
onPointerEnter={() => { inside.current = true; }}
|
||||
onPointerLeave={() => { inside.current = false; setFocused(null); }}
|
||||
>
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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<ThreadViewportHandle, ThreadViewportPro
|
||||
runStartedAt = null,
|
||||
retryStatus = null,
|
||||
composer,
|
||||
navigationFrame,
|
||||
emptyState,
|
||||
scrollToBottomSignal = 0,
|
||||
activeTurnId: liveTurnId = null,
|
||||
@@ -1130,7 +1132,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
messages={messages}
|
||||
prompts={navigation?.prompts}
|
||||
scrollRef={scrollRef}
|
||||
bottomOffset={scrollButtonBottom}
|
||||
navigationFrame={navigationFrame}
|
||||
onJumpToPrompt={jumpToUserPrompt}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
@@ -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<HTMLElement | null>(null);
|
||||
const [composerPortalTarget, setComposerPortalTarget] = useState<HTMLElement | null>(null);
|
||||
const [navigationFrame, setNavigationFrame] = useState<HTMLElement | null>(null);
|
||||
const gridRef = useRef<HTMLDivElement | null>(null);
|
||||
const paneRefs = useRef(new Map<string, HTMLElement>());
|
||||
const lastRectsRef = useRef(new Map<string, DOMRect>());
|
||||
@@ -675,6 +677,7 @@ export function PaneWorkbench({
|
||||
|
||||
return (
|
||||
<section
|
||||
ref={setNavigationFrame}
|
||||
aria-label={t("workbench.aria", { defaultValue: "Conversation workbench" })}
|
||||
className={cn(
|
||||
"thread-workspace relative flex h-full min-h-0 flex-col overflow-hidden bg-background",
|
||||
@@ -726,6 +729,9 @@ export function PaneWorkbench({
|
||||
// Multiple conversation panes still share the workbench toolbar.
|
||||
headerPortalTarget: chrome && displayedPanes.length > 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 ? (
|
||||
|
||||
Reference in new issue
Block a user