Skip to content

Commit cf9aed4

Browse files
fix(web): palette requests without a mounted palette are dropped, add-project dialog ends with its project
openCommandPalette now ignores requests while no palette is mounted, as the window event on main did, so Ctrl+N on /pair cannot leave a stale palette open for the app shell. The add-project-to-machine request subscribes to its project and ends when the project goes away, so a project that vanishes before the dialog shows cannot reopen it when it returns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a8eba2d commit cf9aed4

5 files changed

Lines changed: 134 additions & 11 deletions

File tree

‎apps/web/src/commandPaletteBus.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EnvironmentId, PullRequestLinkedThreadsResult } from "@t3tools/contracts";
22

3-
import { dispatchCommandPalette } from "./commandPaletteStore";
3+
import { dispatchCommandPalette, isCommandPaletteMounted } from "./commandPaletteStore";
44

55
export interface CommandPaletteLinkedThreads {
66
readonly environmentId: EnvironmentId;
@@ -14,6 +14,9 @@ export interface CommandPaletteOpenDetail {
1414
}
1515

1616
export function openCommandPalette(detail: CommandPaletteOpenDetail = {}): void {
17+
// With no palette on screen (e.g. /pair) the request is dropped; kept in the
18+
// store, it would pop a stale palette once the app shell mounts.
19+
if (!isCommandPaletteMounted()) return;
1720
if (detail.open === "new-thread-in") {
1821
dispatchCommandPalette({ _tag: "OpenNewThreadIn" });
1922
} else if (detail.open === "add-project") {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2+
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3+
import { describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { act, render } from "../../test/render";
6+
7+
const project = vi.hoisted(() => {
8+
let value: unknown = null;
9+
const listeners = new Set<() => void>();
10+
return {
11+
get: () => value,
12+
set: (next: unknown) => {
13+
value = next;
14+
for (const listener of listeners) listener();
15+
},
16+
subscribe: (listener: () => void) => {
17+
listeners.add(listener);
18+
return () => listeners.delete(listener);
19+
},
20+
};
21+
});
22+
23+
vi.mock(import("../state/entities"), async (original) => {
24+
const { useSyncExternalStore } = await import("react");
25+
return {
26+
...(await original()),
27+
readProject: (() => project.get()) as never,
28+
subscribeProject: ((_ref: unknown, listener: (value: unknown) => void) =>
29+
project.subscribe(() => listener(project.get()))) as never,
30+
useProject: (() => useSyncExternalStore(project.subscribe, project.get)) as never,
31+
};
32+
});
33+
vi.mock(import("../state/environments"), async (original) => ({
34+
...(await original()),
35+
useEnvironments: (() => ({ environments: [] })) as never,
36+
}));
37+
vi.mock(import("../hooks/useHandleNewThread"), async (original) => ({
38+
...(await original()),
39+
useNewThreadHandler: (() => () => undefined) as never,
40+
}));
41+
vi.mock(import("../hooks/useAddProjectTargets"), async (original) => ({
42+
...(await original()),
43+
useAddProjectTargets: (() => []) as never,
44+
}));
45+
vi.mock(import("../hooks/useLogicalProjectEnvironments"), async (original) => ({
46+
...(await original()),
47+
useLogicalProjectEnvironments: (() => []) as never,
48+
}));
49+
50+
import {
51+
AddProjectToMachineDialogHost,
52+
openAddProjectToMachine,
53+
} from "./AddProjectToMachineDialog";
54+
55+
const environmentId = EnvironmentId.make("environment-1");
56+
const projectRef = scopeProjectRef(environmentId, ProjectId.make("project-1"));
57+
const PROJECT = {
58+
environmentId,
59+
id: projectRef.projectId,
60+
title: "Repo",
61+
workspaceRoot: "/repo",
62+
repositoryIdentity: null,
63+
};
64+
65+
describe("AddProjectToMachineDialog", () => {
66+
it("closes when its project goes away", async () => {
67+
project.set(PROJECT);
68+
await using screen = await render(<AddProjectToMachineDialogHost />);
69+
70+
await act(() => openAddProjectToMachine({ projectRef, targetEnvironmentId: null }));
71+
await expect.element(screen.getByText("Add to another machine")).toBeInTheDocument();
72+
73+
await act(() => project.set(null));
74+
await act(() => project.set(PROJECT));
75+
await expect.element(screen.getByText("Add to another machine")).not.toBeInTheDocument();
76+
});
77+
78+
it("does not reopen for a project that vanished before the dialog showed", async () => {
79+
project.set(PROJECT);
80+
await using screen = await render(<AddProjectToMachineDialogHost />);
81+
82+
await act(() => {
83+
openAddProjectToMachine({ projectRef, targetEnvironmentId: null });
84+
project.set(null);
85+
});
86+
await act(() => project.set(PROJECT));
87+
88+
await expect.element(screen.getByText("Add to another machine")).not.toBeInTheDocument();
89+
});
90+
});

‎apps/web/src/components/AddProjectToMachineDialog.tsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from "../lib/addProjectToMachine";
3232
import { formatBytes } from "../lib/formatBytes";
3333
import { newProjectId } from "../lib/utils";
34-
import { readProject, useProject } from "../state/entities";
34+
import { readProject, subscribeProject, useProject } from "../state/entities";
3535
import { useEnvironments } from "../state/environments";
3636
import { projectEnvironment } from "../state/projects";
3737
import { useEnvironmentQuery } from "../state/query";
@@ -63,15 +63,25 @@ const useRequest = create<{ request: AddProjectToMachineRequest | null; session:
6363
session: 0,
6464
}));
6565

66+
let endRequestWithProject: (() => void) | null = null;
67+
6668
/** Opens the dialog that clones a project onto another connected machine. */
6769
export function openAddProjectToMachine(request: AddProjectToMachineRequest) {
68-
// The dialog closes itself when its project goes away, so it must not open
69-
// for one that is already gone and then appear when it loads.
7070
if (readProject(request.projectRef) === null) return;
71+
endRequestWithProject?.();
72+
// The request ends when its project goes away, so a project that comes back
73+
// later cannot reopen a dialog nobody asked for.
74+
endRequestWithProject = subscribeProject(request.projectRef, (project) => {
75+
if (project === null) close();
76+
});
7177
useRequest.setState((state) => ({ request, session: state.session + 1 }));
7278
}
7379

74-
const close = () => useRequest.setState({ request: null });
80+
function close() {
81+
endRequestWithProject?.();
82+
endRequestWithProject = null;
83+
useRequest.setState({ request: null });
84+
}
7585

7686
export function AddProjectToMachineDialogHost() {
7787
const request = useRequest((state) => state.request);
@@ -197,15 +207,10 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe
197207

198208
return (
199209
<Dialog
200-
open={project !== null}
210+
open
201211
onOpenChange={(open) => {
202212
if (!open) close();
203213
}}
204-
// A user close unmounts at once through onOpenChange; this ends a request
205-
// whose project vanished while the dialog was open.
206-
onOpenChangeComplete={(open) => {
207-
if (!open) close();
208-
}}
209214
>
210215
<DialogPopup className="sm:max-w-xl">
211216
<form

‎apps/web/src/shortcuts/commandPaletteCommands.browser.test.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
33
import { userEvent } from "vite-plus/test/browser";
44

55
import { render } from "../../test/render";
6+
import { openCommandPalette } from "../commandPaletteBus";
67
import { useCommandPaletteMount, useCommandPaletteStore } from "../commandPaletteStore";
78
import { installCommandPaletteCommands } from "./commandPaletteCommands";
89

@@ -50,3 +51,20 @@ describe("command palette shortcuts", () => {
5051
expect(useCommandPaletteStore.getState().open).toBe(false);
5152
});
5253
});
54+
55+
describe("openCommandPalette", () => {
56+
it("drops a request made while no palette is mounted", async () => {
57+
setup();
58+
await using screen = await render(null, { router: "/pair" });
59+
60+
openCommandPalette({ open: "new-thread-in" });
61+
await screen.rerender(<MountedPalette />);
62+
expect(useCommandPaletteStore.getState()).toMatchObject({ open: false, openIntent: null });
63+
64+
openCommandPalette({ open: "new-thread-in" });
65+
expect(useCommandPaletteStore.getState()).toMatchObject({
66+
open: true,
67+
openIntent: { kind: "new-thread-in" },
68+
});
69+
});
70+
});

‎apps/web/src/state/entities.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
232232
return appAtomRegistry.get(environmentProjects.projectAtom(ref));
233233
}
234234

235+
export function subscribeProject(
236+
ref: ScopedProjectRef,
237+
listener: (project: EnvironmentProject | null) => void,
238+
): () => void {
239+
return appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), listener);
240+
}
241+
235242
export function readProjects(): ReadonlyArray<EnvironmentProject> {
236243
return appAtomRegistry.get(environmentProjects.projectsAtom);
237244
}

0 commit comments

Comments
 (0)