From fea28b1a001460343440d5530d30e37ea634c619 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 9 Sep 2026 14:40:35 +0800 Subject: [PATCH] fix(webui): preserve project when creating a topic from its menu --- webui/src/App.tsx | 12 +++++++-- webui/src/tests/app-layout.test.tsx | 39 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/webui/src/App.tsx b/webui/src/App.tsx index 6cd0facee..274c3bcb5 100644 --- a/webui/src/App.tsx +++ b/webui/src/App.tsx @@ -316,7 +316,11 @@ function writeShellRoute(route: ShellRoute, replace = false): void { ); return; } - window.location.hash = nextHash; + window.history.pushState( + null, + "", + `${window.location.pathname}${window.location.search}${nextHash}`, + ); } function bootstrapTokenExpiresAt(expiresInSeconds: number): number { @@ -1135,7 +1139,11 @@ function Shell({ } }; window.addEventListener("hashchange", applyRoute); - return () => window.removeEventListener("hashchange", applyRoute); + window.addEventListener("popstate", applyRoute); + return () => { + window.removeEventListener("hashchange", applyRoute); + window.removeEventListener("popstate", applyRoute); + }; }, []); useEffect(() => { diff --git a/webui/src/tests/app-layout.test.tsx b/webui/src/tests/app-layout.test.tsx index ab3ea13d8..6539d8046 100644 --- a/webui/src/tests/app-layout.test.tsx +++ b/webui/src/tests/app-layout.test.tsx @@ -766,6 +766,45 @@ describe("App layout", () => { expect(screen.queryByText("connection-sensitive message")).not.toBeInTheDocument(); }); + it("keeps the context-menu project through navigation and first-message creation", async () => { + const projectScope = { + project_path: "/tmp/selected-project", + project_name: "selected-project", + access_mode: "full" as const, + restrict_to_workspace: false, + }; + mockSessions = [{ + key: "websocket:existing-chat", + channel: "websocket", + chatId: "existing-chat", + preview: "Existing topic", + workspaceScope: projectScope, + }]; + mockFetchRoutes({ + "/api/workspaces": { + schema_version: 1, + default_access_mode: "full", + default_scope: { ...projectScope, project_path: "/tmp/workspace", project_name: "workspace" }, + controls: { can_change_project: true, can_use_full_access: true }, + }, + }); + window.history.replaceState(null, "", "/#/chat/websocket%3Aexisting-chat"); + render(); + await waitFor(() => expect(connectSpy).toHaveBeenCalled()); + const projectSection = screen.getByRole("region", { name: "selected-project" }); + fireEvent.contextMenu(within(projectSection).getByRole("button", { name: "selected-project" })); + fireEvent.click(await screen.findByRole("menuitem", { name: "New topic" })); + expect(window.location.hash).toBe("#/new"); + // Let queued browser navigation events settle before sending the first message. + await act(async () => { await new Promise((resolve) => setTimeout(resolve, 50)); }); + expect(await screen.findByRole("button", { name: "Choose project" })).toHaveTextContent("selected-project"); + fireEvent.change(screen.getByLabelText("Message input"), { + target: { value: "project topic" }, + }); + fireEvent.click(screen.getByRole("button", { name: "Send message" })); + await waitFor(() => expect(createChatSpy).toHaveBeenCalledWith(projectScope)); + }); + it("uses the restricted default scope without offering project selection", async () => { mockFetchRoutes({ "/api/workspaces": {