11import { useNavigation , usePreventRemove , type StaticScreenProps } from "@react-navigation/native" ;
2- import { useEffect , useMemo , useState } from "react" ;
2+ import { useEffect , useMemo , useRef , useState } from "react" ;
33import { Alert , View } from "react-native" ;
44import {
55 isAtomCommandInterrupted ,
88import { AppText as Text } from "../../components/AppText" ;
99import { useProjects } from "../../state/entities" ;
1010import { useAtomCommand } from "../../state/use-atom-command" ;
11+ import { useWorkspaceState } from "../../state/workspace" ;
1112import { vcsEnvironment } from "../../state/vcs" ;
1213import { checkoutNewTaskBranch } from "./checkout-new-task-branch" ;
1314import { NativeStackScreenOptions } from "../../native/StackHeader" ;
@@ -32,6 +33,7 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
3233 : params . pendingTaskId ;
3334 const draftId = Array . isArray ( params . draftId ) ? params . draftId [ 0 ] : params . draftId ;
3435 const projects = useProjects ( ) ;
36+ const { state : catalogState } = useWorkspaceState ( ) ;
3537 const navigation = useNavigation ( ) ;
3638 const switchRef = useAtomCommand ( vcsEnvironment . switchRef , { reportFailure : false } ) ;
3739
@@ -65,31 +67,52 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
6567 const needsPreparation = Boolean ( initialProjectRef . branch && ! pendingTaskId && ! draftId ) ;
6668
6769 const [ pendingCheckouts , setPendingCheckouts ] = useState ( 0 ) ;
70+ const checkoutTail = useRef ( Promise . resolve ( ) ) ;
71+ const waitingForProject =
72+ ! project &&
73+ ( catalogState . isLoadingConnections ||
74+ ( ! catalogState . hasLoadedShellSnapshot &&
75+ catalogState . hasConnectingEnvironment &&
76+ catalogState . connectionError === null ) ) ;
6877
6978 useEffect ( ( ) => {
70- if ( ! needsPreparation || ! initialProjectRef . branch ) return ;
79+ if ( ! needsPreparation || ! initialProjectRef . branch || waitingForProject ) return ;
80+ const branchName = initialProjectRef . branch ;
7181 let active = true ;
7282 setPendingCheckouts ( ( count ) => count + 1 ) ;
73- void checkoutNewTaskBranch ( {
74- // A thread's branch is historical; only switchRef can establish that
75- // the shared project checkout now matches it.
76- branch : {
77- name : initialProjectRef . branch ,
78- current : false ,
79- isDefault : false ,
80- worktreePath : initialProjectRef . worktreePath ?? null ,
81- } ,
82- project : environmentId && workspaceRoot ? { environmentId, workspaceRoot } : null ,
83- workspaceMode : "local" ,
84- switchRef,
85- } ) . then ( ( result ) => {
83+ // Serialize replacements: ignoring a stale result cannot undo its Git mutation.
84+ checkoutTail . current = checkoutTail . current . then ( async ( ) => {
85+ if ( ! active ) {
86+ setPendingCheckouts ( ( count ) => count - 1 ) ;
87+ return ;
88+ }
89+ const result = await checkoutNewTaskBranch ( {
90+ // A thread's branch is historical; only switchRef can establish that
91+ // the shared project checkout now matches it.
92+ branch : {
93+ name : branchName ,
94+ current : false ,
95+ isDefault : false ,
96+ worktreePath : initialProjectRef . worktreePath ?? null ,
97+ } ,
98+ project : environmentId && workspaceRoot ? { environmentId, workspaceRoot } : null ,
99+ workspaceMode : "local" ,
100+ switchRef,
101+ } ) ;
86102 setPendingCheckouts ( ( count ) => count - 1 ) ;
87103 if ( active ) setPreparation ( { request : initialProjectRef , result, workspaceRoot } ) ;
88104 } ) ;
89105 return ( ) => {
90106 active = false ;
91107 } ;
92- } , [ environmentId , workspaceRoot , initialProjectRef , needsPreparation , switchRef ] ) ;
108+ } , [
109+ environmentId ,
110+ workspaceRoot ,
111+ initialProjectRef ,
112+ needsPreparation ,
113+ switchRef ,
114+ waitingForProject ,
115+ ] ) ;
93116
94117 const result =
95118 preparation ?. request === initialProjectRef && preparation . workspaceRoot === workspaceRoot
0 commit comments