Skip to content

Commit 2381e0f

Browse files
fix(mobile): defer draft navigation until submission completes
1 parent 9b2d043 commit 2381e0f

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
22
import {
3+
CommonActions,
34
StackActions,
45
useFocusEffect,
56
useNavigation,
67
usePreventRemove,
8+
type NavigationAction,
79
} from "@react-navigation/native";
810
import { useCallback, useEffect, useRef, useState } from "react";
911
import { Alert, Platform, Pressable, ScrollView, View } from "react-native";
@@ -212,6 +214,9 @@ export function NewTaskDraftScreen(props: {
212214
const [isCancellingShareImport, setIsCancellingShareImport] = useState(false);
213215
const [cancelledIncomingShareId, setCancelledIncomingShareId] = useState<string | null>(null);
214216
const [isReturningToProjectPicker, setIsReturningToProjectPicker] = useState(false);
217+
const [submitNavigationAction, setSubmitNavigationAction] = useState<NavigationAction | null>(
218+
null,
219+
);
215220
const [shareImportAttempt, setShareImportAttempt] = useState(0);
216221
const startedShareImportKeyRef = useRef<string | null>(null);
217222
const cancellingShareImportKeyRef = useRef<string | null>(null);
@@ -275,12 +280,20 @@ export function NewTaskDraftScreen(props: {
275280
voiceInput.elapsedSeconds,
276281
);
277282
const isVoiceInputPresented = voicePresentation.statusLabel !== null;
278-
usePreventRemove(
283+
const preventRemove =
279284
(isIncomingShareTransferPending && !isProjectPickerReturnActive) ||
280-
isCancellingShareImport ||
281-
flow.submitting,
282-
() => undefined,
283-
);
285+
isCancellingShareImport ||
286+
flow.submitting;
287+
usePreventRemove(preventRemove, () => undefined);
288+
useEffect(() => {
289+
if (preventRemove || submitNavigationAction === null) {
290+
return;
291+
}
292+
// setSubmitting(false) does not release the guard synchronously. Navigate
293+
// after usePreventRemove has committed, for both sent and queued tasks.
294+
setSubmitNavigationAction(null);
295+
(navigation.getParent() ?? navigation).dispatch(submitNavigationAction);
296+
}, [navigation, preventRemove, submitNavigationAction]);
284297
const hasImportedIncomingShare = Boolean(
285298
props.incomingShareId &&
286299
flow.draftKey &&
@@ -872,7 +885,7 @@ export function NewTaskDraftScreen(props: {
872885
clearWorkspaceSelection: true,
873886
});
874887
}
875-
navigation.getParent()?.goBack();
888+
setSubmitNavigationAction(CommonActions.goBack());
876889
return;
877890
}
878891

@@ -943,7 +956,7 @@ export function NewTaskDraftScreen(props: {
943956
clearWorkspaceSelection: true,
944957
});
945958
}
946-
navigation.dispatch(
959+
setSubmitNavigationAction(
947960
StackActions.replace("Thread", {
948961
environmentId: String(result.value.environmentId),
949962
threadId: String(result.value.threadId),

0 commit comments

Comments
 (0)