diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 9e0312aef2f..360afa67afc 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -1,11 +1,7 @@ import _ from 'underscore'; import React from 'react'; import {Keyboard} from 'react-native'; -import { - StackActions, - DrawerActions, - getPathFromState, -} from '@react-navigation/native'; +import {DrawerActions, getPathFromState, StackActions} from '@react-navigation/native'; import PropTypes from 'prop-types'; import Onyx from 'react-native-onyx'; import Log from '../Log'; @@ -168,6 +164,16 @@ function dismissModal(shouldOpenDrawer = false) { } } +/** + * Returns the current active route + * @returns {String} + */ +function getActiveRoute() { + return navigationRef.current && navigationRef.current.getCurrentRoute().name + ? getPathFromState(navigationRef.current.getState(), linkingConfig.config) + : ''; +} + /** * Check whether the passed route is currently Active or not. * @@ -179,10 +185,7 @@ function dismissModal(shouldOpenDrawer = false) { */ function isActiveRoute(routePath) { // We remove First forward slash from the URL before matching - const path = navigationRef.current && navigationRef.current.getCurrentRoute().name - ? getPathFromState(navigationRef.current.getState(), linkingConfig.config).substring(1) - : ''; - return path === routePath; + return getActiveRoute().substring(1) === routePath; } /** @@ -214,9 +217,11 @@ DismissModal.defaultProps = { }; export default { + canNavigate, navigate, dismissModal, isActiveRoute, + getActiveRoute, goBack, DismissModal, closeDrawer, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3aaf2288775..c7ccb84500b 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -797,8 +797,17 @@ function subscribeToReportCommentPushNotifications() { // Open correct report when push notification is clicked PushNotification.onSelected(PushNotification.TYPE.REPORT_COMMENT, ({reportID}) => { - Navigation.setDidTapNotification(); - Linking.openURL(`${CONST.DEEPLINK_BASE_URL}${ROUTES.getReportRoute(reportID)}`); + if (Navigation.canNavigate('navigate')) { + // If a chat is visible other than the one we are trying to navigate to, then we need to navigate back + if (Navigation.getActiveRoute().slice(1, 2) === ROUTES.REPORT && !Navigation.isActiveRoute(`r/${reportID}`)) { + Navigation.goBack(); + } + Navigation.navigate(ROUTES.getReportRoute(reportID)); + } else { + // Navigation container is not yet ready, use deeplinking to open to correct report instead + Navigation.setDidTapNotification(); + Linking.openURL(`${CONST.DEEPLINK_BASE_URL}${ROUTES.getReportRoute(reportID)}`); + } }); }