diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index 245d5d7340b..aa3083c573d 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -6,40 +6,28 @@ import lodashGet from 'lodash/get'; import linkingConfig from './linkingConfig'; import navigationRef from './navigationRef'; +/** + * @returns {Object} + */ +function getActiveState() { + // We use our RootState as the dispatch's state is relative to the active navigator and might not contain our active screen. + return navigationRef.current.getRootState(); +} + /** * Go back to the Main Drawer * @param {Object} navigationRef */ function navigateBackToRootDrawer() { - let isLeavingNestedDrawerNavigator = false; - - // This should take us to the first view of the modal's stack navigator - navigationRef.current.dispatch((state) => { - // If this is a nested drawer navigator then we pop the screen and - // prevent calling goBack() as it's default behavior is to toggle open the active drawer - if (state.type === 'drawer') { - isLeavingNestedDrawerNavigator = true; - return StackActions.pop(); - } - - // If there are multiple routes then we can pop back to the first route - if (state.routes.length > 1) { - return StackActions.popToTop(); - } - - // Otherwise, we are already on the last page of a modal so just do nothing here as goBack() will navigate us - // back to the screen we were on before we opened the modal. - return StackActions.pop(0); + const activeState = getActiveState(); + + // To navigate to the main drawer Route, pop to the first route on the Root Stack Navigator as the main drawer is always the first route that is activated. + // It will pop all fullscreen and RHN modals that are over the main drawer. + // It won't work when the main drawer is not the first route of the Root Stack Navigator which is not the case ATM. + navigationRef.current.dispatch({ + ...StackActions.popToTop(), + target: activeState.key, }); - - if (isLeavingNestedDrawerNavigator) { - return; - } - - // Navigate back to where we were before we launched the modal - if (navigationRef.current.canGoBack()) { - navigationRef.current.goBack(); - } } /** @@ -69,14 +57,6 @@ function getScreenNameFromState(state) { return getRouteFromState(state).name || ''; } -/** - * @returns {Object} - */ -function getActiveState() { - // We use our RootState as the dispatch's state is relative to the active navigator and might not contain our active screen. - return navigationRef.current.getRootState(); -} - /** * Special accomodation must be made for navigating to a screen inside a DrawerNavigator (e.g. our ReportScreen). The web/mWeb default behavior when * calling "navigate()" does not give us the browser history we would expect for a typical web paradigm (e.g. that navigating from one screen another