Skip to content

Commit 91e7239

Browse files
committed
feat(mobile): make new-thread project reuse a local preference
1 parent 2ee0456 commit 91e7239

10 files changed

Lines changed: 265 additions & 97 deletions

‎apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -556,25 +556,19 @@ function AdaptiveWorkspaceLayoutContent(
556556
const thread = appAtomRegistry
557557
.get(environmentThreadShells.navigationThreadShellsAtom)
558558
.find((candidate) => candidate.environmentId === environmentId && candidate.id === threadId);
559-
const currentRoute = navigation
560-
.getState()
561-
?.routes.find((route) => route.key === props.workspaceRouteKey);
562-
const draft =
563-
currentRoute?.name === "Thread"
564-
? (currentRoute.params as ReactNavigation.RootParamList["Thread"])?.draft
565-
: undefined;
566-
const first = (value: string | string[] | undefined) =>
567-
Array.isArray(value) ? value[0] : value;
568-
const draftEnvironmentId = first(draft?.environmentId);
569-
const draftProjectId = first(draft?.projectId);
570559
const currentProject = thread
571560
? { environmentId: thread.environmentId, id: thread.projectId }
572-
: projects.find(
573-
(project) =>
574-
project.environmentId === draftEnvironmentId && project.id === draftProjectId,
575-
);
561+
: undefined;
562+
const preferences = appAtomRegistry.get(mobilePreferencesAtom);
563+
const saved = AsyncResult.isSuccess(preferences) ? preferences.value : undefined;
576564
handleSelectThreadRoute(
577-
newTaskDestination(layout.usesSplitView, projects, currentProject, uuidv4()),
565+
newTaskDestination(
566+
saved?.useLastProjectForNewThreads,
567+
projects,
568+
currentProject,
569+
uuidv4(),
570+
saved?.lastThreadProject,
571+
),
578572
);
579573
};
580574

‎apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function SettingsThreadsRouteScreen() {
4646
contentContainerClassName="gap-6 px-5 pt-4"
4747
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
4848
>
49+
<NewThreadSettingsSection />
4950
<AutoSettleSettingsRows />
5051
<BetaSettingsSection />
5152
<LegacySettingsSection />
@@ -55,6 +56,30 @@ export function SettingsThreadsRouteScreen() {
5556
);
5657
}
5758

59+
function NewThreadSettingsSection() {
60+
const preferences = useAtomValue(mobilePreferencesAtom);
61+
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
62+
const ready = AsyncResult.isSuccess(preferences) && !preferences.waiting;
63+
64+
return (
65+
<SettingsSection
66+
title="New threads"
67+
footer="Start in the last project you used on this device. When off, choose a project each time."
68+
>
69+
<SettingsSwitchRow
70+
icon="square.and.pencil"
71+
label="Use last project for new threads"
72+
value={
73+
AsyncResult.isSuccess(preferences) &&
74+
preferences.value.useLastProjectForNewThreads === true
75+
}
76+
disabled={!ready}
77+
onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })}
78+
/>
79+
</SettingsSection>
80+
);
81+
}
82+
5883
const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
5984

6085
function AutoSettleSettingsRows() {

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useWorkspaceBackNavigation } from "../workspace/useAndroidWorkspaceNavi
88
import { submittedThreadParams } from "./thread-route-state";
99
import { ThreadScreenBinding, ThreadScreenFallback } from "./ThreadScreenHost";
1010
import { draftThreadShell } from "./draft-thread-shell";
11+
import { useRememberThreadProject } from "./useRememberThreadProject";
1112
import { connectionTone } from "../connection/connectionTone";
1213
import { AppBar } from "../../components/ui/AppBar";
1314
import { ThreadId } from "@t3tools/contracts";
@@ -166,6 +167,12 @@ export function NewTaskDraftScreen(props: {
166167
reserveShare,
167168
} = useIncomingShare();
168169
const { projectScopes, selectedProject, selectedProjectKey, setProject } = flow;
170+
useRememberThreadProject(
171+
selectedProject &&
172+
selectedProjectKey === `${selectedProject.environmentId}:${selectedProject.id}`
173+
? selectedProject
174+
: null,
175+
);
169176
const selectedProjectLabel = (() => {
170177
if (!selectedProject) return null;
171178
const scope = projectScopes.find((candidate) =>
@@ -498,18 +505,12 @@ export function NewTaskDraftScreen(props: {
498505
return;
499506
}
500507

501-
const selection = resolveDraftProjectSelection(selectedProjectKey, projects, projectScopes);
508+
const selection = resolveDraftProjectSelection(selectedProjectKey, projects);
502509
if (selection.kind === "preserve") {
503510
return;
504511
}
505-
if (selection.kind === "select") {
506-
setProject(selection.project);
507-
return;
508-
}
509-
510512
navigation.dispatch(CommonActions.navigate("NewTaskProject"));
511513
}, [
512-
projectScopes,
513514
projects,
514515
flow.draftKey,
515516
props.initialProjectRef,

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Dialog from "../../components/ui/Dialog";
2+
import { useRememberThreadProject } from "./useRememberThreadProject";
23
import { useOpenPullRequest } from "../../native/useOpenPullRequest";
34
import { makeTurnCommandMetadata } from "../../lib/commandMetadata";
45
import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTurn";
@@ -288,6 +289,7 @@ function ThreadRouteContent(
288289
selectedThreadProject,
289290
selectedEnvironmentConnection,
290291
} = useThreadSelection();
292+
useRememberThreadProject(selectedThreadProject);
291293
const canOperateThread = useEnvironmentScope(
292294
selectedThread?.environmentId ?? null,
293295
AuthOrchestrationOperateScope,

‎apps/mobile/src/features/threads/new-task-project-selection.test.ts‎

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { EnvironmentId, ProjectId } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

44
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
5-
import type { HomeProjectScope } from "../home/homeThreadList";
65
import {
76
newTaskDestination,
87
resolveDraftProjectSelection,
@@ -40,19 +39,6 @@ function makeProject(
4039
};
4140
}
4241

43-
function makeScope(projects: ReadonlyArray<EnvironmentProject>): HomeProjectScope {
44-
return {
45-
key: "github.com/t3tools/t3code",
46-
title: "T3 Code",
47-
representative: projects[0]!,
48-
projects,
49-
projectRefs: projects.map((project) => ({
50-
environmentId: project.environmentId,
51-
projectId: project.id,
52-
})),
53-
};
54-
}
55-
5642
describe("resolveEnvironmentProjectMatch", () => {
5743
it("follows the same repository onto the target machine", () => {
5844
const selected = makeProject("t3code", "mac", { repositoryKey: "github.com/t3tools/t3code" });
@@ -95,60 +81,89 @@ describe("resolveEnvironmentProjectMatch", () => {
9581
});
9682

9783
describe("resolveDraftProjectSelection", () => {
98-
it("selects one logical project even when it has multiple physical workspaces", () => {
99-
const projects = [makeProject("t3code"), makeProject("t3code-2"), makeProject("t3code-3")];
100-
expect(resolveDraftProjectSelection(null, projects, [makeScope(projects)])).toEqual({
101-
kind: "select",
102-
project: projects[0],
84+
it("keeps the picker even when only one project is available", () => {
85+
expect(resolveDraftProjectSelection(null, [makeProject("only")])).toEqual({ kind: "pick" });
86+
});
87+
88+
it("preserves an explicit project selection", () => {
89+
expect(resolveDraftProjectSelection("environment:chosen", [makeProject("chosen")])).toEqual({
90+
kind: "preserve",
10391
});
10492
});
10593

106-
it("does not preserve a project key that is missing from the catalog", () => {
107-
const project = makeProject("t3code");
108-
expect(
109-
resolveDraftProjectSelection("environment:removed", [project], [makeScope([project])]),
110-
).toEqual({
111-
kind: "select",
112-
project,
94+
it("returns to the picker when the selected project is missing", () => {
95+
expect(resolveDraftProjectSelection("environment:removed", [makeProject("only")])).toEqual({
96+
kind: "pick",
11397
});
11498
});
11599
});
116100

117101
describe("newTaskDestination", () => {
118-
it("opens the current thread's project directly in split view", () => {
102+
const request = { draft: { requestId: "request" } };
103+
104+
it.each([undefined, false])("opens the picker when the preference is %s", (useLastProject) => {
119105
const projects = [makeProject("first"), makeProject("current", "other-environment")];
120-
expect(newTaskDestination(true, projects, projects[1], "request")).toEqual({
106+
expect(
107+
newTaskDestination(useLastProject, projects, projects[1], "request", {
108+
environmentId: "environment",
109+
projectId: "first",
110+
}),
111+
).toEqual(request);
112+
});
113+
114+
it("uses the current thread or draft project when enabled", () => {
115+
const projects = [makeProject("first"), makeProject("current", "other-environment")];
116+
expect(
117+
newTaskDestination(true, projects, projects[1], "request", {
118+
environmentId: "environment",
119+
projectId: "first",
120+
}),
121+
).toEqual({
121122
draft: { requestId: "request", environmentId: "other-environment", projectId: "current" },
122123
});
123124
});
124-
it.each([true, false])(
125-
"leaves the draft unassigned when there is no current project (split view: %s)",
126-
(usesSplitView) => {
127-
const projects = [makeProject("first"), makeProject("second")];
128-
expect(newTaskDestination(usesSplitView, projects, undefined, "request")).toEqual({
129-
draft: { requestId: "request" },
130-
});
131-
},
132-
);
133-
it.each([true, false])(
134-
"leaves the draft unassigned when the current project is stale (split view: %s)",
135-
(usesSplitView) => {
136-
const projects = [makeProject("first"), makeProject("second")];
137-
const currentProject = makeProject("first", "removed-environment");
138-
expect(newTaskDestination(usesSplitView, projects, currentProject, "request")).toEqual({
139-
draft: { requestId: "request" },
140-
});
141-
},
142-
);
143-
it("keeps the picker when there are no projects", () => {
144-
expect(newTaskDestination(true, [], undefined, "request")).toEqual({
145-
draft: { requestId: "request" },
125+
126+
it("remembers the last project from Home without relying on project order", () => {
127+
const projects = [makeProject("same-id"), makeProject("same-id", "other-environment")];
128+
expect(
129+
newTaskDestination(true, projects, undefined, "request", {
130+
environmentId: "other-environment",
131+
projectId: "same-id",
132+
}),
133+
).toEqual({
134+
draft: { requestId: "request", environmentId: "other-environment", projectId: "same-id" },
146135
});
147136
});
148-
it("opens the same draft route in compact view", () => {
149-
const project = makeProject("current");
150-
expect(newTaskDestination(false, [project], project, "request")).toEqual({
151-
draft: { requestId: "request", environmentId: "environment", projectId: "current" },
137+
138+
it("uses the remembered project when the current project is stale", () => {
139+
const project = makeProject("remembered");
140+
expect(
141+
newTaskDestination(true, [project], makeProject("removed"), "request", {
142+
environmentId: "environment",
143+
projectId: "remembered",
144+
}),
145+
).toEqual({
146+
draft: { requestId: "request", environmentId: "environment", projectId: "remembered" },
152147
});
153148
});
149+
150+
it.each([
151+
undefined,
152+
{ environmentId: "removed-environment", projectId: "first" },
153+
{ environmentId: "environment", projectId: "removed" },
154+
])("keeps the picker when the last project is missing or stale: %s", (lastProject) => {
155+
const projects = [makeProject("first"), makeProject("second")];
156+
expect(
157+
newTaskDestination(true, projects, makeProject("removed"), "request", lastProject),
158+
).toEqual(request);
159+
});
160+
161+
it("keeps the picker when no projects are available", () => {
162+
expect(
163+
newTaskDestination(true, [], undefined, "request", {
164+
environmentId: "environment",
165+
projectId: "removed",
166+
}),
167+
).toEqual(request);
168+
});
154169
});

‎apps/mobile/src/features/threads/new-task-project-selection.ts‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { repositoryGroupingKeyOf, type EnvironmentId } from "@t3tools/contracts"
44
import { scopedProjectKey } from "../../lib/scopedEntities";
55
import type { HomeProjectScope } from "../home/homeThreadList";
66

7-
type DraftProjectSelectionResolution =
8-
| { readonly kind: "preserve" }
9-
| { readonly kind: "select"; readonly project: EnvironmentProject }
10-
| { readonly kind: "pick" };
7+
type DraftProjectSelectionResolution = { readonly kind: "preserve" } | { readonly kind: "pick" };
118

129
export function getProjectScopeSelectionTarget(
1310
scope: HomeProjectScope,
@@ -36,13 +33,6 @@ export function filterProjectScopes(
3633
);
3734
}
3835

39-
function getOnlySelectableProject(
40-
projectScopes: ReadonlyArray<HomeProjectScope>,
41-
): EnvironmentProject | null {
42-
const onlyScope = projectScopes.length === 1 ? projectScopes[0] : null;
43-
return onlyScope?.representative ?? null;
44-
}
45-
4636
export function resolveEnvironmentProjectMatch(
4737
projectsOnTarget: ReadonlyArray<EnvironmentProject>,
4838
selectedProject: EnvironmentProject | null,
@@ -88,7 +78,6 @@ export function resolveEnvironmentProjectMatch(
8878
export function resolveDraftProjectSelection(
8979
selectedProjectKey: string | null,
9080
projects: ReadonlyArray<EnvironmentProject>,
91-
projectScopes: ReadonlyArray<HomeProjectScope>,
9281
): DraftProjectSelectionResolution {
9382
const hasExplicitProjectSelection =
9483
selectedProjectKey !== null &&
@@ -99,21 +88,28 @@ export function resolveDraftProjectSelection(
9988
return { kind: "preserve" };
10089
}
10190

102-
const onlyProject = getOnlySelectableProject(projectScopes);
103-
return onlyProject ? { kind: "select", project: onlyProject } : { kind: "pick" };
91+
return { kind: "pick" };
10492
}
10593

10694
export function newTaskDestination(
107-
_usesSplitView: boolean,
95+
useLastProject: boolean | undefined,
10896
projects: ReadonlyArray<EnvironmentProject>,
10997
currentProject: Pick<EnvironmentProject, "environmentId" | "id"> | undefined,
11098
requestId: string,
99+
lastProject?: { readonly environmentId: string; readonly projectId: string },
111100
) {
112-
const project = projects.find(
113-
(candidate) =>
114-
candidate.environmentId === currentProject?.environmentId &&
115-
candidate.id === currentProject.id,
116-
);
101+
const project = useLastProject
102+
? (projects.find(
103+
(candidate) =>
104+
candidate.environmentId === currentProject?.environmentId &&
105+
candidate.id === currentProject.id,
106+
) ??
107+
projects.find(
108+
(candidate) =>
109+
candidate.environmentId === lastProject?.environmentId &&
110+
candidate.id === lastProject.projectId,
111+
))
112+
: undefined;
117113
return project
118114
? {
119115
draft: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useAtomSet } from "@effect/atom-react";
2+
import { useFocusEffect } from "@react-navigation/native";
3+
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
4+
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
5+
import { AsyncResult } from "effect/reactivity";
6+
import { useCallback } from "react";
7+
8+
import { appAtomRegistry } from "../../state/atom-registry";
9+
import { useEnvironmentServerConfig } from "../../state/entities";
10+
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
11+
12+
export function useRememberThreadProject(project: EnvironmentProject | null) {
13+
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
14+
const serverConfig = useEnvironmentServerConfig(project?.environmentId ?? null);
15+
const environmentId = project?.environmentId;
16+
const projectId = project?.id;
17+
const remember =
18+
project !== null &&
19+
serverConfig !== null &&
20+
!isScratchProject(project, serverConfig.scratchWorkspaceRoot);
21+
22+
useFocusEffect(
23+
useCallback(() => {
24+
if (!remember || !environmentId || !projectId) return;
25+
const preferences = appAtomRegistry.get(mobilePreferencesAtom);
26+
if (!AsyncResult.isSuccess(preferences)) return;
27+
const last = preferences.value.lastThreadProject;
28+
if (last?.environmentId === environmentId && last.projectId === projectId) return;
29+
savePreferences({ lastThreadProject: { environmentId, projectId } });
30+
}, [environmentId, projectId, remember, savePreferences]),
31+
);
32+
}

0 commit comments

Comments
 (0)