fix(webui): add a close control to the expanded context panel (#6629)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-11 17:06:45 +08:00
1 parent 27f1a836b5
commit e3a1af2ed3
5 files changed
+32 -9

No files matched your search

+9
View File
@@ -53,6 +53,15 @@ test("graph keyboard controls, pointer hit targets, pixels and replay agree", as
await expect(page.getByRole("progressbar")).toHaveAttribute("aria-valuenow", "20");
});
test("closes the context sidebar from its own control", async ({ page }) => {
await page.goto("/browser/context-graph.html?single&drawer");
await page.getByTestId("composer-context-usage").click();
const sidebar = page.getByTestId("context-sidebar");
await expect(sidebar).toBeVisible();
await sidebar.getByRole("button", { name: "Close" }).click();
await expect(sidebar).toHaveCount(0);
});
test.describe("touch graph", () => {
test.use({ viewport: { width: 390, height: 740 }, hasTouch: true });
test("recovers batched drawer visibility, replays and opens a file by touch", async ({ page }) => {
@@ -176,7 +176,7 @@ export function ComposerUsagePopover({
};
const inspector = <ContextInspector key={sessionKey} sidebar expandable={!narrow} context={context} rounds={rounds} snapshots={snapshots}
onOpenFile={onOpenFile ? (path) => { setOpen(false); onOpenFile(path); } : undefined} />;
onClose={narrow ? undefined : close} onOpenFile={onOpenFile ? (path) => { setOpen(false); onOpenFile(path); } : undefined} />;
const onPanelKeyDown = (event: KeyboardEvent<HTMLElement>) => {
if (event.key === "Escape" && !event.defaultPrevented) { event.preventDefault(); close(); }
};
@@ -1,6 +1,6 @@
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useReducedMotion } from "motion/react";
import { Maximize2, Minimize2 } from "lucide-react";
import { Maximize2, Minimize2, X } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Popover, PopoverAnchor, PopoverContent } from "@/components/ui/popover";
import { cn } from "@/lib/utils";
@@ -12,8 +12,8 @@ import { paintGraph, readGraphPalette, type GraphPalette } from "./graph-canvas"
const surface = "context-inspector-surface select-none rounded-[20px] bg-card";
export const ContextGraphScene = memo(function ContextGraphScene({ graph, epoch, expanded, onExpand, onOpenFile, onReady, sidebar = false }: {
graph: ContextGraph; epoch?: string; expanded: boolean; onExpand?: () => void; onOpenFile?: (path: string) => void; onReady?: (graph: ContextGraph) => void; sidebar?: boolean;
export const ContextGraphScene = memo(function ContextGraphScene({ graph, epoch, expanded, onExpand, onClose, onOpenFile, onReady, sidebar = false }: {
graph: ContextGraph; epoch?: string; expanded: boolean; onExpand?: () => void; onClose?: () => void; onOpenFile?: (path: string) => void; onReady?: (graph: ContextGraph) => void; sidebar?: boolean;
}) {
const { t } = useTranslation();
const container = useRef<HTMLDivElement>(null);
@@ -222,7 +222,7 @@ export const ContextGraphScene = memo(function ContextGraphScene({ graph, epoch,
onBlur={() => { focused.current = null; setHovered(null); schedule(); }}
onClick={() => setActive((path) => path === node.path ? null : node.path)}>{node.name}</button>)}
</div>), [graph.nodes, active, schedule]);
return <div ref={container} className={cn(surface, "relative isolate overflow-clip touch-none cursor-grab active:cursor-grabbing", sidebar ? "min-h-0 flex-1" : expanded ? "h-[min(65dvh,600px)]" : "h-[300px]")}
return <div ref={container} className={cn(surface, "relative isolate overflow-clip touch-none cursor-grab active:cursor-grabbing", sidebar ? "min-h-0 flex-1" : expanded ? "h-[min(82dvh,900px)]" : "h-[300px]")}
onPointerDown={(e) => {
if (!(e.target instanceof Node) || !e.currentTarget.contains(e.target)) return;
if (e.button !== 0 || !e.isPrimary || (e.target instanceof Element && e.target.closest("button"))) return;
@@ -273,6 +273,10 @@ export const ContextGraphScene = memo(function ContextGraphScene({ graph, epoch,
interacted.current = false; setView({ k, x: size.width / 2 - (left + right) / 2 * k, y: (size.height + 22) / 2 - (top + bottom) / 2 * k });
}}>
<span ref={paletteProbe} aria-hidden className="invisible absolute bg-background text-muted-foreground" />
{onClose && <button type="button" onClick={onClose} aria-label={t("common.close")}
className="absolute left-2 top-2 z-10 flex size-9 items-center justify-center rounded-full bg-background/90 text-muted-foreground hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
<X size={15} />
</button>}
{onExpand && <button type="button" onClick={onExpand} aria-label={t(expanded ? "thread.composer.context.collapse" : "thread.composer.context.expand")}
className="absolute right-2 top-2 z-10 flex size-9 items-center justify-center rounded-full bg-background/90 text-muted-foreground hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
{expanded ? <Minimize2 size={15} /> : <Maximize2 size={15} />}
@@ -21,9 +21,9 @@ function latestUsageSnapshot(snapshots: readonly ContextSnapshot[]) {
}
}
export function ContextInspector({ context, rounds, snapshots, onOpenFile, sidebar = false, expandable = true }: {
export function ContextInspector({ context, rounds, snapshots, onOpenFile, sidebar = false, expandable = true, onClose }: {
context: ComposerContextUsage | null; rounds: readonly ComposerRoundUsage[];
snapshots: readonly ContextSnapshot[]; onOpenFile?: (path: string) => void; sidebar?: boolean; expandable?: boolean;
snapshots: readonly ContextSnapshot[]; onOpenFile?: (path: string) => void; sidebar?: boolean; expandable?: boolean; onClose?: () => void;
}) {
const { t, i18n } = useTranslation();
const layoutId = useId();
@@ -63,7 +63,7 @@ export function ContextInspector({ context, rounds, snapshots, onOpenFile, sideb
const openFile = useCallback((path: string) => { setExpanded(false); openFileCallback.current?.(path); }, []);
const content = (large: boolean) => <motion.div layoutId={layoutId} transition={{ duration: reducedMotion ? 0 : 0.25, ease: [0.2, 0, 0, 1] }} className={cn("flex flex-col gap-3", sidebar && !large && "h-full min-h-0")}>
<Suspense fallback={<div aria-busy="true" className={cn(surface, "min-h-[300px]", sidebar && !large && "min-h-0 flex-1")} />}>
<ContextGraphScene graph={snapshot?.graph ?? EMPTY_GRAPH} epoch={epoch} onReady={setReadyGraph} expanded={large} sidebar={sidebar && !large} onExpand={expandable ? toggleExpanded : undefined} onOpenFile={onOpenFile ? openFile : undefined} />
<ContextGraphScene graph={snapshot?.graph ?? EMPTY_GRAPH} epoch={epoch} onReady={setReadyGraph} expanded={large} sidebar={sidebar && !large} onExpand={expandable ? toggleExpanded : undefined} onClose={large ? undefined : onClose} onOpenFile={onOpenFile ? openFile : undefined} />
</Suspense>
<div className="flex shrink-0 items-center gap-3">
<div onPointerEnter={() => setMeterHover(true)} onPointerLeave={() => setMeterHover(false)} className={cn(surface, "relative flex h-14 min-w-0 flex-1 items-center gap-4 px-4 transition-transform duration-150 ease-out motion-reduce:transition-none", meterHover && "scale-[1.015]")}>
@@ -99,7 +99,7 @@ export function ContextInspector({ context, rounds, snapshots, onOpenFile, sideb
return <LayoutGroup>
{!expanded && content(false)}
<Dialog open={expanded} onOpenChange={setExpanded}>
<DialogContent showCloseButton={false} aria-describedby={undefined} overlayClassName="context-expanded-overlay bg-background/30 backdrop-blur-md" className="w-[min(960px,calc(100vw-2rem))] max-w-none border-0 bg-transparent p-0 shadow-none !animate-none">
<DialogContent showCloseButton={false} aria-describedby={undefined} overlayClassName="context-expanded-overlay bg-background/30 backdrop-blur-md" className="w-[min(1440px,calc(100vw-2rem))] max-w-none border-0 bg-transparent p-0 shadow-none !animate-none">
<DialogTitle className="sr-only">{t("thread.composer.context.panelTitle")}</DialogTitle>
{content(true)}
</DialogContent>
@@ -35,6 +35,16 @@ describe("Context sidebar", () => {
await waitFor(() => expect(screen.getByTestId("composer-context-usage")).toHaveFocus());
});
it("closes the sidebar from its own control", async () => {
const user = userEvent.setup();
render(<ComposerUsagePopover context={null} rounds={[]} snapshots={[first]} />);
await user.click(screen.getByTestId("composer-context-usage"));
const sidebar = screen.getByRole("complementary", { name: "Context usage" });
await user.click(within(sidebar).getByRole("button", { name: "Close" }));
await waitFor(() => expect(screen.queryByRole("complementary")).not.toBeInTheDocument());
expect(screen.getByTestId("composer-context-usage")).toHaveFocus();
});
it("mounts in the conversation's side slot and closes with Escape from its controls", async () => {
const user = userEvent.setup();
const host = document.createElement("section");