Skip to content

Commit 981b850

Browse files
committed
fix(desktop): keep startup thread links queued until catalog loads
1 parent 28416e9 commit 981b850

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎apps/web/src/components/desktop/DesktopWebLinkCoordinator.browser.test.tsx‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const state = vi.hoisted(() => ({
1616
confirm: vi.fn(async (_message: string) => true),
1717
connectPairing: vi.fn(async (_input: { pairingUrl: string }) => ({ _tag: "Success" })),
1818
localEnvironmentDisabled: false,
19+
catalogReady: true,
20+
environmentIds: ["local"] as string[],
1921
}));
2022

2123
// One connected primary environment that can read pull requests. Its project list and
@@ -31,6 +33,10 @@ vi.mock(import("@tanstack/react-router"), async (original) => ({
3133
vi.mock(import("../../state/environments"), async (original) => ({
3234
...(await original()),
3335
usePrimaryEnvironmentId: () => "local" as EnvironmentId,
36+
useEnvironments: (() => ({
37+
isReady: state.catalogReady,
38+
environments: state.environmentIds.map((environmentId) => ({ environmentId })),
39+
})) as never,
3440
usePrimaryEnvironment: (() =>
3541
state.localEnvironmentDisabled
3642
? null
@@ -147,6 +153,8 @@ function setup() {
147153
vi.clearAllMocks();
148154
state.confirm.mockResolvedValue(true);
149155
state.localEnvironmentDisabled = false;
156+
state.catalogReady = true;
157+
state.environmentIds = ["local"];
150158
state.projects = [];
151159
state.routeParams = {};
152160
state.preview
@@ -175,14 +183,21 @@ async function mount(...urls: string[]) {
175183
},
176184
}),
177185
);
178-
stack.use(
186+
const screen = stack.use(
179187
await render(
180188
<AppAtomRegistryProvider>
181189
<DesktopWebLinkCoordinator />
182190
</AppAtomRegistryProvider>,
183191
),
184192
);
185-
return stack.move();
193+
return Object.assign(stack.move(), {
194+
rerender: () =>
195+
screen.rerender(
196+
<AppAtomRegistryProvider>
197+
<DesktopWebLinkCoordinator />
198+
</AppAtomRegistryProvider>,
199+
),
200+
});
186201
}
187202

188203
it("holds a launch PR link until project discovery settles, then opens the PR view", async () => {
@@ -335,3 +350,28 @@ it("does not hold a PR link for project discovery that never settles with Local
335350
await using _coordinator = await mount(pullRequestUrl);
336351
await vi.waitFor(() => expect(state.openScratchProject).toHaveBeenCalledOnce());
337352
});
353+
354+
it.each([false, true])(
355+
"keeps a saved remote startup thread link queued until its catalog loads (local disabled: %s)",
356+
async (localEnvironmentDisabled) => {
357+
setup();
358+
state.localEnvironmentDisabled = localEnvironmentDisabled;
359+
state.catalogReady = false;
360+
state.environmentIds = [];
361+
await using coordinator = await mount("t3code://threads/remote/thread");
362+
expect(state.navigate).not.toHaveBeenCalled();
363+
expect(state.openScratchProject).not.toHaveBeenCalled();
364+
expect(state.openLink).not.toHaveBeenCalled();
365+
366+
const navigated = new Promise<void>((resolve) => {
367+
state.navigate.mockImplementationOnce(async () => resolve());
368+
});
369+
state.environmentIds = ["remote"];
370+
state.catalogReady = true;
371+
await coordinator.rerender();
372+
await navigated;
373+
expect(state.navigate).toHaveBeenCalledExactlyOnceWith({ href: "/remote/thread" });
374+
expect(state.openScratchProject).not.toHaveBeenCalled();
375+
expect(state.openLink).not.toHaveBeenCalled();
376+
},
377+
);

‎apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function threadShowingBrowser(
8686
export function DesktopWebLinkCoordinator() {
8787
const router = useRouter();
8888
const primaryEnvironment = usePrimaryEnvironment();
89-
const { environments } = useEnvironments();
89+
const { environments, isReady: environmentsReady } = useEnvironments();
9090
const openPullRequestsInApp = useClientSettings((settings) => settings.pullRequestLinksOpenInApp);
9191
const openChangeRequest = useOpenChangeRequestLink();
9292
const { scratchEnvironmentId, openScratchProject } = useScratchProject();
@@ -104,9 +104,11 @@ export function DesktopWebLinkCoordinator() {
104104
// A sync IPC read; the setting only changes across a relaunch.
105105
const [localEnvironmentDisabled] = useState(isLocalEnvironmentDisabled);
106106
// With Local environment disabled there is no primary to wait for, so links run against the
107-
// saved remote environments instead of waiting forever.
107+
// saved remote environments instead of waiting forever. The catalog must finish loading
108+
// before the bridge drains startup links, so saved remotes are not mistaken for unknown ones.
108109
const ready =
109110
webLinks !== undefined &&
111+
environmentsReady &&
110112
(localEnvironmentDisabled ||
111113
(primaryEnvironment?.connection.phase === "connected" &&
112114
primaryEnvironment.serverConfig !== null));

0 commit comments

Comments
 (0)