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 21, 17:15 UTC
fix(mobile): adding a folder that is already a project opens it (#34942)
Julius Marminge committed
Nov 13, 2026 at 11:57 UTC
e94db17ead0e52813b38c0b67537843b44634d44
2 files changed
+178
-44
apps/mobile/src/features/projects/AddProjectScreen.test.ts
+74
-5
@@ -6,7 +6,11 @@ import * as NodeModule from "node:module";
6
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
7
import { workspaceDetailRouter } from "../layout/workspace/workspace-detail-selection";
8
import type { NavigationState } from "@react-navigation/native";
9
-import { AuthOrchestrationOperateScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
9
+import {
10
+ AuthFilesystemReadScope,
11
+ AuthOrchestrationOperateScope,
12
+ AuthSourceControlWriteScope,
13
+} from "@t3tools/contracts";
14
import * as Cause from "effect/Cause";
15
import { AsyncResult } from "effect/reactivity";
16
import { act, createElement, isValidElement, type ReactNode } from "react";
@@ -22,7 +26,9 @@ const state = vi.hoisted(() => ({
26
id: string;
27
title: string;
28
workspaceRoot: string;
29
+ repositoryIdentity?: { canonicalKey: string };
30
}[],
31
+ cloneDestinations: [] as string[],
32
createResult: null as Promise<unknown> | null,
33
createNewResult: null as Promise<unknown> | null,
34
startCloneResult: null as Promise<unknown> | null,
@@ -134,7 +140,15 @@ vi.mock("../../state/use-remote-environment-registry", () => ({
140
}),
141
}));
142
vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command }));
137
-vi.mock("../../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => () => {} }));
143
+// Only filesystem.browse of `~` answers, with the host's home as its parentPath.
144
+vi.mock("../../state/use-atom-query-runner", () => ({
145
+ useAtomQueryRunner:
146
+ () =>
147
+ async ({ input }: { input?: { partialPath?: string } }) =>
148
+ input?.partialPath === "~"
149
+ ? AsyncResult.success({ parentPath: "/home/tester", entries: [] })
150
+ : undefined,
151
+}));
152
vi.mock("../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) }));
153
vi.mock("../../state/presentation", () => ({
154
useEnvironmentPresentation: () => ({ isReady: true, presentation: null }),
@@ -144,6 +158,7 @@ vi.mock("../../state/sourceControl", () => ({
158
sourceControlEnvironment: {
159
discovery: (input: unknown) => input,
160
startProjectClone: async ({ input }: { input: { destinationPath: string } }) => {
161
+ state.cloneDestinations.push(input.destinationPath);
162
if (state.startCloneResult) return state.startCloneResult;
163
NodeFS.mkdirSync(input.destinationPath);
164
return AsyncResult.success(undefined);
@@ -230,7 +245,7 @@ function cloneAction() {
245
renderScreen(() =>
246
AddProjectDestinationScreen({
247
environmentId: "environment",
233
- remoteUrl: "https://example.com/repo.git",
248
+ remoteUrl: "https://example.com/owner/repo.git",
249
repositoryName: "repo",
250
}),
251
),
@@ -274,6 +289,38 @@ describe("clone project permissions", () => {
289
expect(state.projects).toEqual([]);
290
},
291
);
292
+
293
+ it.each([
294
+ [
295
+ "opens the existing project of that repository instead of cloning into its folder",
296
+ "example.com/owner/repo",
297
+ false,
298
+ ],
299
+ ["leaves a folder holding another repository to the clone", "example.com/owner/other", true],
300
+ ])("%s", async (_name, canonicalKey, clones) => {
301
+ state.scopes.add(AuthOrchestrationOperateScope);
302
+ state.cloneDestinations = [];
303
+ state.startCloneResult = Promise.resolve(
304
+ AsyncResult.failure(Cause.fail(new Error("Destination is not empty"))),
305
+ );
306
+ state.existingProjects = [
307
+ {
308
+ environmentId: "environment",
309
+ id: "existing",
310
+ title: "repo",
311
+ workspaceRoot: NodePath.join(state.baseDirectory, "repo"),
312
+ repositoryIdentity: { canonicalKey },
313
+ },
314
+ ];
315
+ try {
316
+ await cloneAction()();
317
+
318
+ expect(state.cloneDestinations.length > 0).toBe(clones);
319
+ } finally {
320
+ state.existingProjects = [];
321
+ state.startCloneResult = null;
322
+ }
323
+ });
324
});
325
326
function mountProjectFlow(
@@ -488,10 +535,10 @@ describe("add project source environment", () => {
535
});
536
537
describe("local project navigation", () => {
491
- function setup() {
538
+ function setup(baseDirectory = "/tmp/t3-mobile-audit-fixture") {
539
state.navigationState = undefined;
540
state.scopes = new Set([AuthOrchestrationOperateScope]);
494
- state.baseDirectory = "/tmp/t3-mobile-audit-fixture";
541
+ state.baseDirectory = baseDirectory;
542
state.projects = [];
543
state.dispatch.mockReset();
544
state.alert.mockReset();
@@ -523,6 +570,28 @@ describe("local project navigation", () => {
570
);
571
});
572
573
+ it("opens an existing project typed as a home-relative path instead of creating it", async () => {
574
+ state.existingProjects = [
575
+ {
576
+ environmentId: "environment",
577
+ id: "existing",
578
+ title: "bike-documents",
579
+ workspaceRoot: "/home/tester/projects/bike-documents",
580
+ },
581
+ ];
582
+ try {
583
+ const { created, projected, submit } = setup("~/projects/bike-documents/");
584
+ state.scopes.add(AuthFilesystemReadScope);
585
+ created.resolve(AsyncResult.success(undefined));
586
+ projected.resolve(null);
587
+ await submit();
588
+
589
+ expect(state.projects).toEqual([]);
590
+ } finally {
591
+ state.existingProjects = [];
592
+ }
593
+ });
594
+
595
it("does not replace a reopened flow when the previous submission finishes", async () => {
596
const { created, projected, submit } = setup();
597
const navigation = mountProjectFlow(false);
apps/mobile/src/features/projects/AddProjectScreen.tsx
+104
-39
@@ -387,7 +387,26 @@ function useBrowsePathInput(
387
[browseNavigation],
388
);
389
390
- return { isBrowseNavigating, pathInput, setPathInput, navigateToBrowsePath };
390
+ // Registered roots are absolute and `~` expands on the host, so ask the host for its home.
391
+ const resolveHomePath = async (path: string): Promise<string> => {
392
+ if (
393
+ !/^~(?:[\\/]|$)/.test(path) ||
394
+ !environment ||
395
+ !readEnvironmentScope(environment.environmentId, AuthFilesystemReadScope) ||
396
+ !canPreloadBrowsePath(environmentRuntime?.connectionState)
397
+ ) {
398
+ return path;
399
+ }
400
+ const home = await loadBrowsePath({
401
+ environmentId: environment.environmentId,
402
+ input: { partialPath: "~" },
403
+ });
404
+ return AsyncResult.isSuccess(home) && home.value
405
+ ? `${home.value.parentPath.replace(/[\\/]+$/, "")}${path.slice(1)}`
406
+ : path;
407
+ };
408
+
409
+ return { isBrowseNavigating, pathInput, setPathInput, navigateToBrowsePath, resolveHomePath };
410
}
411
412
function useEnvironmentOptions(
@@ -730,16 +749,76 @@ async function draftWithPrompt(
749
return draftId;
750
}
751
733
-function useCreateProject(environment: EnvironmentOption | null) {
752
+function useOpenExistingProject(environment: EnvironmentOption | null) {
753
const openNewTaskDraft = useOpenNewTaskDraft();
735
- const navigation = useNavigation();
736
- const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });
754
const updateProject = useAtomCommand(projectEnvironment.update, {
755
label: "project unarchive",
756
});
757
const projects = useProjects();
758
742
- return async (workspaceRoot: string, originRoute = getActiveRoute(navigation)) => {
759
+ return async (
760
+ path: string,
761
+ originRoute: ReturnType<typeof getActiveRoute>,
762
+ clone?: { readonly remoteUrl: string; readonly prompt: string | null },
763
+ ): Promise<boolean> => {
764
+ if (!environment) return false;
765
+ const existing = findExistingAddProject({
766
+ projects,
767
+ environmentId: environment.environmentId,
768
+ path,
769
+ });
770
+ if (!existing) return false;
771
+ // A folder holding another repository is not this clone; let the clone report it occupied.
772
+ if (
773
+ clone &&
774
+ existing.repositoryIdentity?.canonicalKey !==
775
+ sourceControlClients.repositoryKeyFromRemote(clone.remoteUrl)
776
+ ) {
777
+ return false;
778
+ }
779
+ const prompt = clone?.prompt;
780
+ // Adding an archived project's folder again brings the project back.
781
+ const unarchived =
782
+ existing.archivedAt == null ||
783
+ (
784
+ await updateProject({
785
+ environmentId: existing.environmentId,
786
+ input: { projectId: existing.id, archived: false },
787
+ })
788
+ )._tag === "Success";
789
+ Dialog.alert(
790
+ "Project already exists",
791
+ unarchived ? existing.title : `${existing.title} is still archived. Try again later.`,
792
+ );
793
+ openNewTaskDraft(
794
+ {
795
+ environmentId: existing.environmentId,
796
+ projectId: existing.id,
797
+ title: existing.title,
798
+ ...(prompt
799
+ ? await draftWithPrompt(existing.environmentId, existing.id, prompt).then(
800
+ (draftId) => ({ draftId }),
801
+ () => ({}),
802
+ )
803
+ : {}),
804
+ },
805
+ originRoute,
806
+ );
807
+ return true;
808
+ };
809
+}
810
+
811
+function useCreateProject(environment: EnvironmentOption | null) {
812
+ const openNewTaskDraft = useOpenNewTaskDraft();
813
+ const navigation = useNavigation();
814
+ const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });
815
+ const openExistingProject = useOpenExistingProject(environment);
816
+
817
+ return async (
818
+ workspaceRoot: string,
819
+ originRoute = getActiveRoute(navigation),
820
+ lookupPath = workspaceRoot,
821
+ ) => {
822
if (
823
!environment ||
824
!canCreateProjectInEnvironment(environment.connectionState) ||
@@ -752,33 +831,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
831
return;
832
}
833
755
- const existing = findExistingAddProject({
756
- projects,
757
- environmentId: environment.environmentId,
758
- path: workspaceRoot,
759
- });
760
- if (existing) {
761
- // Adding an archived project's folder again brings the project back.
762
- const unarchived =
763
- existing.archivedAt == null ||
764
- (
765
- await updateProject({
766
- environmentId: existing.environmentId,
767
- input: { projectId: existing.id, archived: false },
768
- })
769
- )._tag === "Success";
770
- Dialog.alert(
771
- "Project already exists",
772
- unarchived ? existing.title : `${existing.title} is still archived. Try again later.`,
773
- );
774
- openNewTaskDraft(
775
- {
776
- environmentId: existing.environmentId,
777
- projectId: existing.id,
778
- title: existing.title,
779
- },
780
- originRoute,
781
- );
834
+ if (await openExistingProject(lookupPath, originRoute)) {
835
return;
836
}
837
@@ -1313,8 +1366,9 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
1366
environment?.environmentId ?? null,
1367
AuthOrchestrationOperateScope,
1368
);
1369
+ const navigation = useNavigation();
1370
const createProject = useCreateProject(environment);
1317
- const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } =
1371
+ const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput, resolveHomePath } =
1372
useBrowsePathInput(environment);
1373
const [isSubmitting, setIsSubmitting] = useState(false);
1374
const [error, setError] = useState<string | null>(null);
@@ -1333,7 +1387,11 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
1387
}
1388
1389
setIsSubmitting(true);
1336
- const result = await createProject(resolved.path);
1390
+ const originRoute = getActiveRoute(navigation);
1391
+ const lookupPath = resolved.path.startsWith("~")
1392
+ ? await resolveHomePath(resolved.path)
1393
+ : resolved.path;
1394
+ const result = await createProject(resolved.path, originRoute, lookupPath);
1395
if (result && AsyncResult.isFailure(result)) {
1396
setError(errorMessage(Cause.squash(result.cause)));
1397
}
@@ -1404,11 +1462,9 @@ export function AddProjectDestinationScreen(props: {
1462
? getCloneDirectoryPath(remoteUrl, true, repositoryName)
1463
: null;
1464
const cloneDirectoryName = ghqDirectoryPath?.split("/").at(-1) ?? repositoryName;
1407
- const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } = useBrowsePathInput(
1408
- environment,
1409
- cloneDirectoryName,
1410
- ghqDirectoryPath ?? repositoryName,
1411
- );
1465
+ const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput, resolveHomePath } =
1466
+ useBrowsePathInput(environment, cloneDirectoryName, ghqDirectoryPath ?? repositoryName);
1467
+ const openExistingProject = useOpenExistingProject(environment);
1468
const [isSubmitting, setIsSubmitting] = useState(false);
1469
const [error, setError] = useState<string | null>(null);
1470
@@ -1436,6 +1492,15 @@ export function AddProjectDestinationScreen(props: {
1492
1493
const originRoute = getActiveRoute(navigation);
1494
setIsSubmitting(true);
1495
+ if (
1496
+ await openExistingProject(await resolveHomePath(resolved.path), originRoute, {
1497
+ remoteUrl,
1498
+ prompt: prompt ?? null,
1499
+ })
1500
+ ) {
1501
+ setIsSubmitting(false);
1502
+ return;
1503
+ }
1504
const title = inferProjectTitleFromPath(resolved.path);
1505
const startResult = await startProjectClone({
1506
environmentId: environment.environmentId,