From 904e3eea460444ff883d0091a78a543f2598aadb Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Tue, 29 Mar 2022 19:47:59 -0600 Subject: [PATCH 1/6] Navigate to correct report on push notification tap --- src/libs/Navigation/Navigation.js | 21 +++++++++++++++++++++ src/libs/actions/Report.js | 13 +++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 9e0312aef2f1..db23c2603e2e 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -35,6 +35,14 @@ function setDidTapNotification() { didTapNotificationBeforeReady = true; } +/** + * Returns true if the Navigation is ready to navigate + * @returns {boolean} + */ +function isReady() { + return navigationRef.isReady(); +} + /** * @param {String} methodName * @param {Object} params @@ -185,6 +193,17 @@ function isActiveRoute(routePath) { return path === routePath; } +/** + * Returns whether we have a chat visible or not + * @returns {boolean} + */ +function isChatVisible() { + const path = navigationRef.current && navigationRef.current.getCurrentRoute().name + ? getPathFromState(navigationRef.current.getState(), linkingConfig.config).slice(0, 3) + : ''; + return path === '/r/'; +} + /** * Alternative to the `Navigation.dismissModal()` function that we can use inside * the render function of other components to avoid breaking React rules about side-effects. @@ -217,6 +236,8 @@ export default { navigate, dismissModal, isActiveRoute, + isChatVisible, + isReady, goBack, DismissModal, closeDrawer, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3aaf22887757..da6154138d61 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.isReady()) { + // If a chat is visible other than the one we are trying to navigate to, then we need to navigate back + if (Navigation.isChatVisible() && 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)}`); + } }); } From 7b3c15854a66a8d06391ab9e19ac1155d4766ad6 Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Thu, 31 Mar 2022 12:49:42 -0600 Subject: [PATCH 2/6] Tweak checking for report --- src/libs/Navigation/Navigation.js | 19 +++++++------------ src/libs/actions/Report.js | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index db23c2603e2e..c0d6edf78706 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'; @@ -194,14 +190,13 @@ function isActiveRoute(routePath) { } /** - * Returns whether we have a chat visible or not - * @returns {boolean} + * Returns the current active path + * @returns {String} */ -function isChatVisible() { - const path = navigationRef.current && navigationRef.current.getCurrentRoute().name - ? getPathFromState(navigationRef.current.getState(), linkingConfig.config).slice(0, 3) +function getActiveRoute() { + return navigationRef.current && navigationRef.current.getCurrentRoute().name + ? getPathFromState(navigationRef.current.getState(), linkingConfig.config) : ''; - return path === '/r/'; } /** @@ -236,7 +231,7 @@ export default { navigate, dismissModal, isActiveRoute, - isChatVisible, + getActiveRoute, isReady, goBack, DismissModal, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index da6154138d61..371150f6e522 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -799,7 +799,7 @@ function subscribeToReportCommentPushNotifications() { PushNotification.onSelected(PushNotification.TYPE.REPORT_COMMENT, ({reportID}) => { if (Navigation.isReady()) { // If a chat is visible other than the one we are trying to navigate to, then we need to navigate back - if (Navigation.isChatVisible() && Navigation.isActiveRoute(`r/${reportID}`)) { + if (Navigation.getActiveRoute().slice(1, 2) === ROUTES.REPORT && !Navigation.isActiveRoute(`r/${reportID}`)) { Navigation.goBack(); } Navigation.navigate(ROUTES.getReportRoute(reportID)); From 0763eabeddf53aad7d474010b098d4304ca7e480 Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Thu, 31 Mar 2022 12:56:59 -0600 Subject: [PATCH 3/6] Update comment --- src/libs/Navigation/Navigation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index c0d6edf78706..00724a465d52 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -190,7 +190,7 @@ function isActiveRoute(routePath) { } /** - * Returns the current active path + * Returns the current active route * @returns {String} */ function getActiveRoute() { From 3467f88020b074c6ad0cfcfc0ff7c1b1200d7dde Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Mon, 4 Apr 2022 13:47:49 -0600 Subject: [PATCH 4/6] Update src/libs/Navigation/Navigation.js Co-authored-by: Marc Glasser --- src/libs/Navigation/Navigation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 00724a465d52..d3ddd944d989 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -33,7 +33,7 @@ function setDidTapNotification() { /** * Returns true if the Navigation is ready to navigate - * @returns {boolean} + * @returns {Boolean} */ function isReady() { return navigationRef.isReady(); From 86050cc2a9b5dbe43e70c34e9727276404d9d4c6 Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Mon, 4 Apr 2022 14:08:49 -0600 Subject: [PATCH 5/6] Use `getActiveRoute` in `isActiveRoute` --- src/libs/Navigation/Navigation.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index d3ddd944d989..96323fca0cef 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -172,6 +172,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. * @@ -183,20 +193,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; -} - -/** - * Returns the current active route - * @returns {String} - */ -function getActiveRoute() { - return navigationRef.current && navigationRef.current.getCurrentRoute().name - ? getPathFromState(navigationRef.current.getState(), linkingConfig.config) - : ''; + return getActiveRoute().substring(1) === routePath; } /** From 2ef127776e5612016f3772b945fefa61e1bc6d04 Mon Sep 17 00:00:00 2001 From: Andrew Gable Date: Mon, 4 Apr 2022 14:13:27 -0600 Subject: [PATCH 6/6] Use `canNavigate` instead of `isReady` --- src/libs/Navigation/Navigation.js | 10 +--------- src/libs/actions/Report.js | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 96323fca0cef..360afa67afc9 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -31,14 +31,6 @@ function setDidTapNotification() { didTapNotificationBeforeReady = true; } -/** - * Returns true if the Navigation is ready to navigate - * @returns {Boolean} - */ -function isReady() { - return navigationRef.isReady(); -} - /** * @param {String} methodName * @param {Object} params @@ -225,11 +217,11 @@ DismissModal.defaultProps = { }; export default { + canNavigate, navigate, dismissModal, isActiveRoute, getActiveRoute, - isReady, goBack, DismissModal, closeDrawer, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 371150f6e522..c7ccb84500b3 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -797,7 +797,7 @@ function subscribeToReportCommentPushNotifications() { // Open correct report when push notification is clicked PushNotification.onSelected(PushNotification.TYPE.REPORT_COMMENT, ({reportID}) => { - if (Navigation.isReady()) { + 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();