diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx index 8216a08c31f85..15a4018073da2 100644 --- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx @@ -629,17 +629,19 @@ function AdaptiveWorkspaceLayoutContent( .filter((environment) => environment.connectionState === "connected") .map((environment) => environment.environmentId), ); + const currentProject = environmentId && projectId ? { environmentId, projectId } : undefined; handleSelectThreadRoute( newTaskDestination({ projects, requestId: uuidv4(), useLastProject: saved?.useLastProjectForNewThreads, - currentProject: environmentId && projectId ? { environmentId, projectId } : undefined, + currentProject, filteredProject: filteredScope ? newTaskFilteredProject( filteredScope, listEnvironmentId, connectedEnvironmentIds, + currentProject, saved?.lastThreadProject, ) : undefined, 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.ts index 3105158237d67..96bb77ac357fa 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 @@ -88,6 +88,7 @@ describe("newTaskFilteredProject", () => { makeScope([mac, server]), null, new Set([mac.environmentId, server.environmentId]), + undefined, { environmentId: "mac", projectId: "t3code" }, ), ).toEqual({ environmentId: "server", projectId: "t3code" }); @@ -98,7 +99,7 @@ describe("newTaskFilteredProject", () => { const server = makeProject("t3code", "server"); const connected = new Set([mac.environmentId, server.environmentId]); const filtered = (last?: { readonly environmentId: string; readonly projectId: string }) => - newTaskFilteredProject(makeScope([mac, server]), null, connected, last); + newTaskFilteredProject(makeScope([mac, server]), null, connected, undefined, last); expect(filtered({ environmentId: "server", projectId: "t3code" })).toEqual({ environmentId: "server", projectId: "t3code", @@ -108,6 +109,20 @@ describe("newTaskFilteredProject", () => { projectId: "t3code", }); }); + + it("starts in the current thread's copy of the filtered project", () => { + const mac = makeProject("t3code", "mac"); + const server = makeProject("t3code", "server"); + const connected = new Set([mac.environmentId, server.environmentId]); + const remembered = { environmentId: "mac", projectId: "t3code" }; + const filtered = (current: { readonly environmentId: string; readonly projectId: string }) => + newTaskFilteredProject(makeScope([mac, server]), null, connected, current, remembered); + expect(filtered({ environmentId: "server", projectId: "t3code" })).toEqual({ + environmentId: "server", + projectId: "t3code", + }); + expect(filtered({ environmentId: "server", projectId: "other" })).toEqual(remembered); + }); }); describe("resolveEnvironmentProjectMatch", () => { @@ -252,15 +267,26 @@ describe("newTaskDestination", () => { }); } - it("starts in the current thread's project ahead of the filtered and remembered ones", () => { - const projects = [makeProject("remembered"), makeProject("current", "other-environment")]; + it("starts in the thread list's filtered project ahead of a current thread outside it", () => { + const projects = [makeProject("filtered"), makeProject("current", "other-environment")]; + const last = { environmentId: "environment", projectId: "remembered" }; expect( destination({ useLastProject: true, - projects, + projects: [...projects, makeProject("remembered")], current: projects[1], filtered: projects[0], - last: { environmentId: "environment", projectId: "remembered" }, + last, + }), + ).toEqual({ + draft: { requestId: "request", environmentId: "environment", projectId: "filtered" }, + }); + expect( + destination({ + useLastProject: true, + projects: [...projects, makeProject("remembered")], + current: projects[1], + last, }), ).toEqual({ draft: { requestId: "request", environmentId: "other-environment", projectId: "current" }, diff --git a/apps/mobile/src/features/threads/new-task-project-selection.ts b/apps/mobile/src/features/threads/new-task-project-selection.ts index 58f7adcf27b64..550ee4d812e4b 100644 --- a/apps/mobile/src/features/threads/new-task-project-selection.ts +++ b/apps/mobile/src/features/threads/new-task-project-selection.ts @@ -176,21 +176,27 @@ export function resolveDraftProjectSelection( type ProjectRef = { readonly environmentId: string; readonly projectId: string }; -/** The live copy of the thread list's filtered project a new thread starts in: the remembered one if it is a copy. */ +const isProjectRef = (project: EnvironmentProject, ref: ProjectRef | undefined) => + project.environmentId === ref?.environmentId && project.id === ref.projectId; + +/** + * The copy of the thread list's filtered project a new thread starts in: the current thread's + * if it is a copy, else the remembered live one, else a live one on a connected machine. + */ export function newTaskFilteredProject( scope: HomeProjectScope, preferredEnvironmentId: EnvironmentId | null, connectedEnvironmentIds: ReadonlySet, + currentProject: ProjectRef | undefined, lastProject: ProjectRef | undefined, ): ProjectRef | undefined { + if (scope.projects.some((project) => isProjectRef(project, currentProject))) { + return currentProject; + } const live = scope.projects.filter((project) => project.archivedAt == null); if (live.length === 0) return undefined; const project = - live.find( - (candidate) => - candidate.environmentId === lastProject?.environmentId && - candidate.id === lastProject.projectId, - ) ?? + live.find((candidate) => isProjectRef(candidate, lastProject)) ?? getProjectScopeSelectionTarget( { ...scope, projects: live }, preferredEnvironmentId, @@ -212,13 +218,14 @@ export function newTaskDestination(options: { options.projects.find( (project) => project.environmentId === ref.environmentId && project.id === ref.projectId, ); + // The list filter is the user's explicit choice, so a current thread outside it doesn't win. // A remembered project that was archived since opens the picker; the current one still wins. const last = find(options.lastProject); const project = options.useLastProject === false ? undefined - : (find(options.currentProject) ?? - find(options.filteredProject) ?? + : (find(options.filteredProject) ?? + find(options.currentProject) ?? (last?.archivedAt == null ? last : undefined)); return project ? { diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index 38dd4684723f9..e020a54056d6f 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -12,8 +12,8 @@ your model and mode selections, unless the destination project has its own model Its branch and workspace mode come from your configured defaults. To continue in an existing worktree, use **New thread in this worktree** from the branch toolbar. -On mobile, **New thread** starts in the current thread's project, or else in the -project the thread list is filtered to, or else in the last project you opened +On mobile, **New thread** starts in the project the thread list is filtered to, +or else in the current thread's project, or else in the last project you opened or chose on that device. If none is available, the project picker opens. Turn off **Settings → General → Use last project** to choose a project every time. When you pick a project that is on more than one machine, the thread starts on