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 22, 17:00 UTC
refactor(mobile): branch picker owns its search and checkout guard (#25067)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 17:23 UTC
4db2ffc8d9333f6a43d944c718c554a699877ef1
2 files changed
+45
-74
apps/mobile/src/features/threads/NewTaskContextPickerScreens.tsx
+41
-38
@@ -20,7 +20,8 @@ import {
20
} from "@t3tools/client-runtime/state/runtime";
21
import * as Haptics from "expo-haptics";
22
import { useNavigation } from "@react-navigation/native";
23
-import { useEffect, useRef, useState, type ReactNode } from "react";
23
+import { useRef, useState, type ReactNode } from "react";
24
+import { sanitizeNewRefName } from "@t3tools/shared/git";
25
import { Platform, Pressable, ScrollView, View } from "react-native";
26
import { useSafeAreaInsets } from "react-native-safe-area-context";
27
@@ -42,6 +43,9 @@ import {
43
import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
44
import { branchBadgeLabel, useNewTaskFlow } from "./new-task-flow-provider";
45
import { checkoutNewTaskBranch } from "./checkout-new-task-branch";
46
+import { filterNewTaskBranches } from "./new-task-context-presentation";
47
+import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";
48
+import { setPendingConnectionError } from "../../state/use-remote-environment-registry";
49
50
import { useEnvironmentPresentation } from "../../state/presentation";
51
import { useRemoteConnections } from "../../state/use-remote-environment-registry";
@@ -52,6 +56,8 @@ import {
56
} from "../connection/ConnectionDiagnosisDetails";
57
import { environmentStatusText } from "../connection/environmentStatusText";
58
59
+const BRANCH_SEARCH_DEBOUNCE_MS = 150;
60
+
61
function EnvironmentPickerRow(props: {
62
environmentId: EnvironmentId;
63
title: string;
@@ -240,26 +246,22 @@ export function NewTaskBranchPickerRouteScreen() {
246
const switchRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
247
const [switchingBranchName, setSwitchingBranchName] = useState<string | null>(null);
248
const selectingBranchNameRef = useRef<string | null>(null);
243
- const allowSelectionNavigationRef = useRef(false);
244
- const mountedRef = useRef(true);
245
-
246
- useEffect(() => {
247
- mountedRef.current = true;
248
- return () => {
249
- mountedRef.current = false;
250
- flow.setBranchQuery("");
251
- };
252
- }, [flow.setBranchQuery]);
253
-
254
- useEffect(
255
- () =>
256
- navigation.addListener("beforeRemove", (event) => {
257
- if (selectingBranchNameRef.current !== null && !allowSelectionNavigationRef.current) {
258
- event.preventDefault();
259
- }
260
- }),
261
- [navigation],
262
- );
249
+ const [branchQuery, setBranchQuery] = useState("");
250
+ const branchSearchQuery = sanitizeNewRefName(branchQuery);
251
+ const debouncedBranchQuery = useDebouncedValue(branchSearchQuery, BRANCH_SEARCH_DEBOUNCE_MS);
252
+ const branchState = usePaginatedBranches({
253
+ environmentId: flow.selectedProject?.environmentId ?? null,
254
+ cwd: flow.selectedProject?.workspaceRoot || null,
255
+ query: debouncedBranchQuery,
256
+ });
257
+ const branchesLoading =
258
+ branchSearchQuery !== debouncedBranchQuery ||
259
+ (branchState.isPending && branchState.data === null);
260
+ const refreshBranches = () => {
261
+ if (!flow.selectedProject) return;
262
+ setPendingConnectionError(null);
263
+ branchState.refresh();
264
+ };
265
266
const selectBranch = async (branch: VcsRef) => {
267
const needsCheckout = shouldCheckoutNewTaskBranch({
@@ -279,17 +281,22 @@ export function NewTaskBranchPickerRouteScreen() {
281
return;
282
}
283
setSwitchingBranchName(branch.name);
284
+ // The guard belongs to this checkout: back actions are refused while it is
285
+ // in flight, and our own goBack runs after it is lifted.
286
+ const releaseRemovalGuard = navigation.addListener("beforeRemove", (event) => {
287
+ event.preventDefault();
288
+ });
289
const result = await checkoutNewTaskBranch({
290
branch,
291
project,
292
workspaceMode: flow.workspaceMode,
293
readRefs,
294
switchRef,
288
- });
289
- if (mountedRef.current) setSwitchingBranchName(null);
295
+ }).finally(releaseRemovalGuard);
296
+ setSwitchingBranchName(null);
297
if (result._tag === "Failure") {
298
selectingBranchNameRef.current = null;
292
- if (mountedRef.current && navigation.isFocused() && !isAtomCommandInterrupted(result)) {
299
+ if (navigation.isFocused() && !isAtomCommandInterrupted(result)) {
300
const error = squashAtomCommandFailure(result);
301
Dialog.alert(
302
"Could not switch branch",
@@ -301,15 +308,11 @@ export function NewTaskBranchPickerRouteScreen() {
308
309
// Checkout already changed Git; persist the draft selection even if the sheet dismissed mid-command.
310
flow.selectBranch(result.value);
304
- if (!mountedRef.current || !navigation.isFocused()) {
311
+ if (!navigation.isFocused()) {
312
selectingBranchNameRef.current = null;
313
return;
314
}
308
- flow.setBranchQuery("");
309
- // beforeRemove blocks dismissal while a switch is in flight; this one is ours.
310
- allowSelectionNavigationRef.current = true;
315
navigation.goBack();
312
- allowSelectionNavigationRef.current = false;
316
selectingBranchNameRef.current = null;
317
};
318
@@ -317,21 +320,21 @@ export function NewTaskBranchPickerRouteScreen() {
320
<BranchPickerScreen
321
title={flow.workspaceMode === "worktree" ? "Base branch" : "Branch"}
322
project={flow.selectedProject}
320
- branches={flow.filteredBranches}
323
+ branches={filterNewTaskBranches(branchState.refs, branchQuery)}
324
selectedBranchName={
325
flow.selectedBranchName ??
326
flow.availableBranches.find((branch) => branch.current)?.name ??
327
flow.availableBranches.find((branch) => branch.isDefault)?.name ??
328
null
329
}
327
- query={flow.branchQuery}
328
- onQueryChange={flow.setBranchQuery}
329
- loading={flow.branchesLoading}
330
- error={flow.branchesError}
331
- refreshing={flow.branchesFetchingNextPage}
332
- hasMore={flow.hasMoreBranches}
333
- onRefresh={flow.loadBranches}
334
- onLoadMore={flow.loadMoreBranches}
330
+ query={branchQuery}
331
+ onQueryChange={setBranchQuery}
332
+ loading={branchesLoading}
333
+ error={branchState.error}
334
+ refreshing={branchState.isFetchingNextPage}
335
+ hasMore={branchState.data?.nextCursor !== null && branchState.data?.nextCursor !== undefined}
336
+ onRefresh={refreshBranches}
337
+ onLoadMore={branchState.loadNext}
338
selectionDisabled={switchingBranchName !== null}
339
onSelect={selectBranch}
340
worktree={
apps/mobile/src/features/threads/new-task-flow-provider.tsx
+4
-36
@@ -24,7 +24,6 @@ import {
24
T3_PROJECT_FILE_NAME,
25
ThreadId,
26
} from "@t3tools/contracts";
27
-import { sanitizeNewRefName } from "@t3tools/shared/git";
27
import { threadContextsOutsideEnvironment } from "@t3tools/shared/composerContextReferences";
28
import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings";
29
import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
@@ -80,7 +79,7 @@ import {
79
rememberModelOptions,
80
withRememberedModelOptions,
81
} from "../../state/use-model-option-memory";
83
-import { useBranches, useDebouncedValue, usePaginatedBranches } from "../../state/queries";
82
+import { useBranches, usePaginatedBranches } from "../../state/queries";
83
import { vcsEnvironment } from "../../state/vcs";
84
import {
85
flattenQueuedThreadMessages,
@@ -117,7 +116,6 @@ import {
116
} from "../../state/legacy-plan-mode";
117
import { useLegacyPlanModeState } from "./use-legacy-plan-mode-enabled";
118
import {
120
- filterNewTaskBranches,
119
resolveNewTaskBranchWorktreePath,
120
resolveNewTaskLocalWorkspaceSelection,
121
resolveNewTaskWorkspaceSelection,
@@ -128,8 +126,6 @@ import { useOlderSettledCount } from "../../state/olderSettledShelf";
126
127
type WorkspaceMode = "local" | "worktree";
128
131
-const BRANCH_SEARCH_DEBOUNCE_MS = 150;
132
-
129
function pendingTaskDraftKey(messageId: string): string {
130
return `pending-task:${messageId}`;
131
}
@@ -182,11 +178,7 @@ type NewTaskFlowContextValue = {
178
readonly prompt: string;
179
readonly attachments: ReadonlyArray<DraftComposerAttachment>;
180
readonly submitting: boolean;
185
- readonly branchQuery: string;
181
readonly branchesLoading: boolean;
187
- readonly branchesError: string | null;
188
- readonly branchesFetchingNextPage: boolean;
189
- readonly hasMoreBranches: boolean;
182
readonly availableBranches: ReadonlyArray<VcsRef>;
183
readonly currentCheckoutBranchName: string | null;
184
readonly runtimeMode: RuntimeMode;
@@ -204,7 +196,6 @@ type NewTaskFlowContextValue = {
196
readonly selectedModelOption: ModelOption | null;
197
readonly selectedProviderStatus: ServerProvider | null;
198
readonly providerGroups: ReadonlyArray<ProviderGroup>;
207
- readonly filteredBranches: ReadonlyArray<VcsRef>;
199
readonly reset: () => void;
200
readonly setProject: (project: EnvironmentProject) => void;
201
/** Returns false if the existing draft is gone, allowing fallback to a fresh draft. */
@@ -238,9 +229,7 @@ type NewTaskFlowContextValue = {
229
readonly removeAttachment: (imageId: string) => void;
230
readonly clearAttachments: () => void;
231
readonly setSubmitting: (value: boolean) => void;
241
- readonly setBranchQuery: (value: string) => void;
232
readonly loadBranches: () => void;
243
- readonly loadMoreBranches: () => void;
233
readonly setRuntimeMode: (value: RuntimeMode) => void;
234
readonly setInteractionMode: (value: ProviderInteractionMode) => void;
235
readonly setSelectedModelOptions: (
@@ -287,7 +276,6 @@ export function NewTaskFlowProvider(
276
const [selectedProjectKey, setSelectedProjectKey] = useState<string | null>(null);
277
const [activeDraftKey, setActiveDraftKey] = useState<string | null>(null);
278
const [submitting, setSubmitting] = useState(false);
290
- const [branchQuery, setBranchQuery] = useState("");
279
const [expandedProvider, setExpandedProvider] = useState<string | null>(null);
280
const [editingPendingTask, setEditingPendingTask] = useState<QueuedThreadMessage | null>(null);
281
const pendingLocalBranchSyncDraftKeysRef = useRef(new Set<string>());
@@ -300,7 +288,6 @@ export function NewTaskFlowProvider(
288
setSelectedProjectKey(null);
289
setActiveDraftKey(null);
290
setSubmitting(false);
303
- setBranchQuery("");
291
setExpandedProvider(null);
292
pendingLocalBranchSyncDraftKeysRef.current.clear();
293
const editing = editingPendingTaskRef.current;
@@ -684,28 +671,17 @@ export function NewTaskFlowProvider(
671
}
672
replaceComposerDraftAttachments(selectedProjectDraftKey, []);
673
};
687
- const branchSearchQuery = sanitizeNewRefName(branchQuery);
688
- const debouncedBranchQuery = useDebouncedValue(branchSearchQuery, BRANCH_SEARCH_DEBOUNCE_MS);
689
- const branchTarget = {
674
+ const branchState = usePaginatedBranches({
675
environmentId: selectedProject?.environmentId ?? null,
676
cwd: selectedProject?.workspaceRoot || null,
692
- query: debouncedBranchQuery,
693
- };
694
- const branchState = usePaginatedBranches(branchTarget);
695
- const branchSearchIsDebouncing = branchSearchQuery !== debouncedBranchQuery;
696
- const branchesLoading =
697
- branchSearchIsDebouncing || (branchState.isPending && branchState.data === null);
698
- const branchesFetchingNextPage = branchState.isFetchingNextPage;
699
- const hasMoreBranches =
700
- branchState.data?.nextCursor !== null && branchState.data?.nextCursor !== undefined;
677
+ });
678
+ const branchesLoading = branchState.isPending && branchState.data === null;
679
const allBranchRefs = branchState.refs;
680
const availableBranches = pipe(
681
allBranchRefs,
682
Arr.filter((branch) => !branch.isRemote),
683
);
684
707
- const filteredBranches = filterNewTaskBranches(allBranchRefs, branchQuery);
708
-
685
const carryDraftContentTo = (project: EnvironmentProject) => {
686
const target = { environmentId: project.environmentId, projectId: project.id };
687
if (activeDraftKey !== null && isNewTaskDraftKey(activeDraftKey)) {
@@ -884,7 +860,6 @@ export function NewTaskFlowProvider(
860
};
861
862
const refreshBranches = branchState.refresh;
887
- const loadMoreBranches = branchState.loadNext;
863
const loadBranches = () => {
864
if (!selectedProject) {
865
return;
@@ -1163,11 +1138,7 @@ export function NewTaskFlowProvider(
1138
prompt,
1139
attachments,
1140
submitting,
1166
- branchQuery,
1141
branchesLoading,
1168
- branchesError: branchState.error,
1169
- branchesFetchingNextPage,
1170
- hasMoreBranches,
1142
availableBranches,
1143
currentCheckoutBranchName,
1144
runtimeMode,
@@ -1182,7 +1153,6 @@ export function NewTaskFlowProvider(
1153
selectedModelOption,
1154
selectedProviderStatus,
1155
providerGroups,
1185
- filteredBranches,
1156
reset,
1157
setProject,
1158
openDraft,
@@ -1203,9 +1173,7 @@ export function NewTaskFlowProvider(
1173
removeAttachment,
1174
clearAttachments,
1175
setSubmitting,
1206
- setBranchQuery,
1176
loadBranches,
1208
- loadMoreBranches,
1177
setRuntimeMode,
1178
setInteractionMode,
1179
setSelectedModelOptions,