diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index 623ce67a3957..81daa35e5768 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -202,17 +202,10 @@ type GoBackOptions = { * In that case we want to goUp to a country picker with any params so we don't compare them. */ compareParams?: boolean; - - /** - * Specifies whether goBack should pop to top when invoked. - * Additionaly, to execute popToTop, set the value of the global variable ShouldPopAllStateOnUP to true using the setShouldPopAllStateOnUP function. - */ - shouldPopToTop?: boolean; }; const defaultGoBackOptions: Required = { compareParams: true, - shouldPopToTop: false, }; /** @@ -274,21 +267,13 @@ function goUp(fallbackRoute: Route, options?: GoBackOptions) { /** * @param fallbackRoute - Fallback route if pop/goBack action should, but is not possible within RHP - * @param options - Optional configuration that affects navigation logic, e.g. whether goBack should popToTop. + * @param options - Optional configuration that affects navigation logic. */ function goBack(fallbackRoute?: Route, options?: GoBackOptions) { if (!canNavigate('goBack')) { return; } - if (options?.shouldPopToTop) { - if (shouldPopAllStateOnUP) { - shouldPopAllStateOnUP = false; - navigationRef.current?.dispatch(StackActions.popToTop()); - return; - } - } - if (fallbackRoute) { goUp(fallbackRoute, options); return; @@ -485,6 +470,18 @@ const dismissModalWithReport = (report: OnyxEntry) => { isNavigationReady().then(() => navigateToReportWithPolicyCheck({report})); }; +/** + * Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopAllStateOnUP is set to true. + */ +function popToTop() { + if (!shouldPopAllStateOnUP) { + return; + } + + shouldPopAllStateOnUP = false; + navigationRef.current?.dispatch(StackActions.popToTop()); +} + export default { setShouldPopAllStateOnUP, navigate, @@ -508,6 +505,7 @@ export default { closeRHPFlow, setNavigationActionToMicrotaskQueue, navigateToReportWithPolicyCheck, + popToTop, }; export {navigationRef}; diff --git a/src/libs/navigateAfterJoinRequest/index.desktop.ts b/src/libs/navigateAfterJoinRequest/index.desktop.ts index 9b72ee30de57..461f5376e6c1 100644 --- a/src/libs/navigateAfterJoinRequest/index.desktop.ts +++ b/src/libs/navigateAfterJoinRequest/index.desktop.ts @@ -4,7 +4,7 @@ import ROUTES from '@src/ROUTES'; const navigateAfterJoinRequest = () => { // @TODO: Check if this method works the same as on the main branch - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); if (getIsSmallScreenWidth()) { Navigation.navigate(ROUTES.SETTINGS); } diff --git a/src/libs/navigateAfterJoinRequest/index.ts b/src/libs/navigateAfterJoinRequest/index.ts index a3ac50cd59be..91b2fdade606 100644 --- a/src/libs/navigateAfterJoinRequest/index.ts +++ b/src/libs/navigateAfterJoinRequest/index.ts @@ -3,7 +3,7 @@ import ROUTES from '@src/ROUTES'; const navigateAfterJoinRequest = () => { // @TODO: Check if this method works the same as on the main branch - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); Navigation.navigate(ROUTES.SETTINGS); Navigation.navigate(ROUTES.SETTINGS_WORKSPACES); }; diff --git a/src/libs/navigateAfterJoinRequest/index.web.ts b/src/libs/navigateAfterJoinRequest/index.web.ts index 9b72ee30de57..461f5376e6c1 100644 --- a/src/libs/navigateAfterJoinRequest/index.web.ts +++ b/src/libs/navigateAfterJoinRequest/index.web.ts @@ -4,7 +4,7 @@ import ROUTES from '@src/ROUTES'; const navigateAfterJoinRequest = () => { // @TODO: Check if this method works the same as on the main branch - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); if (getIsSmallScreenWidth()) { Navigation.navigate(ROUTES.SETTINGS); } diff --git a/src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx b/src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx index 060d5664ac93..a273b210efa9 100644 --- a/src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx +++ b/src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx @@ -52,7 +52,7 @@ function AddBankAccount() { PaymentMethods.continueSetup(onSuccessFallbackRoute); return; } - Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true}); + Navigation.goBack(ROUTES.SETTINGS_WALLET); }; const handleBackButtonPress = () => { @@ -63,7 +63,7 @@ function AddBankAccount() { if (screenIndex === 0) { BankAccounts.clearPersonalBankAccount(); Wallet.updateCurrentStep(null); - Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true}); + Navigation.goBack(ROUTES.SETTINGS_WALLET); return; } prevScreen(); diff --git a/src/pages/EnablePayments/EnablePayments.tsx b/src/pages/EnablePayments/EnablePayments.tsx index b8aeb4103a59..742202e43bb3 100644 --- a/src/pages/EnablePayments/EnablePayments.tsx +++ b/src/pages/EnablePayments/EnablePayments.tsx @@ -46,7 +46,7 @@ function EnablePaymentsPage() { > Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true})} + onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)} /> diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 0f80147cbfa4..be0b049e67d0 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -295,7 +295,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro return; } // @TODO: Check if this method works the same as on the main branch - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); }, [isInNarrowPaneModal]); let headerView = ( @@ -592,7 +592,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro if (Navigation.getTopmostReportId() === prevOnyxReportID) { Navigation.setShouldPopAllStateOnUP(true); // @TODO: Check if this method works the same as on the main branch - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); } if (prevReport?.parentReportID) { // Prevent navigation to the IOU/Expense Report if it is pending deletion. diff --git a/src/pages/workspace/WorkspaceJoinUserPage.tsx b/src/pages/workspace/WorkspaceJoinUserPage.tsx index 66caff7263ef..6e8333d75451 100644 --- a/src/pages/workspace/WorkspaceJoinUserPage.tsx +++ b/src/pages/workspace/WorkspaceJoinUserPage.tsx @@ -47,7 +47,7 @@ function WorkspaceJoinUserPage({route, policy}: WorkspaceJoinUserPageProps) { Navigation.isNavigationReady().then(() => { // @TODO: Check if this method works the same as on the main branch // NOTE: It probably doesn't need any params. When this method is called, shouldPopAllStateOnUP is always false - Navigation.goBack(undefined, {shouldPopToTop: true}); + Navigation.popToTop(); Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID ?? '-1')); }); return;