TypeScript
fix(mobile): picking a project on two machines starts on the connected one (#25839)
Theo Browne00c636e4b02a
diff --git a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsxindex cae0f7771a4..611eb3c4d6d 100644--- a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx+++ b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx@@ -188,6 +188,11 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara) ?? null): null;const { connectedEnvironments } = useRemoteConnectionStatus();+ const connectedEnvironmentIds = new Set(+ connectedEnvironments+ .filter((environment) => environment.connectionState === "connected")+ .map((environment) => environment.environmentId),+ );const openScratch = useAtomCommand(projectEnvironment.openScratch, {reportFailure: false,});@@ -461,6 +466,7 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRouteParaconst selectionTarget = getProjectScopeSelectionTarget(scope,selectedEnvironmentId,+ connectedEnvironmentIds,);if (Platform.OS === "android") {return (diff --git a/apps/mobile/src/features/threads/new-task-project-selection.test.ts b/apps/mobile/src/features/threads/new-task-project-selection.test.tsindex 04bcfe317cf..34357436b60 100644--- a/apps/mobile/src/features/threads/new-task-project-selection.test.ts+++ b/apps/mobile/src/features/threads/new-task-project-selection.test.ts@@ -4,6 +4,7 @@ import { describe, expect, it } from "vite-plus/test";import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";import type { HomeProjectScope } from "../home/homeThreadList";import {+ getProjectScopeSelectionTarget,newTaskDestination,resolveDraftProjectSelection,resolveEnvironmentProjectMatch,@@ -53,6 +54,26 @@ function makeScope(projects: ReadonlyArray<EnvironmentProject>): HomeProjectScop};}+describe("getProjectScopeSelectionTarget", () => {+ it("prefers a connected copy of a project that spans environments", () => {+ const mac = makeProject("t3code", "mac");+ const server = makeProject("t3code", "server");+ const target = (connected: ReadonlyArray<string>, preferred: string | null = null) =>+ getProjectScopeSelectionTarget(+ makeScope([mac, server]),+ preferred === null ? null : EnvironmentId.make(preferred),+ new Set(connected.map((id) => EnvironmentId.make(id))),+ );+ expect(target(["server"])).toBe(server);+ expect(target(["server"], "mac")).toBe(server);+ expect(target(["mac", "server"], "server")).toBe(server);+ expect(target(["mac", "server"])).toBe(mac);+ // With nothing connected, the usual order still applies.+ expect(target([], "server")).toBe(server);+ expect(target([])).toBe(mac);+ });+});+describe("resolveEnvironmentProjectMatch", () => {it("follows the same repository onto the target machine", () => {const selected = makeProject("t3code", "mac", { repositoryKey: "github.com/t3tools/t3code" });diff --git a/apps/mobile/src/features/threads/new-task-project-selection.ts b/apps/mobile/src/features/threads/new-task-project-selection.tsindex aabc7929380..99167a04e38 100644--- a/apps/mobile/src/features/threads/new-task-project-selection.ts+++ b/apps/mobile/src/features/threads/new-task-project-selection.ts@@ -10,12 +10,21 @@ type DraftProjectSelectionResolution =| { readonly kind: "pick" }| { readonly kind: "dismiss" };+/**+ * The copy of a project a new thread starts in: the preferred environment's, then the+ * representative. A connected copy wins over a disconnected one.+ */export function getProjectScopeSelectionTarget(scope: HomeProjectScope,preferredEnvironmentId: EnvironmentId | null,+ connectedEnvironmentIds: ReadonlySet<EnvironmentId>,): EnvironmentProject {+ const rank = (project: EnvironmentProject) =>+ project.environmentId === preferredEnvironmentId ? 0 : project === scope.representative ? 1 : 2;+ const ordered = [...scope.projects].sort((left, right) => rank(left) - rank(right));return (- scope.projects.find((project) => project.environmentId === preferredEnvironmentId) ??+ ordered.find((project) => connectedEnvironmentIds.has(project.environmentId)) ??+ ordered[0] ??scope.representative);}diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.mdindex a61fd40c9bc..8cfee144b44 100644--- a/docs/user/thread-sidebar.md+++ b/docs/user/thread-sidebar.md@@ -13,7 +13,8 @@ an existing worktree, use **New thread in this worktree** from the branch toolbaOn mobile, **New thread** starts in the current thread's project, or else in thelast project you opened or chose on that device. If neither is available, theproject picker opens. Turn off **Settings → General → Use last project** to-choose a project every time.+choose a project every time. When you pick a project that is on more than one+machine, the thread starts on a connected one if any is connected.When you change a new thread's project, T3 Code stays in the current environmentif that project exists there. Otherwise it selects an environment that has it.