diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index d29cbed73b9f..5f8762600acf 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -1,4 +1,5 @@ -import {CommonActions} from '@react-navigation/native'; +import {CommonActions, StackActions, DrawerActions} from '@react-navigation/native'; +import lodashGet from 'lodash/get'; /** * In order to create the desired browser navigation behavior on web and mobile web we need to replace any @@ -10,10 +11,24 @@ import {CommonActions} from '@react-navigation/native'; * * @param {String} screenName * @param {Object} params + * @param {Object} navigationRef * @returns {Function} */ -function pushDrawerRoute(screenName, params) { +function pushDrawerRoute(screenName, params, navigationRef) { return (state) => { + // Avoid the navigation and refocus the report if we're trying to navigate to our active report + // We use our RootState as the dispatch's state is relative to the active navigator and might + // not contain our active report. + const rootState = navigationRef.current.getRootState(); + const activeReportID = lodashGet(rootState, 'routes[0].state.routes[0].params.reportID', ''); + + if (activeReportID === params.reportID) { + if (state.type !== 'drawer') { + navigationRef.current.dispatch(StackActions.pop()); + } + return DrawerActions.closeDrawer(); + } + // Non Drawer navigators have routes and not history so we'll fallback to navigate() in the case where we are // unable to push a new screen onto the history stack e.g. navigating to a ReportScreen via a modal screen. // Note: One downside of this is that the history will be reset. diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 480d4fa4c3ba..8d0b307a0d00 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -62,7 +62,7 @@ function navigate(route = ROUTES.HOME) { // have a participants route since those should go through linkTo() as they open a different screen. const {reportID, isParticipantsRoute} = ROUTES.parseReportRouteParams(route); if (reportID && !isParticipantsRoute) { - navigationRef.current.dispatch(CustomActions.pushDrawerRoute(SCREENS.REPORT, {reportID})); + navigationRef.current.dispatch(CustomActions.pushDrawerRoute(SCREENS.REPORT, {reportID}, navigationRef)); return; }