The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(desktop): route external PR links to the pull request view (#22048)
Tim committed
Oct 20, 2026 at 15:59 UTC
b99955489af31efae279c407f877faebe6c39ace
4 files changed
+197
-6
apps/web/src/components/desktop/DesktopWebLinkCoordinator.browser.test.tsx
new
+147
@@ -0,0 +1,147 @@
1
+import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2
+import type { Atom } from "effect/reactivity";
3
+import { act } from "react";
4
+import { createRoot, type Root } from "react-dom/client";
5
+import { afterEach, expect, it, vi } from "vite-plus/test";
6
+
7
+const state = vi.hoisted(() => ({
8
+ projects: [] as never[],
9
+ navigate: vi.fn(async (): Promise<void> => undefined),
10
+ openScratchProject: vi.fn(async () => null),
11
+}));
12
+
13
+// One connected primary environment that can read pull requests. Its project list and
14
+// discovery flag start empty so a test can deliver links before they arrive.
15
+vi.mock(import("@tanstack/react-router"), async (original) => ({
16
+ ...(await original()),
17
+ useNavigate: (() => state.navigate) as never,
18
+ useRouter: (() => ({ navigate: state.navigate, state: { matches: [] } })) as never,
19
+}));
20
+vi.mock(import("../../state/environments"), async (original) => ({
21
+ ...(await original()),
22
+ usePrimaryEnvironmentId: () => "local" as EnvironmentId,
23
+ usePrimaryEnvironment: (() => ({
24
+ environmentId: "local",
25
+ connection: { phase: "connected" },
26
+ serverConfig: {},
27
+ })) as never,
28
+}));
29
+vi.mock(import("../../state/server"), async (original) => {
30
+ const { Atom } = await import("effect/reactivity");
31
+ const capabilities = { pullRequests: true, threadPullRequests: true };
32
+ return {
33
+ ...(await original()),
34
+ environmentServerConfigsAtom: Atom.make(
35
+ new Map([["local", { environment: { capabilities } }]]),
36
+ ) as never,
37
+ };
38
+});
39
+vi.mock(import("../../state/shell"), async (original) => {
40
+ const { Atom } = await import("effect/reactivity");
41
+ return { ...(await original()), allReachableShellsLiveAtom: Atom.make(false) };
42
+});
43
+vi.mock(import("../../state/entities"), async (original) => ({
44
+ ...(await original()),
45
+ readProjects: () => state.projects,
46
+}));
47
+vi.mock(import("../../hooks/useScratchProject"), async (original) => ({
48
+ ...(await original()),
49
+ useScratchProject: (() => ({
50
+ scratchEnvironmentId: () => "local",
51
+ openScratchProject: state.openScratchProject,
52
+ })) as never,
53
+}));
54
+
55
+import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
56
+import { allReachableShellsLiveAtom } from "../../state/shell";
57
+import { DesktopWebLinkCoordinator } from "./DesktopWebLinkCoordinator";
58
+
59
+const discoverySettled = allReachableShellsLiveAtom as Atom.Writable<boolean>;
60
+const environmentId = EnvironmentId.make("local");
61
+const projectId = ProjectId.make("effect");
62
+const project = {
63
+ id: projectId,
64
+ environmentId,
65
+ repositoryIdentity: {
66
+ provider: "github",
67
+ displayName: "Effect-TS/effect",
68
+ canonicalKey: "github.com/effect-ts/effect",
69
+ locator: {
70
+ source: "git-remote",
71
+ remoteName: "origin",
72
+ remoteUrl: "https://github.com/Effect-TS/effect.git",
73
+ },
74
+ },
75
+} as never;
76
+const pullRequestUrl = "https://github.com/Effect-TS/effect/pull/9339";
77
+const pullRequestView = {
78
+ to: "/pull-requests",
79
+ search: {
80
+ involvement: "all",
81
+ state: "all",
82
+ repository: "effect-ts/effect",
83
+ number: 9339,
84
+ selectedHost: "github.com",
85
+ selectedProjectId: projectId,
86
+ selectedEnvironmentId: environmentId,
87
+ },
88
+};
89
+
90
+let root: Root;
91
+
92
+/** Mounts the coordinator behind a desktop bridge that hands over `urls` once the renderer is ready. */
93
+async function mount(...urls: string[]) {
94
+ let deliver: ((url: string) => void) | undefined;
95
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
96
+ vi.stubGlobal("desktopBridge", {
97
+ getClientSettings: async () => null,
98
+ webLinks: {
99
+ onOpen: (listener: (url: string) => void) => {
100
+ deliver = listener;
101
+ return () => undefined;
102
+ },
103
+ setReady: async (ready: boolean) => {
104
+ if (ready) for (const url of urls.splice(0)) deliver?.(url);
105
+ },
106
+ },
107
+ });
108
+ root = createRoot(document.createElement("div"));
109
+ await act(async () =>
110
+ root.render(
111
+ <AppAtomRegistryProvider>
112
+ <DesktopWebLinkCoordinator />
113
+ </AppAtomRegistryProvider>,
114
+ ),
115
+ );
116
+}
117
+
118
+afterEach(async () => {
119
+ await act(async () => root.unmount());
120
+ vi.unstubAllGlobals();
121
+ vi.clearAllMocks();
122
+ state.projects = [];
123
+ appAtomRegistry.set(discoverySettled, false);
124
+});
125
+
126
+it("holds a launch PR link until project discovery settles, then opens the PR view", async () => {
127
+ await mount(pullRequestUrl);
128
+ expect(state.navigate).not.toHaveBeenCalled();
129
+ expect(state.openScratchProject).not.toHaveBeenCalled();
130
+
131
+ state.projects = [project];
132
+ const navigated = new Promise<void>((resolve) => {
133
+ state.navigate.mockImplementationOnce(async () => resolve());
134
+ });
135
+ await act(async () => {
136
+ appAtomRegistry.set(discoverySettled, true);
137
+ await navigated;
138
+ });
139
+ expect(state.navigate).toHaveBeenCalledExactlyOnceWith(pullRequestView);
140
+ expect(state.openScratchProject).not.toHaveBeenCalled();
141
+});
142
+
143
+it("opens an ordinary link in the browser while a PR link is still held", async () => {
144
+ await mount(pullRequestUrl, "https://example.com/docs");
145
+ expect(state.openScratchProject).toHaveBeenCalledOnce();
146
+ expect(state.navigate).not.toHaveBeenCalled();
147
+});
apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx
+43
-3
@@ -1,8 +1,12 @@
1
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
2
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
3
import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
4
+import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
5
import { promptLinkAppPath } from "@t3tools/shared/promptLinks";
6
import { useRouter } from "@tanstack/react-router";
7
+import * as Effect from "effect/Effect";
8
+import { AtomRegistry } from "effect/reactivity";
9
+import * as Stream from "effect/Stream";
10
import { useEffect, useEffectEvent, useRef } from "react";
11
12
import { openFileInPreview, openUrlInPreview } from "../../browser/openFileInPreview";
@@ -10,15 +14,37 @@ import { isPreviewAvailableFor } from "../../browser/previewRuntime";
14
import { useComposerDraftStore } from "../../composerDraftStore";
15
import { useNewThreadHandler } from "../../hooks/useHandleNewThread";
16
import { useScratchProject } from "../../hooks/useScratchProject";
17
+import { useClientSettings } from "../../hooks/useSettings";
18
+import { useOpenChangeRequestLink } from "../../lib/openPullRequestLink";
19
import { selectActiveRightPanelSurface, useRightPanelStore } from "../../rightPanelStore";
20
+import { appAtomRegistry } from "../../rpc/atomRegistry";
21
import { resolveThreadRouteTarget } from "../../threadRoutes";
22
import { assetEnvironment } from "../../state/assets";
23
import { useEnvironmentHttpBaseUrl, usePrimaryEnvironment } from "../../state/environments";
24
import { previewEnvironment } from "../../state/preview";
25
+import { allReachableShellsLiveAtom } from "../../state/shell";
26
import { useAtomCommand } from "../../state/use-atom-command";
27
import { useAtomQueryRunner } from "../../state/use-atom-query-runner";
28
import { stackedThreadToast, toastManager } from "../ui/toast";
29
30
+/** A link handed over by the OS is a plain click: nothing about it asks for the browser. */
31
+const externalClick = {
32
+ preventDefault() {},
33
+ stopPropagation() {},
34
+ metaKey: false,
35
+ ctrlKey: false,
36
+};
37
+
38
+/** Resolves once every reachable environment has reported its projects or settled offline. */
39
+const projectsSettled = (): Promise<void> =>
40
+ Effect.runPromise(
41
+ AtomRegistry.toStream(appAtomRegistry, allReachableShellsLiveAtom).pipe(
42
+ Stream.filter((settled) => settled),
43
+ Stream.runHead,
44
+ Effect.asVoid,
45
+ ),
46
+ );
47
+
48
/**
49
* HTML files are served by this machine's environment, so only its threads can
50
* take them.
@@ -47,6 +73,8 @@ function threadShowingBrowser(
73
export function DesktopWebLinkCoordinator() {
74
const router = useRouter();
75
const primaryEnvironment = usePrimaryEnvironment();
76
+ const openPullRequestsInApp = useClientSettings((settings) => settings.pullRequestLinksOpenInApp);
77
+ const openChangeRequest = useOpenChangeRequestLink();
78
const { scratchEnvironmentId, openScratchProject } = useScratchProject();
79
const openThread = useNewThreadHandler();
80
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
@@ -91,6 +119,7 @@ export function DesktopWebLinkCoordinator() {
119
await router.navigate({ href: appPath });
120
return;
121
}
122
+ if (openChangeRequest(externalClick, url)) return;
123
const isFile = url.startsWith("file:");
124
const routeParams = router.state.matches.at(-1)?.params ?? {};
125
const threadRef =
@@ -121,12 +150,23 @@ export function DesktopWebLinkCoordinator() {
150
}
151
});
152
153
+ const receiveLink = useEffectEvent((url: string) => {
154
+ // A connected server publishes its config before its project snapshot, so a PR link
155
+ // delivered at launch would miss its project and fall through to the browser. Hold PR
156
+ // links, and only those, until discovery settles; an ordinary link never waits for it.
157
+ const hold =
158
+ openPullRequestsInApp && parseChangeRequestUrl(url) !== null
159
+ ? projectsSettled()
160
+ : Promise.resolve();
161
+ void hold.then(() => {
162
+ queueRef.current = queueRef.current.then(() => openLink(url)).catch(() => undefined);
163
+ });
164
+ });
165
+
166
useEffect(() => {
167
if (!ready || webLinks === undefined) return;
168
let subscribed = true;
127
- const unsubscribe = webLinks.onOpen((url) => {
128
- queueRef.current = queueRef.current.then(() => openLink(url)).catch(() => undefined);
129
- });
169
+ const unsubscribe = webLinks.onOpen(receiveLink);
170
queueMicrotask(() => {
171
if (subscribed) void webLinks.setReady(true).catch(() => undefined);
172
});
apps/web/src/lib/openPullRequestLink.ts
+4
-3
@@ -14,7 +14,7 @@ import { useRightPanelStore } from "../rightPanelStore";
14
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
15
16
import { appAtomRegistry } from "../rpc/atomRegistry";
17
-import { useProjects } from "../state/entities";
17
+import { readProjects, useProjects } from "../state/entities";
18
import { environmentServerConfigsAtom, serverEnvironment } from "../state/server";
19
import { usePrimaryEnvironmentId } from "../state/environments";
20
@@ -116,14 +116,15 @@ export function useOpenChangeRequestLink(
116
targetEnvironmentId?: EnvironmentId,
117
) => boolean {
118
const navigate = useNavigate();
119
- const allProjects = useProjects();
119
const primaryEnvironmentId = usePrimaryEnvironmentId();
120
const openInApp = useClientSettings((settings) => settings.pullRequestLinksOpenInApp);
121
return (event, targetUrl, targetThreadRef, targetEnvironmentId) => {
122
if (!openInApp || shouldOpenPullRequestExternally(event)) return false;
123
// Read at click time: every provider status push replaces the config map, and
125
- // subscribing would hand every rendered link a new handler.
124
+ // subscribing would hand every rendered link a new handler. Projects likewise, so a
125
+ // link can resolve against a snapshot that arrived after this handler was built.
126
const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
127
+ const allProjects = readProjects();
128
const resolvedThreadRef = targetThreadRef ?? threadRef;
129
const resolvedPanelRef = panelRef ?? resolvedThreadRef;
130
const parsed = parseChangeRequestUrl(targetUrl);
docs/user/default-browser.md
+3
@@ -8,6 +8,9 @@ page, so you can ask an agent about it right away.
8
Choose **T3 Code** under **System Settings → Desktop & Dock → Default web browser**. To go back,
9
choose another browser there.
10
11
+Pull request links for a connected project open in the pull request view when opening pull
12
+request links in the app is enabled. Other links still open in the browser panel.
13
+
14
HTML files you open from Finder open the same way. [Prompt links](prompt-links.md) to
15
app.t3.codes open in the app as a draft.
16