GeezerVison is showing github.com — feat(mobile): add a "Use last project" preference for new threads (#2… · pingdotgg/t3code@c976a09
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +20-17Lines changed: 20 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -523,24 +523,27 @@ function AdaptiveWorkspaceLayoutContent( | 523 | 523 | : undefined; | 524 | 524 | const first = (value: string | string[] | undefined) => | 525 | 525 | Array.isArray(value) ? value[0] : value; | 526 | | - const thread = appAtomRegistry | 527 | | - .get(environmentThreadShells.navigationThreadShellsAtom) | 528 | | - .find( | 529 | | - (candidate) => | 530 | | - candidate.environmentId === first(params?.environmentId) && | 531 | | - candidate.id === first(params?.threadId), | 532 | | - ); | 533 | | - const draft = params?.draft; | 534 | | - const draftEnvironmentId = first(draft?.environmentId); | 535 | | - const draftProjectId = first(draft?.projectId); | 536 | | - const currentProject = thread | 537 | | - ? { environmentId: thread.environmentId, id: thread.projectId } | 538 | | - : projects.find( | 539 | | - (project) => | 540 | | - project.environmentId === draftEnvironmentId && project.id === draftProjectId, | 541 | | - ); | | 526 | + const environmentId = first(params?.environmentId); | | 527 | + const threadId = first(params?.threadId); | | 528 | + // The full index, not the sidebar list: subagent and archived threads are current threads too. | | 529 | + const projectId = | | 530 | + environmentId && threadId | | 531 | + ? appAtomRegistry | | 532 | + .get( | | 533 | + environmentThreadShells.environmentThreadIndexAtom(EnvironmentId.make(environmentId)), | | 534 | + ) | | 535 | + .get(ThreadId.make(threadId))?.projectId | | 536 | + : undefined; | | 537 | + const preferences = appAtomRegistry.get(mobilePreferencesAtom); | | 538 | + const saved = AsyncResult.isSuccess(preferences) ? preferences.value : undefined; | 542 | 539 | handleSelectThreadRoute( | 543 | | - newTaskDestination(layout.usesSplitView, projects, currentProject, uuidv4()), | | 540 | + newTaskDestination({ | | 541 | + projects, | | 542 | + requestId: uuidv4(), | | 543 | + useLastProject: saved?.useLastProjectForNewThreads, | | 544 | + currentProject: environmentId && projectId ? { environmentId, projectId } : undefined, | | 545 | + lastProject: saved?.lastThreadProject, | | 546 | + }), | 544 | 547 | ); | 545 | 548 | }; | 546 | 549 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/settings/SettingsProjectGroupingRouteScreen.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ import { | 12 | 12 | import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; | 13 | 13 | import { SettingsChoiceRow } from "./components/SettingsChoiceRow"; | 14 | 14 | import { SettingsSection } from "./components/SettingsSection"; | 15 | | -import { WorkingSectionSettings } from "./ThreadSettingsSections"; | | 15 | +import { NewThreadSettingsSection, WorkingSectionSettings } from "./ThreadSettingsSections"; | 16 | 16 |
| 17 | 17 | const GROUPING_OPTIONS: ReadonlyArray<{ | 18 | 18 | readonly mode: SidebarProjectGroupingMode; | @@ -68,6 +68,7 @@ export function SettingsProjectGroupingRouteScreen() { | 68 | 68 | ))} | 69 | 69 | </SettingsSection> | 70 | 70 | <WorkingSectionSettings /> | | 71 | + <NewThreadSettingsSection /> | 71 | 72 | </ScrollView> | 72 | 73 | </SettingsScreen> | 73 | 74 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/settings/ThreadSettingsSections.tsx +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -195,6 +195,25 @@ export function WorkingSectionSettings() { | 195 | 195 | ); | 196 | 196 | } | 197 | 197 |
| | 198 | +export function NewThreadSettingsSection() { | | 199 | + const savePreferences = useAtomSet(updateMobilePreferencesAtom); | | 200 | + const preferences = useAtomValue(mobilePreferencesAtom); | | 201 | + const useLastProject = | | 202 | + AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false; | | 203 | + | | 204 | + return ( | | 205 | + <SettingsSection title="New threads"> | | 206 | + <SettingsSwitchRow | | 207 | + icon="square.and.pencil" | | 208 | + label="Use last project" | | 209 | + subtitle="Start new threads in the last project you used on this device." | | 210 | + value={useLastProject} | | 211 | + onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })} | | 212 | + /> | | 213 | + </SettingsSection> | | 214 | + ); | | 215 | +} | | 216 | + | 198 | 217 | function LegacySettingsSection() { | 199 | 218 | const savePreferences = useAtomSet(updateMobilePreferencesAtom); | 200 | 219 | const preferences = useAtomValue(mobilePreferencesAtom); | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/settings/settings-search.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,8 @@ const SETTINGS_SEARCH_TERMS = { | 38 | 38 | "organization", | 39 | 39 | "sidebar", | 40 | 40 | "working section", | | 41 | + "new threads", | | 42 | + "last project", | 41 | 43 | ], | 42 | 44 | SettingsThreads: [ | 43 | 45 | "new threads", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +29-3Lines changed: 29 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,8 @@ import { useWorkspaceBackNavigation } from "../workspace/useAndroidWorkspaceNavi | 8 | 8 | import { submittedThreadParams } from "./thread-route-state"; | 9 | 9 | import { ThreadScreenBinding, ThreadScreenFallback } from "./ThreadScreenHost"; | 10 | 10 | import { draftThreadShell } from "./draft-thread-shell"; | | 11 | +import { useRememberThreadProject } from "./useRememberThreadProject"; | | 12 | +import { scopedProjectKey } from "../../lib/scopedEntities"; | 11 | 13 | import { connectionTone } from "../connection/connectionTone"; | 12 | 14 | import { AppBar } from "../../components/ui/AppBar"; | 13 | 15 | import { ThreadId } from "@t3tools/contracts"; | @@ -20,6 +22,7 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime | 20 | 22 | import { | 21 | 23 | CommonActions, | 22 | 24 | StackActions, | | 25 | + useIsFocused, | 23 | 26 | useNavigation, | 24 | 27 | usePreventRemove, | 25 | 28 | } from "@react-navigation/native"; | @@ -156,6 +159,7 @@ export function NewTaskDraftScreen(props: { | 156 | 159 | const projects = useProjects(); | 157 | 160 | const flow = useNewTaskFlow(); | 158 | 161 | const navigation = useNavigation(); | | 162 | + const isFocused = useIsFocused(); | 159 | 163 | const goBack = useWorkspaceBackNavigation("threads"); | 160 | 164 | const { | 161 | 165 | consumeShare, | @@ -165,6 +169,13 @@ export function NewTaskDraftScreen(props: { | 165 | 169 | reserveShare, | 166 | 170 | } = useIncomingShare(); | 167 | 171 | const { projectScopes, selectedProject, selectedProjectKey, setProject } = flow; | | 172 | + // selectedProject falls back to the environment's first project; remember only a chosen one. | | 173 | + useRememberThreadProject( | | 174 | + selectedProject && | | 175 | + selectedProjectKey === scopedProjectKey(selectedProject.environmentId, selectedProject.id) | | 176 | + ? selectedProject | | 177 | + : null, | | 178 | + ); | 168 | 179 | const selectedProjectLabel = (() => { | 169 | 180 | if (!selectedProject) return null; | 170 | 181 | const scope = projectScopes.find((candidate) => | @@ -416,6 +427,7 @@ export function NewTaskDraftScreen(props: { | 416 | 427 | }, [navigation, openDraft, projects, props.draftId]); | 417 | 428 |
| 418 | 429 | const lastInitialProjectRefRef = useRef(props.initialProjectRef); | | 430 | + const initialPickerRef = useRef<"open" | "dismissed" | null>(null); | 419 | 431 |
| 420 | 432 | useEffect(() => { | 421 | 433 | if (props.draftId) { | @@ -477,22 +489,36 @@ export function NewTaskDraftScreen(props: { | 477 | 489 | return; | 478 | 490 | } | 479 | 491 |
| 480 | | - const selection = resolveDraftProjectSelection(selectedProjectKey, projects, projectScopes); | | 492 | + const selection = resolveDraftProjectSelection(selectedProjectKey, projects, projectScopes, { | | 493 | + incomingShareId: props.incomingShareId, | | 494 | + pickerClosed: initialPickerRef.current === "open" && isFocused, | | 495 | + }); | 481 | 496 | if (selection.kind === "preserve") { | | 497 | + initialPickerRef.current = null; | 482 | 498 | return; | 483 | 499 | } | 484 | 500 | if (selection.kind === "select") { | 485 | 501 | setProject(selection.project); | 486 | 502 | return; | 487 | 503 | } | 488 | | - | | 504 | + if (selection.kind === "dismiss") { | | 505 | + initialPickerRef.current = "dismissed"; | | 506 | + goBack(); | | 507 | + return; | | 508 | + } | | 509 | + // Open the first picker once; catalog updates must not push it again over a cancelled draft. | | 510 | + if (initialPickerRef.current !== null) { | | 511 | + return; | | 512 | + } | | 513 | + initialPickerRef.current = "open"; | 489 | 514 | navigation.dispatch(CommonActions.navigate("NewTaskProject")); | 490 | 515 | }, [ | | 516 | + goBack, | | 517 | + isFocused, | 491 | 518 | projectScopes, | 492 | 519 | projects, | 493 | 520 | flow.draftKey, | 494 | 521 | props.initialProjectRef, | 495 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Opening a draft from a share or draft id re-resolves its project. | 496 | 522 | props.incomingShareId, | 497 | 523 | props.draftId, | 498 | 524 | navigation, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/ThreadRouteScreen.tsx +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as Dialog from "../../components/ui/Dialog"; | | 2 | +import { useRememberThreadProject } from "./useRememberThreadProject"; | 2 | 3 | import { useOpenPullRequest } from "../../native/useOpenPullRequest"; | 3 | 4 | import { makeTurnCommandMetadata } from "../../lib/commandMetadata"; | 4 | 5 | import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTurn"; | @@ -81,7 +82,6 @@ import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread- | 81 | 82 | import { threadEnvironment, useThreadFamily } from "../../state/threads"; | 82 | 83 | import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; | 83 | 84 | import { useProjects } from "../../state/entities"; | 84 | | -import { newTaskDestination } from "./new-task-project-selection"; | 85 | 85 | import { projectThreadContentPresentation } from "./threadContentPresentation"; | 86 | 86 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; | 87 | 87 | import { | @@ -198,14 +198,18 @@ function ThreadUnavailableScreen(props: { | 198 | 198 | function UnavailableThreadDraftRedirect(props: { readonly environmentId: EnvironmentId }) { | 199 | 199 | const navigation = useNavigation(); | 200 | 200 | const projects = useProjects(); | 201 | | - const { layout } = useAdaptiveWorkspaceLayout(); | 202 | 201 | const openDraft = useEffectEvent(() => { | 203 | 202 | const project = projects.find((candidate) => candidate.environmentId === props.environmentId); | 204 | 203 | navigation.dispatch( | 205 | | - StackActions.replace( | 206 | | - "Thread", | 207 | | - newTaskDestination(layout.usesSplitView, projects, project, uuidv4()), | 208 | | - ), | | 204 | + StackActions.replace("Thread", { | | 205 | + draft: project | | 206 | + ? { | | 207 | + requestId: uuidv4(), | | 208 | + environmentId: String(project.environmentId), | | 209 | + projectId: String(project.id), | | 210 | + } | | 211 | + : { requestId: uuidv4() }, | | 212 | + }), | 209 | 213 | ); | 210 | 214 | }); | 211 | 215 | // oxlint-disable-next-line t3code/react-effect-budget -- replaces this route in the navigator | @@ -339,6 +343,7 @@ function ThreadRouteContent( | 339 | 343 | selectedThreadProject, | 340 | 344 | selectedEnvironmentConnection, | 341 | 345 | } = useThreadSelection(); | | 346 | + useRememberThreadProject(selectedThreadProject); | 342 | 347 | const canOperateThread = useEnvironmentScope( | 343 | 348 | selectedThread?.environmentId ?? null, | 344 | 349 | AuthOrchestrationOperateScope, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/new-task-project-selection.test.ts +117-26Lines changed: 117 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,46 +95,137 @@ describe("resolveEnvironmentProjectMatch", () => { | 95 | 95 | }); | 96 | 96 |
| 97 | 97 | 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], | | 98 | + const projects = [makeProject("t3code"), makeProject("t3code-2")]; | | 99 | + const draft = { incomingShareId: undefined, pickerClosed: false }; | | 100 | + | | 101 | + it("opens the picker instead of auto-selecting the only project", () => { | | 102 | + expect(resolveDraftProjectSelection(null, projects, [makeScope(projects)], draft)).toEqual({ | | 103 | + kind: "pick", | 103 | 104 | }); | 104 | 105 | }); | 105 | 106 |
| 106 | | - it("does not preserve a project key that is missing from the catalog", () => { | 107 | | - const project = makeProject("t3code"); | | 107 | + it("selects the only logical project for shared content", () => { | 108 | 108 | expect( | 109 | | - resolveDraftProjectSelection("environment:removed", [project], [makeScope([project])]), | 110 | | - ).toEqual({ | 111 | | - kind: "select", | 112 | | - project, | 113 | | - }); | | 109 | + resolveDraftProjectSelection(null, projects, [makeScope(projects)], { | | 110 | + ...draft, | | 111 | + incomingShareId: "share", | | 112 | + }), | | 113 | + ).toEqual({ kind: "select", project: projects[0] }); | | 114 | + }); | | 115 | + | | 116 | + it("dismisses the draft when the first picker closes without a choice", () => { | | 117 | + expect( | | 118 | + resolveDraftProjectSelection(null, projects, [makeScope(projects)], { | | 119 | + ...draft, | | 120 | + pickerClosed: true, | | 121 | + }), | | 122 | + ).toEqual({ kind: "dismiss" }); | | 123 | + }); | | 124 | + | | 125 | + it("keeps the draft when the picker closes after a choice", () => { | | 126 | + expect( | | 127 | + resolveDraftProjectSelection("environment:t3code-2", projects, [makeScope(projects)], { | | 128 | + ...draft, | | 129 | + pickerClosed: true, | | 130 | + }), | | 131 | + ).toEqual({ kind: "preserve" }); | | 132 | + }); | | 133 | + | | 134 | + it("keeps a share draft asking for a project when its picker closes without a choice", () => { | | 135 | + const scopes = [makeScope([projects[0]!]), makeScope([projects[1]!])]; | | 136 | + expect( | | 137 | + resolveDraftProjectSelection(null, projects, scopes, { | | 138 | + incomingShareId: "share", | | 139 | + pickerClosed: true, | | 140 | + }), | | 141 | + ).toEqual({ kind: "pick" }); | 114 | 142 | }); | 115 | 143 | }); | 116 | 144 |
| 117 | 145 | describe("newTaskDestination", () => { | 118 | | - it("opens the current thread's project directly in split view", () => { | 119 | | - const projects = [makeProject("first"), makeProject("current", "other-environment")]; | 120 | | - expect(newTaskDestination(true, projects, projects[1], "request")).toEqual({ | | 146 | + const picker = { draft: { requestId: "request" } }; | | 147 | + | | 148 | + function destination(options: { | | 149 | + readonly useLastProject: boolean | undefined; | | 150 | + readonly projects: ReadonlyArray<EnvironmentProject>; | | 151 | + readonly current?: EnvironmentProject; | | 152 | + readonly last?: { readonly environmentId: string; readonly projectId: string }; | | 153 | + }) { | | 154 | + return newTaskDestination({ | | 155 | + projects: options.projects, | | 156 | + requestId: "request", | | 157 | + useLastProject: options.useLastProject, | | 158 | + currentProject: options.current && { | | 159 | + environmentId: options.current.environmentId, | | 160 | + projectId: options.current.id, | | 161 | + }, | | 162 | + lastProject: options.last, | | 163 | + }); | | 164 | + } | | 165 | + | | 166 | + it("starts in the current thread's project ahead of the remembered one", () => { | | 167 | + const projects = [makeProject("remembered"), makeProject("current", "other-environment")]; | | 168 | + expect( | | 169 | + destination({ | | 170 | + useLastProject: true, | | 171 | + projects, | | 172 | + current: projects[1], | | 173 | + last: { environmentId: "environment", projectId: "remembered" }, | | 174 | + }), | | 175 | + ).toEqual({ | 121 | 176 | draft: { requestId: "request", environmentId: "other-environment", projectId: "current" }, | 122 | 177 | }); | 123 | 178 | }); | 124 | | - it("uses an available project when split view has no current project", () => { | 125 | | - expect(newTaskDestination(true, [makeProject("first")], undefined, "request")).toEqual({ | 126 | | - draft: { requestId: "request", environmentId: "environment", projectId: "first" }, | | 179 | + | | 180 | + it("treats an unset preference as on", () => { | | 181 | + const projects = [makeProject("first"), makeProject("current")]; | | 182 | + expect(destination({ useLastProject: undefined, projects, current: projects[1] })).toEqual({ | | 183 | + draft: { requestId: "request", environmentId: "environment", projectId: "current" }, | 127 | 184 | }); | 128 | 185 | }); | 129 | | - it("keeps the picker when there are no projects", () => { | 130 | | - expect(newTaskDestination(true, [], undefined, "request")).toEqual({ | 131 | | - draft: { requestId: "request" }, | | 186 | + | | 187 | + it("starts in the remembered project of its own environment when there is no current one", () => { | | 188 | + const projects = [makeProject("chosen"), makeProject("chosen", "other-environment")]; | | 189 | + expect( | | 190 | + destination({ | | 191 | + useLastProject: true, | | 192 | + projects, | | 193 | + last: { environmentId: "other-environment", projectId: "chosen" }, | | 194 | + }), | | 195 | + ).toEqual({ | | 196 | + draft: { requestId: "request", environmentId: "other-environment", projectId: "chosen" }, | 132 | 197 | }); | 133 | 198 | }); | 134 | | - it("opens the same draft route in compact view", () => { | 135 | | - const project = makeProject("current"); | 136 | | - expect(newTaskDestination(false, [project], project, "request")).toEqual({ | 137 | | - draft: { requestId: "request", environmentId: "environment", projectId: "current" }, | 138 | | - }); | | 199 | + | | 200 | + it("ignores a remembered project id from another environment", () => { | | 201 | + expect( | | 202 | + destination({ | | 203 | + useLastProject: true, | | 204 | + projects: [makeProject("chosen")], | | 205 | + last: { environmentId: "removed-environment", projectId: "chosen" }, | | 206 | + }), | | 207 | + ).toEqual(picker); | | 208 | + }); | | 209 | + | | 210 | + it("opens the picker rather than the first project when nothing matches", () => { | | 211 | + expect( | | 212 | + destination({ | | 213 | + useLastProject: true, | | 214 | + projects: [makeProject("first"), makeProject("second")], | | 215 | + current: makeProject("removed"), | | 216 | + }), | | 217 | + ).toEqual(picker); | | 218 | + }); | | 219 | + | | 220 | + it("always opens the picker when the preference is off", () => { | | 221 | + const only = makeProject("only"); | | 222 | + expect( | | 223 | + destination({ | | 224 | + useLastProject: false, | | 225 | + projects: [only], | | 226 | + current: only, | | 227 | + last: { environmentId: "environment", projectId: "only" }, | | 228 | + }), | | 229 | + ).toEqual(picker); | 139 | 230 | }); | 140 | 231 | }); |
|