@@ -4,12 +4,13 @@ import React, {memo, useCallback, useEffect, useRef, useState} from 'react';
44import ConfirmModal from '@components/ConfirmModal' ;
55import useBeforeRemove from '@hooks/useBeforeRemove' ;
66import useLocalize from '@hooks/useLocalize' ;
7+ import navigateAfterInteraction from '@libs/Navigation/navigateAfterInteraction' ;
78import navigationRef from '@libs/Navigation/navigationRef' ;
89import type { PlatformStackNavigationProp } from '@libs/Navigation/PlatformStackNavigation/types' ;
910import type { RootNavigatorParamList } from '@libs/Navigation/types' ;
1011import type DiscardChangesConfirmationProps from './types' ;
1112
12- function DiscardChangesConfirmation ( { getHasUnsavedChanges} : DiscardChangesConfirmationProps ) {
13+ function DiscardChangesConfirmation ( { getHasUnsavedChanges, onCancel } : DiscardChangesConfirmationProps ) {
1314 const navigation = useNavigation < PlatformStackNavigationProp < RootNavigatorParamList > > ( ) ;
1415 const { translate} = useLocalize ( ) ;
1516 const [ isVisible , setIsVisible ] = useState ( false ) ;
@@ -25,7 +26,7 @@ function DiscardChangesConfirmation({getHasUnsavedChanges}: DiscardChangesConfir
2526
2627 e . preventDefault ( ) ;
2728 blockedNavigationAction . current = e . data . action ;
28- setIsVisible ( true ) ;
29+ navigateAfterInteraction ( ( ) => setIsVisible ( ( prev ) => ! prev ) ) ;
2930 } ,
3031 [ getHasUnsavedChanges ] ,
3132 ) ,
@@ -39,16 +40,19 @@ function DiscardChangesConfirmation({getHasUnsavedChanges}: DiscardChangesConfir
3940 useEffect ( ( ) => {
4041 // transitionStart is triggered before the previous page is fully loaded so RHP sliding animation
4142 // could be less "glitchy" when going back and forth between the previous and current pages
42- const unsubscribe = navigation . addListener ( 'transitionStart' , ( ) => {
43+ const unsubscribe = navigation . addListener ( 'transitionStart' , ( { data : { closing } } ) => {
4344 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
44- if ( ! getHasUnsavedChanges ( ) || blockedNavigationAction . current || shouldNavigateBack . current ) {
45+ if ( ! getHasUnsavedChanges ( ) ) {
46+ return ;
47+ }
48+ shouldNavigateBack . current = true ;
49+ if ( closing ) {
50+ window . history . go ( 1 ) ;
4551 return ;
4652 }
47-
4853 // Navigation.navigate() rerenders the current page and resets its states
4954 window . history . go ( 1 ) ;
50- setIsVisible ( true ) ;
51- shouldNavigateBack . current = true ;
55+ navigateAfterInteraction ( ( ) => setIsVisible ( ( prev ) => ! prev ) ) ;
5256 } ) ;
5357
5458 return unsubscribe ;
@@ -78,6 +82,10 @@ function DiscardChangesConfirmation({getHasUnsavedChanges}: DiscardChangesConfir
7882 blockedNavigationAction . current = undefined ;
7983 shouldNavigateBack . current = false ;
8084 } }
85+ onModalHide = { ( ) => {
86+ shouldNavigateBack . current = false ;
87+ onCancel ?.( ) ;
88+ } }
8189 />
8290 ) ;
8391}
0 commit comments