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, 17:00 UTC
fix(mobile): retain requested PR environment while connecting (#24262)
Julius Marminge committed
Oct 25, 2026 at 15:08 UTC
2050a12b6d6d6705a42239b9b8b09491c0895bcc
5 files changed
+190
-15
apps/mobile/src/features/pull-requests/pullRequestEnvironmentFallback.test.ts
+19
-6
@@ -1,5 +1,7 @@
1
import { describe, expect, it } from "vite-plus/test";
2
import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
3
+import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
4
+import { EnvironmentId } from "@t3tools/contracts";
5
import { pullRequestFallbackEnvironment } from "./pullRequestEnvironmentFallback";
6
7
const project = (environmentId: string, id: string, canonicalKey: string) =>
@@ -20,24 +22,35 @@ const target = {
22
repository: "pingdotgg/t3code",
23
host: "github.com",
24
};
25
+const environment = (
26
+ environmentId: string,
27
+ connectionState: EnvironmentConnectionPhase = "connected",
28
+ isEnabled = true,
29
+) => ({ environmentId: EnvironmentId.make(environmentId), connectionState, isEnabled });
30
31
describe("pullRequestFallbackEnvironment", () => {
32
it("moves to a connected copy of the repository when the original is down", () => {
33
expect(
27
- pullRequestFallbackEnvironment(target, projects, new Set(["desktop", "server"])),
34
+ pullRequestFallbackEnvironment(target, projects, [
35
+ environment("desktop"),
36
+ environment("server"),
37
+ ]),
38
).toEqual({ environmentId: "server", projectId: "t3-copy" });
39
});
40
41
it("stays put while the original is connected or nothing else holds the repository", () => {
32
- expect(pullRequestFallbackEnvironment(target, projects, new Set(["laptop", "server"]))).toBe(
33
- null,
34
- );
35
- expect(pullRequestFallbackEnvironment(target, projects, new Set(["desktop"]))).toBe(null);
42
+ expect(
43
+ pullRequestFallbackEnvironment(target, projects, [
44
+ environment("laptop"),
45
+ environment("server"),
46
+ ]),
47
+ ).toBe(null);
48
+ expect(pullRequestFallbackEnvironment(target, projects, [environment("desktop")])).toBe(null);
49
});
50
51
it("derives the host from the original project when the route has none", () => {
52
const { host: _host, ...hostless } = target;
40
- expect(pullRequestFallbackEnvironment(hostless, projects, new Set(["server"]))).toEqual({
53
+ expect(pullRequestFallbackEnvironment(hostless, projects, [environment("server")])).toEqual({
54
environmentId: "server",
55
projectId: "t3-copy",
56
});
apps/mobile/src/features/pull-requests/pullRequestEnvironmentFallback.ts
+22
-2
@@ -1,4 +1,5 @@
1
import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
2
+import type { EnvironmentConnectionSummary } from "@t3tools/client-runtime/state/presentation";
3
4
export type PullRequestEnvironmentTarget = {
5
readonly environmentId: string;
@@ -10,9 +11,28 @@ export type PullRequestEnvironmentTarget = {
11
export function pullRequestFallbackEnvironment(
12
target: PullRequestEnvironmentTarget,
13
projects: ReadonlyArray<EnvironmentProject>,
13
- connectedEnvironmentIds: ReadonlySet<string>,
14
+ environments: ReadonlyArray<
15
+ Pick<EnvironmentConnectionSummary, "environmentId" | "connectionState" | "isEnabled">
16
+ >,
17
) {
15
- if (connectedEnvironmentIds.has(target.environmentId)) return null;
18
+ const requested = environments.find(
19
+ (environment) => environment.environmentId === target.environmentId,
20
+ );
21
+ // Pending connections retain the requested target until an explicit failure, even before
22
+ // its project hydrates. Available includes the period before connection startup.
23
+ if (
24
+ requested?.isEnabled &&
25
+ (requested.connectionState === "connected" ||
26
+ requested.connectionState === "available" ||
27
+ requested.connectionState === "connecting" ||
28
+ requested.connectionState === "reconnecting")
29
+ )
30
+ return null;
31
+ const connectedEnvironmentIds = new Set<string>(
32
+ environments
33
+ .filter((environment) => environment.isEnabled && environment.connectionState === "connected")
34
+ .map((environment) => environment.environmentId),
35
+ );
36
const own = projects.find(
37
(project) => project.environmentId === target.environmentId && project.id === target.projectId,
38
);
apps/mobile/src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx
new
+143
@@ -0,0 +1,143 @@
1
+import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2
+import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
3
+import { EnvironmentId } from "@t3tools/contracts";
4
+import { act, useEffect, useState } from "react";
5
+import { createRoot } from "react-dom/client";
6
+import { expect, it, vi } from "vite-plus/test";
7
+
8
+import type { WorkspaceEnvironment } from "../../state/workspaceModel";
9
+import type { PullRequestEnvironmentTarget } from "./pullRequestEnvironmentFallback";
10
+
11
+const mocks = vi.hoisted(() => ({
12
+ environments: [] as ReadonlyArray<WorkspaceEnvironment>,
13
+ isReady: true,
14
+ projects: [] as ReadonlyArray<EnvironmentProject>,
15
+ navigation: { setParams: vi.fn<(params: Partial<PullRequestEnvironmentTarget>) => void>() },
16
+}));
17
+
18
+vi.mock("@react-navigation/native", () => ({ useNavigation: () => mocks.navigation }));
19
+vi.mock("../../state/entities", () => ({ useProjects: () => mocks.projects }));
20
+vi.mock("../../state/workspace", () => ({
21
+ useWorkspaceEnvironments: () => mocks.environments,
22
+ useConnectionsReady: () => mocks.isReady,
23
+}));
24
+
25
+import { usePullRequestEnvironmentFallback } from "./usePullRequestEnvironmentFallback";
26
+
27
+function environment(
28
+ environmentId: string,
29
+ connectionState: EnvironmentConnectionPhase,
30
+ isEnabled = true,
31
+): WorkspaceEnvironment {
32
+ return {
33
+ environmentId: EnvironmentId.make(environmentId),
34
+ environmentLabel: environmentId,
35
+ displayUrl: "",
36
+ isRelayManaged: false,
37
+ isEnabled,
38
+ connectionState,
39
+ connectionError: null,
40
+ connectionErrorTraceId: null,
41
+ };
42
+}
43
+
44
+function setup() {
45
+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
46
+ mocks.isReady = true;
47
+ mocks.environments = [environment("requested", "connecting"), environment("copy", "connected")];
48
+ mocks.projects = [
49
+ { environmentId: "requested", id: "original" },
50
+ { environmentId: "copy", id: "copy-project" },
51
+ ].map(
52
+ (project) =>
53
+ ({
54
+ ...project,
55
+ repositoryIdentity: { canonicalKey: "github.com/owner/repo" },
56
+ }) as unknown as EnvironmentProject,
57
+ );
58
+ const container = document.createElement("div");
59
+ const root = createRoot(container);
60
+ mocks.navigation.setParams = vi.fn();
61
+ function Probe() {
62
+ const [params, updateParams] = useState({
63
+ environmentId: "requested",
64
+ projectId: "original",
65
+ repository: "owner/repo",
66
+ host: "github.com",
67
+ });
68
+ useEffect(() => {
69
+ mocks.navigation.setParams.mockImplementation((next) =>
70
+ updateParams((previous) => ({ ...previous, ...next })),
71
+ );
72
+ }, []);
73
+ usePullRequestEnvironmentFallback(params);
74
+ return <span>{`${params.environmentId}/${params.projectId}`}</span>;
75
+ }
76
+ return {
77
+ render() {
78
+ act(() => root.render(<Probe />));
79
+ },
80
+ get target() {
81
+ return container.textContent;
82
+ },
83
+ [Symbol.dispose]() {
84
+ act(() => root.unmount());
85
+ },
86
+ };
87
+}
88
+
89
+it("keeps the requested target while the catalog initializes or its enabled connection is pending", () => {
90
+ using screen = setup();
91
+ mocks.isReady = false;
92
+ mocks.environments = [environment("copy", "connected")];
93
+ screen.render();
94
+ expect(screen.target).toBe("requested/original");
95
+ expect(mocks.navigation.setParams).not.toHaveBeenCalled();
96
+
97
+ mocks.isReady = true;
98
+ for (const phase of ["available", "connecting", "reconnecting", "connected"] as const) {
99
+ mocks.environments = [environment("requested", phase), environment("copy", "connected")];
100
+ screen.render();
101
+ expect(screen.target).toBe("requested/original");
102
+ expect(mocks.navigation.setParams).not.toHaveBeenCalled();
103
+ }
104
+});
105
+
106
+it("falls back to a connected repository copy when the requested environment cannot connect", () => {
107
+ for (const state of ["missing", "disabled", "offline", "error", "unsupported"] as const) {
108
+ using screen = setup();
109
+ mocks.environments = [
110
+ ...(state === "missing"
111
+ ? []
112
+ : [
113
+ environment(
114
+ "requested",
115
+ state === "disabled" ? "available" : state,
116
+ state !== "disabled",
117
+ ),
118
+ ]),
119
+ environment("copy", "connected"),
120
+ ];
121
+ screen.render();
122
+ expect(screen.target).toBe("copy/copy-project");
123
+ expect(mocks.navigation.setParams).toHaveBeenCalledExactlyOnceWith({
124
+ environmentId: "copy",
125
+ projectId: "copy-project",
126
+ });
127
+ }
128
+});
129
+
130
+it("does not replace a connecting target with a connected repository copy before its project hydrates", () => {
131
+ using screen = setup();
132
+ const projects = mocks.projects;
133
+ mocks.projects = projects.filter((project) => project.environmentId !== "requested");
134
+ screen.render();
135
+ expect(screen.target).toBe("requested/original");
136
+ expect(mocks.navigation.setParams).not.toHaveBeenCalled();
137
+
138
+ mocks.projects = projects;
139
+ mocks.environments = [environment("requested", "connected"), environment("copy", "connected")];
140
+ screen.render();
141
+ expect(screen.target).toBe("requested/original");
142
+ expect(mocks.navigation.setParams).not.toHaveBeenCalled();
143
+});
apps/mobile/src/features/pull-requests/usePullRequestEnvironmentFallback.ts
+5
-7
@@ -2,7 +2,7 @@ import { useNavigation } from "@react-navigation/native";
2
import { useEffect, useMemo } from "react";
3
4
import { useProjects } from "../../state/entities";
5
-import { useWorkspaceEnvironments } from "../../state/workspace";
5
+import { useConnectionsReady, useWorkspaceEnvironments } from "../../state/workspace";
6
import {
7
pullRequestFallbackEnvironment,
8
type PullRequestEnvironmentTarget,
@@ -11,13 +11,11 @@ import {
11
export function usePullRequestEnvironmentFallback(params: PullRequestEnvironmentTarget) {
12
const navigation = useNavigation();
13
const projects = useProjects();
14
- const connected = useWorkspaceEnvironments()
15
- .filter((environment) => environment.connectionState === "connected")
16
- .map((environment) => String(environment.environmentId))
17
- .join("\n");
14
+ const isReady = useConnectionsReady();
15
+ const environments = useWorkspaceEnvironments();
16
const fallback = useMemo(
19
- () => pullRequestFallbackEnvironment(params, projects, new Set(connected.split("\n"))),
20
- [params, projects, connected],
17
+ () => (isReady ? pullRequestFallbackEnvironment(params, projects, environments) : null),
18
+ [params, projects, environments, isReady],
19
);
20
useEffect(() => {
21
if (fallback) navigation.setParams(fallback);
apps/mobile/vite.config.ts
+1
@@ -2,6 +2,7 @@ import { defineConfig } from "vite-plus";
2
import "vite-plus/test/config";
3
4
const reactTests = [
5
+ "src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx",
6
"src/lib/filePreview.test.ts",
7
"src/features/diffs/nativeReviewDiffSurface.test.tsx",
8
"src/features/pull-requests/usePullRequestDiff.test.tsx",