diff --git a/packages/webui/src/client/platform/browser-dom.ts b/packages/webui/src/client/platform/browser-dom.ts index d741be11c..45f729638 100644 --- a/packages/webui/src/client/platform/browser-dom.ts +++ b/packages/webui/src/client/platform/browser-dom.ts @@ -95,6 +95,14 @@ export interface WebuiBrowserDom { readonly portalTarget: () => HTMLElement | null; readonly listenForMouseDown: (listener: (event: MouseEvent) => void) => () => void; readonly listenForPointerDown: (listener: (event: PointerEvent) => void, capture?: boolean) => () => void; + /** + * A click anywhere in the document. + * + * A rail row is a plain anchor, so the surface that owns the shell hears + * the clicks the row cannot report itself. The listener is the document's, + * and it is the platform's: a surface does not reach for the document. + */ + readonly listenForClick: (listener: (event: MouseEvent) => void, capture?: boolean) => () => void; readonly listenForKeyDown: (listener: (event: KeyboardEvent) => void) => () => void; /** * The viewport was resized. @@ -241,6 +249,7 @@ export function createWebuiBrowserDom( portalTarget: () => documentObject?.body ?? null, listenForMouseDown: (listener) => listen("mousedown", listener), listenForPointerDown: (listener, capture) => listen("pointerdown", listener, capture), + listenForClick: (listener, capture) => listen("click", listener, capture), listenForKeyDown: (listener) => listen("keydown", listener), listenForResize: (listener) => { if (!windowObject) return () => undefined; diff --git a/packages/webui/src/client/shell/WebuiClientFoundationApp.tsx b/packages/webui/src/client/shell/WebuiClientFoundationApp.tsx index 222161d79..a29e94831 100644 --- a/packages/webui/src/client/shell/WebuiClientFoundationApp.tsx +++ b/packages/webui/src/client/shell/WebuiClientFoundationApp.tsx @@ -386,6 +386,24 @@ export function WebuiClientFoundationApp( useEffect(() => { dispatchShellSurface({ type: "show-conversation" }); }, [dispatchShellSurface, selectedSessionId]); + // Clicking the rail link of the session that is already selected changes + // nothing the hash subscription can observe: the hash is the same, so no + // `hashchange` fires, the selection does not change, and the effect above + // does not run again -- the shell keeps the surface it was showing (the + // marketplace, the 远程 page) although the user asked for the conversation. + // The click is the one signal left, so the shell listens for it on the rail's + // session links and puts the conversation back. + useEffect(() => { + if (!selectedSessionId) return; + return browserDom.listenForClick((event) => { + const target = event.target; + if (!(target instanceof Element)) return; + const link = target.closest("[data-webui-session-link]"); + if (!link) return; + if (link.getAttribute("data-webui-session-link") !== selectedSessionId) return; + dispatchShellSurface({ type: "show-conversation" }); + }, true); + }, [browserDom, dispatchShellSurface, selectedSessionId]); // Composer input history + per-session drafts. The one owner is the application // composer store: the shell subscribes to it and submits through it. const composerState = useSyncExternalStore( diff --git a/packages/webui/test/browser/rail-session-navigation.spec.mjs b/packages/webui/test/browser/rail-session-navigation.spec.mjs new file mode 100644 index 000000000..30d92604d --- /dev/null +++ b/packages/webui/test/browser/rail-session-navigation.spec.mjs @@ -0,0 +1,51 @@ +// The rail's session links, end to end against the built client. +// +// A rail session row is a plain `#session=` anchor, so selecting a session +// runs through the hash subscription inside `useSelectedSessionId` rather than +// through a setter the shell holds. Clicking the link of the session that is +// *already* selected changes nothing that subscription can observe: the hash +// stays the same, so no `hashchange` fires, the selection does not change, and +// the shell never hears that the user asked for the conversation -- it keeps +// the surface it was showing. +// +// The symptom is the 远程 page: open it, click the session you are already in, +// and the page stays put. +// +// The session link is clicked by dispatching the event rather than by moving +// the pointer. The rail row carries its own action buttons (置顶, 归档, 更多) on +// the right, and they intercept the pointer at the link's centre -- the same +// spot a user has to aim around. + +import { expect } from "@playwright/test"; + +import { openApp, test } from "./harness.mjs"; + +function sessionLink(page, sessionId) { + return page.locator(`[data-webui-session-link="${sessionId}"]`); +} + +function conversation(page) { + return page.locator('[data-webui-session-layout="true"]'); +} + +function remotePage(page) { + return page.locator('[data-webui-component="remote-page"]'); +} + +test("clicking the selected session's rail link returns to the conversation", async ({ page }) => { + await openApp(page, "#session=A"); + + await expect(sessionLink(page, "A")).toBeVisible(); + await expect(conversation(page)).toBeVisible(); + + await page.locator('[data-webui-nav-item="远程"]').click(); + await expect(remotePage(page)).toBeVisible(); + await expect(conversation(page)).toHaveCount(0); + + // The same session, clicked again. The hash does not change, so this click + // is the only signal the shell gets. + await sessionLink(page, "A").dispatchEvent("click"); + + await expect(remotePage(page)).toHaveCount(0); + await expect(conversation(page)).toBeVisible(); +});