@devries-cameron20 / t3code Mirror · read-only / commits / c2845de6c4
Fork 0

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, 18:15 UTC

fix(mobile): PR links open in the app the same way they do on web (#24738)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 24, 2026 at 21:56 UTC c2845de6c451db48f8229d77b3533730fb1644ac
8 files changed +280 -142
apps/mobile/src/features/pull-requests/pullRequestNavigation.test.ts
+26 -2
@@ -1,6 +1,6 @@
1 import { EnvironmentId, ProjectId, SourceControlProviderKind } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3 -import { nativePullRequestDestination } from "../../lib/pullRequestNavigation";
3 +import { nativePullRequestDestination as destination } from "../../lib/pullRequestNavigation";
4
5 const context = { environmentId: "remote", projectId: "project" };
6 const project = (id: string, canonicalKey: string, provider: string, remoteUrl: string) => ({
@@ -19,6 +19,14 @@ const projects = [
19 project("forgejo", "forgejo.test/org/repo", "forgejo", "http://forgejo.test:3000/org/repo.git"),
20 ];
21
22 +const hostReads = () => ({ pullRequests: true, threadPullRequests: true });
23 +const nativePullRequestDestination = (
24 + url: string,
25 + linkContext: typeof context | null,
26 + linkProjects: typeof projects,
27 + capabilities: Parameters<typeof destination>[3] = hostReads,
28 +) => destination(url, linkContext, linkProjects, capabilities);
29 +
30 describe("native PR entry points", () => {
31 it("keeps a thread link in its own environment and handles renamed host path shapes", () => {
32 expect(
@@ -41,9 +49,25 @@ describe("native PR entry points", () => {
49 ).toBe("forgejo.test:3000");
50 });
51
44 - it("leaves pull requests from repositories without a project to the external opener", () => {
52 + it("opens a repository nobody has checked out under a project on its host, as web does", () => {
53 expect(
54 nativePullRequestDestination("https://github.com/someone/else/pull/12", context, projects),
55 + ).toEqual({ ...context, host: "github.com", repository: "someone/else", number: 12 });
56 + });
57 +
58 + it("leaves links to the external opener when the environment cannot show them", () => {
59 + const url = "https://github.com/org/repo/pull/12";
60 + expect(nativePullRequestDestination(url, context, projects, () => undefined)).toBeNull();
61 + expect(
62 + nativePullRequestDestination(
63 + "https://github.com/someone/else/pull/12",
64 + context,
65 + projects,
66 + () => ({
67 + pullRequests: true,
68 + threadPullRequests: false,
69 + }),
70 + ),
71 ).toBeNull();
72 });
73
apps/mobile/src/lib/pullRequestNavigation.ts
+21 -21
@@ -1,32 +1,32 @@
1 -import { findProjectForChangeRequest } from "@t3tools/client-runtime/change-request-links";
1 +import {
2 + resolveChangeRequestLinkTarget,
3 + type ChangeRequestLinkCapabilities,
4 +} from "@t3tools/client-runtime/change-request-links";
5 import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
3 -import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
6 +import { EnvironmentId, ProjectId } from "@t3tools/contracts";
7
5 -/**
6 - * Opens a change request in the app only under a project of the link's environment whose own
7 - * repository holds it, preferring the one the link came from; anything else is the browser's.
8 - */
8 +/** The PullRequest route for a change request link from a project, or null for the browser. */
9 export function nativePullRequestDestination(
10 url: string,
11 context: { environmentId: string; projectId: string } | null,
12 projects: ReadonlyArray<Pick<EnvironmentProject, "id" | "environmentId" | "repositoryIdentity">>,
13 + capabilities: (environmentId: EnvironmentId) => ChangeRequestLinkCapabilities | undefined,
14 ) {
14 - const parsed = sourceControlClients.parseChangeRequestUrl(url)?.link;
15 - if (!parsed || !context) return null;
16 - const candidates = projects.filter((project) => project.environmentId === context.environmentId);
17 - const project = findProjectForChangeRequest(
18 - [
19 - ...candidates.filter((candidate) => candidate.id === context.projectId),
20 - ...candidates.filter((candidate) => candidate.id !== context.projectId),
21 - ],
22 - parsed,
23 - );
24 - if (project === undefined) return null;
15 + if (!context) return null;
16 + const target = resolveChangeRequestLinkTarget({
17 + url,
18 + projects,
19 + capabilities,
20 + environmentId: EnvironmentId.make(context.environmentId),
21 + projectId: ProjectId.make(context.projectId),
22 + lendHostProject: true,
23 + });
24 + if (target === null) return null;
25 return {
26 environmentId: context.environmentId,
27 - projectId: String(project.id),
28 - repository: parsed.repository,
29 - number: parsed.number,
30 - host: parsed.authority ?? parsed.host,
27 + projectId: String(target.project.id),
28 + repository: target.repository,
29 + number: target.number,
30 + ...(target.host === undefined ? {} : { host: target.host }),
31 };
32 }
apps/mobile/src/native/useOpenPullRequest.ts
+10 -1
@@ -6,6 +6,8 @@ import { resetAndroidWorkspace } from "../lib/android-workspace-navigation";
6 import { EnvironmentId, ThreadId } from "@t3tools/contracts";
7 import { useProjects, useThreadShell } from "../state/entities";
8 import { tryOpenExternalUrl } from "../lib/openExternalUrl";
9 +import { appAtomRegistry } from "../state/atom-registry";
10 +import { environmentServerConfigsAtom } from "../state/server";
11
12 type PullRequestContext = { readonly environmentId: string; readonly projectId: string };
13
@@ -16,7 +18,14 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean
18 const replace = options?.replace;
19 return useCallback(
20 async (url: string, context: PullRequestContext | null) => {
19 - const params = nativePullRequestDestination(url, context, projects);
21 + // Read at press time so a capability push does not hand every link a new handler.
22 + const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
23 + const params = nativePullRequestDestination(
24 + url,
25 + context,
26 + projects,
27 + (environmentId) => serverConfigs.get(environmentId)?.environment.capabilities,
28 + );
29 if (!params) return tryOpenExternalUrl(url, "pull-request");
30 if (Platform.OS === "android")
31 resetAndroidWorkspace(navigation, { name: "PullRequest", params });
apps/web/src/components/chat/ThreadDetailsPrRows.tsx
+1 -1
@@ -10,7 +10,7 @@ import { Minus, Plus } from "lucide";
10 import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
11
12 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
13 -import { findProjectOnChangeRequestHost } from "~/lib/openPullRequestLink";
13 +import { findProjectOnChangeRequestHost } from "@t3tools/client-runtime/change-request-links";
14
15 import { useProjects } from "~/state/entities";
16 import { threadEnvironment } from "~/state/threads";
apps/web/src/lib/openPullRequestLink.test.ts deleted
-40
@@ -1,40 +0,0 @@
1 -import { describe, expect, it } from "vite-plus/test";
2 -
3 -import { findProjectOnChangeRequestHost } from "./openPullRequestLink";
4 -
5 -describe("findProjectOnChangeRequestHost", () => {
6 - const project = (id: string, identity: Record<string, unknown>) =>
7 - ({ id, repositoryIdentity: identity }) as never;
8 - const frontend = project("frontend", {
9 - canonicalKey: "github.com/acme/frontend",
10 - provider: "github",
11 - owner: "acme",
12 - name: "frontend",
13 - });
14 - const backend = project("backend", {
15 - canonicalKey: "github.com/acme/backend",
16 - provider: "github",
17 - owner: "acme",
18 - name: "backend",
19 - });
20 -
21 - it("prefers the project checked out from the link's own repository", () => {
22 - expect(
23 - findProjectOnChangeRequestHost([frontend, backend], {
24 - host: "github.com",
25 - repository: "acme/backend",
26 - number: 7,
27 - }),
28 - ).toBe(backend);
29 - });
30 -
31 - it("lends any project on the host to a repository nobody has checked out", () => {
32 - expect(
33 - findProjectOnChangeRequestHost([frontend], {
34 - host: "github.com",
35 - repository: "acme/backend",
36 - number: 7,
37 - }),
38 - ).toBe(frontend);
39 - });
40 -});
apps/web/src/lib/openPullRequestLink.ts
+22 -77
@@ -4,19 +4,14 @@ import { useNavigate } from "@tanstack/react-router";
4 import { type MouseEvent } from "react";
5
6 import {
7 - findProjectForChangeRequest,
7 + resolveChangeRequestLinkTarget,
8 resolvePullRequestPreviewTarget,
9 } from "@t3tools/client-runtime/change-request-links";
10 -import {
11 - sourceControlClients,
12 - type ChangeRequestLink,
13 -} from "@t3tools/client-runtime/source-control-clients";
10
11 import { useOpenLink } from "../browser/useOpenLink";
12 import { useClientSettings } from "../hooks/useSettings";
13 import { stackedThreadToast, toastManager } from "../components/ui/toast";
14 import { useRightPanelStore } from "../rightPanelStore";
19 -import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
15
16 import { appAtomRegistry } from "../rpc/atomRegistry";
17 import { readProjects, useProjects } from "../state/entities";
@@ -34,21 +29,6 @@ export function usePullRequestPreviewTarget(environmentId: EnvironmentId | null,
29 });
30 }
31
37 -export function findProjectOnChangeRequestHost(
38 - projects: ReadonlyArray<EnvironmentProject>,
39 - link: ChangeRequestLink,
40 -): EnvironmentProject | undefined {
41 - const own = findProjectForChangeRequest(projects, link);
42 - if (own !== undefined) return own;
43 - return projects.find((project) => {
44 - const identity = project.repositoryIdentity;
45 - return (
46 - identity?.provider !== undefined &&
47 - sourceControlClients.get(identity.provider).canReadChangeRequestOnHost(identity, link)
48 - );
49 - });
50 -}
51 -
32 function shouldOpenPullRequestExternally(
33 event: Pick<MouseEvent<HTMLElement>, "metaKey" | "ctrlKey"> &
34 Partial<Pick<MouseEvent<HTMLElement>, "button">>,
@@ -77,47 +57,27 @@ export function useOpenChangeRequestLink(
57 // subscribing would hand every rendered link a new handler. Projects likewise, so a
58 // link can resolve against a snapshot that arrived after this handler was built.
59 const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
80 - const allProjects = readProjects();
60 const resolvedThreadRef = targetThreadRef ?? threadRef;
61 const resolvedPanelRef = panelRef ?? resolvedThreadRef;
83 - const parsed = sourceControlClients.parseChangeRequestUrl(targetUrl)?.link;
84 - if (parsed === undefined) return false;
85 - const reads = (environmentId: string) =>
86 - serverConfigs.get(environmentId as EnvironmentId)?.environment.capabilities.pullRequests ===
87 - true;
88 - // Matching remotes do not make project ids usable outside the panel thread's environment.
89 - const projects = resolvedThreadRef
90 - ? allProjects.filter((project) => project.environmentId === resolvedThreadRef.environmentId)
91 - : targetEnvironmentId
92 - ? allProjects.filter((project) => project.environmentId === targetEnvironmentId)
93 - : allProjects
94 - .filter((project) => reads(project.environmentId))
95 - .toSorted(
96 - (left, right) =>
97 - Number(right.environmentId === primaryEnvironmentId) -
98 - Number(left.environmentId === primaryEnvironmentId),
99 - );
100 - const exactProject = findProjectForChangeRequest(projects, parsed);
101 - const project =
102 - exactProject ??
103 - (resolvedPanelRef
104 - ? findProjectOnChangeRequestHost(
105 - projects.filter(
106 - (candidate) =>
107 - serverConfigs.get(candidate.environmentId)?.environment.capabilities
108 - .threadPullRequests === true,
109 - ),
110 - parsed,
111 - )
112 - : undefined);
113 - if (project === undefined || !reads(project.environmentId)) return false;
114 - const repository =
115 - serverConfigs.get(project.environmentId)?.environment.capabilities.threadPullRequests === true
116 - ? parsed.repository
117 - : (sourceControlClients.repositorySelector(project.repositoryIdentity) ??
118 - parsed.repository);
62 + const target = resolveChangeRequestLinkTarget({
63 + url: targetUrl,
64 + projects: readProjects(),
65 + capabilities: (environmentId) => serverConfigs.get(environmentId)?.environment.capabilities,
66 + environmentId: resolvedThreadRef?.environmentId ?? targetEnvironmentId,
67 + primaryEnvironmentId,
68 + lendHostProject: resolvedPanelRef !== undefined,
69 + });
70 + if (target === null) return false;
71 + const { project, repository, number, host } = target;
72 event.preventDefault();
73 event.stopPropagation();
74 + const pullRequestsSearch = {
75 + repository,
76 + number,
77 + ...(host === undefined ? {} : { selectedHost: host }),
78 + selectedProjectId: project.id,
79 + selectedEnvironmentId: project.environmentId,
80 + };
81 if (resolvedPanelRef) {
82 useRightPanelStore.getState().openPullRequest(resolvedPanelRef, {
83 // The standalone PR panel has a synthetic ref; each tab keeps its real environment.
@@ -125,13 +85,10 @@ export function useOpenChangeRequestLink(
85 ? {}
86 : { environmentId: project.environmentId }),
87 projectId: project.id,
128 - ...(serverConfigs.get(project.environmentId)?.environment.capabilities
129 - .threadPullRequests === true
130 - ? { host: parsed.authority ?? parsed.host }
131 - : {}),
88 + ...(host === undefined ? {} : { host }),
89 repository,
90 url: targetUrl,
134 - number: parsed.number,
91 + number,
92 });
93 if (!resolvedThreadRef) {
94 void navigate({
@@ -140,11 +97,7 @@ export function useOpenChangeRequestLink(
97 ...previous,
98 involvement: previous.involvement ?? "all",
99 state: previous.state ?? "all",
143 - repository,
144 - number: parsed.number,
145 - selectedHost: parsed.authority ?? parsed.host,
146 - selectedProjectId: project.id,
147 - selectedEnvironmentId: project.environmentId,
100 + ...pullRequestsSearch,
101 }),
102 replace: true,
103 });
@@ -153,15 +106,7 @@ export function useOpenChangeRequestLink(
106 }
107 void navigate({
108 to: "/pull-requests",
156 - search: {
157 - involvement: "all",
158 - state: "all",
159 - repository,
160 - number: parsed.number,
161 - selectedHost: parsed.authority ?? parsed.host,
162 - selectedProjectId: project.id,
163 - selectedEnvironmentId: project.environmentId,
164 - },
109 + search: { involvement: "all", state: "all", ...pullRequestsSearch },
110 });
111 return true;
112 };
packages/client-runtime/src/changeRequestLinks.test.ts
+108
@@ -8,7 +8,9 @@ import { describe, expect, it } from "vite-plus/test";
8
9 import {
10 findProjectForChangeRequest,
11 + findProjectOnChangeRequestHost,
12 projectChangeRequestUrl,
13 + resolveChangeRequestLinkTarget,
14 resolvePullRequestPreviewTarget,
15 } from "./changeRequestLinks.ts";
16
@@ -54,6 +56,112 @@ describe("findProjectForChangeRequest", () => {
56 });
57 });
58
59 +describe("findProjectOnChangeRequestHost", () => {
60 + const project = (id: string, identity: Record<string, unknown>) =>
61 + ({ id, repositoryIdentity: identity }) as never;
62 + const frontend = project("frontend", {
63 + canonicalKey: "github.com/acme/frontend",
64 + provider: "github",
65 + owner: "acme",
66 + name: "frontend",
67 + });
68 + const backend = project("backend", {
69 + canonicalKey: "github.com/acme/backend",
70 + provider: "github",
71 + owner: "acme",
72 + name: "backend",
73 + });
74 +
75 + it("prefers the project checked out from the link's own repository", () => {
76 + expect(
77 + findProjectOnChangeRequestHost([frontend, backend], {
78 + host: "github.com",
79 + repository: "acme/backend",
80 + number: 7,
81 + }),
82 + ).toBe(backend);
83 + });
84 +
85 + it("lends any project on the host to a repository nobody has checked out", () => {
86 + expect(
87 + findProjectOnChangeRequestHost([frontend], {
88 + host: "github.com",
89 + repository: "acme/backend",
90 + number: 7,
91 + }),
92 + ).toBe(frontend);
93 + });
94 +});
95 +
96 +describe("resolveChangeRequestLinkTarget", () => {
97 + const local = EnvironmentId.make("local");
98 + const remote = EnvironmentId.make("remote");
99 + const project = (id: string, environmentId: EnvironmentId, canonicalKey: string) => ({
100 + id: ProjectId.make(id),
101 + environmentId,
102 + repositoryIdentity: {
103 + canonicalKey,
104 + provider: SourceControlProviderKind.make(
105 + canonicalKey.startsWith("git.cafe") ? "gitcafe" : "github",
106 + ),
107 + displayName: canonicalKey.split("/").slice(1).join("/"),
108 + locator: { source: "git-remote", remoteName: "origin", remoteUrl: `https://${canonicalKey}` },
109 + } satisfies RepositoryIdentity,
110 + });
111 + const mirrored = project("mirrored", local, "git.cafe/acme/app");
112 + const sibling = project("sibling", local, "github.com/acme/tools");
113 + const elsewhere = project("elsewhere", remote, "github.com/acme/app");
114 + const projects = [mirrored, sibling, elsewhere];
115 + const hostReads = () => ({ pullRequests: true, threadPullRequests: true });
116 + const url = "https://github.com/acme/app/pull/7";
117 +
118 + it("opens a mirror's link in its own environment under a project on the link's host", () => {
119 + expect(
120 + resolveChangeRequestLinkTarget({
121 + url,
122 + projects,
123 + capabilities: hostReads,
124 + environmentId: local,
125 + projectId: mirrored.id,
126 + lendHostProject: true,
127 + }),
128 + ).toEqual({ project: sibling, host: "github.com", repository: "acme/app", number: 7 });
129 + });
130 +
131 + it("only borrows a host project for views that can show any repository on the host", () => {
132 + const resolve = (capabilities: typeof hostReads, lendHostProject: boolean) =>
133 + resolveChangeRequestLinkTarget({
134 + url,
135 + projects,
136 + capabilities,
137 + environmentId: local,
138 + lendHostProject,
139 + });
140 + expect(resolve(hostReads, false)).toBeNull();
141 + expect(resolve(() => ({ pullRequests: true, threadPullRequests: false }), true)).toBeNull();
142 + });
143 +
144 + it("finds the checked-out repository in any environment that reads change requests", () => {
145 + expect(
146 + resolveChangeRequestLinkTarget({
147 + url,
148 + projects,
149 + capabilities: hostReads,
150 + primaryEnvironmentId: local,
151 + lendHostProject: false,
152 + })?.project,
153 + ).toBe(elsewhere);
154 + expect(
155 + resolveChangeRequestLinkTarget({
156 + url,
157 + projects,
158 + capabilities: (environmentId) => (environmentId === remote ? undefined : hostReads()),
159 + lendHostProject: false,
160 + }),
161 + ).toBeNull();
162 + });
163 +});
164 +
165 describe("resolvePullRequestPreviewTarget", () => {
166 const environmentId = EnvironmentId.make("env-1");
167 const project = {
packages/client-runtime/src/changeRequestLinks.ts
+92
@@ -28,6 +28,98 @@ export function findProjectForChangeRequest<Project extends ChangeRequestProject
28 });
29 }
30
31 +/** Prefers the project checked out from the link's repository, else lends any project on its host. */
32 +export function findProjectOnChangeRequestHost<Project extends ChangeRequestProject>(
33 + projects: ReadonlyArray<Project>,
34 + link: ChangeRequestLink,
35 +): Project | undefined {
36 + return (
37 + findProjectForChangeRequest(projects, link) ??
38 + projects.find((project) => {
39 + const identity = project.repositoryIdentity;
40 + return (
41 + identity?.provider !== undefined &&
42 + sourceControlClients.get(identity.provider).canReadChangeRequestOnHost(identity, link)
43 + );
44 + })
45 + );
46 +}
47 +
48 +export interface ChangeRequestLinkCapabilities {
49 + readonly pullRequests?: boolean | undefined;
50 + readonly threadPullRequests?: boolean | undefined;
51 +}
52 +
53 +/**
54 + * Where every client opens a change request link in the app, or null for the browser. A link
55 + * opened into a pull request view may borrow any project on its host when the server reads
56 + * change requests by host, so a mirror's or a sibling repository's link still opens in the app.
57 + */
58 +export function resolveChangeRequestLinkTarget<Project extends ChangeRequestProject>({
59 + url,
60 + projects,
61 + capabilities,
62 + environmentId,
63 + projectId,
64 + primaryEnvironmentId,
65 + lendHostProject,
66 +}: {
67 + url: string;
68 + projects: ReadonlyArray<Project>;
69 + capabilities: (environmentId: EnvironmentId) => ChangeRequestLinkCapabilities | undefined;
70 + /** The link's own environment; without one, any environment that reads change requests. */
71 + environmentId?: EnvironmentId | undefined;
72 + /** The project the link came from, tried first. */
73 + projectId?: ProjectId | undefined;
74 + primaryEnvironmentId?: EnvironmentId | null | undefined;
75 + lendHostProject: boolean;
76 +}): {
77 + readonly project: Project;
78 + readonly repository: string;
79 + readonly number: number;
80 + readonly host?: string;
81 +} | null {
82 + const link = sourceControlClients.parseChangeRequestUrl(url)?.link;
83 + if (link === undefined) return null;
84 + // Matching remotes do not make project ids usable outside the link's environment.
85 + const candidates = (
86 + environmentId === undefined
87 + ? projects.filter((project) => capabilities(project.environmentId)?.pullRequests === true)
88 + : projects.filter((project) => project.environmentId === environmentId)
89 + ).sort(
90 + (left, right) =>
91 + Number(right.id === projectId) - Number(left.id === projectId) ||
92 + Number(right.environmentId === primaryEnvironmentId) -
93 + Number(left.environmentId === primaryEnvironmentId),
94 + );
95 + const project =
96 + findProjectForChangeRequest(candidates, link) ??
97 + (lendHostProject
98 + ? findProjectOnChangeRequestHost(
99 + candidates.filter(
100 + (candidate) => capabilities(candidate.environmentId)?.threadPullRequests === true,
101 + ),
102 + link,
103 + )
104 + : undefined);
105 + if (project === undefined) return null;
106 + const projectCapabilities = capabilities(project.environmentId);
107 + if (projectCapabilities?.pullRequests !== true) return null;
108 + return projectCapabilities.threadPullRequests === true
109 + ? {
110 + project,
111 + repository: link.repository,
112 + number: link.number,
113 + host: link.authority ?? link.host,
114 + }
115 + : {
116 + project,
117 + repository:
118 + sourceControlClients.repositorySelector(project.repositoryIdentity) ?? link.repository,
119 + number: link.number,
120 + };
121 +}
122 +
123 export function resolvePullRequestPreviewTarget({
124 environmentId,
125 projects,