diff --git a/src/SCREENS.ts b/src/SCREENS.ts index 0346168f0407..7df37045b959 100644 --- a/src/SCREENS.ts +++ b/src/SCREENS.ts @@ -2,15 +2,17 @@ * This is a file containing constants for all of the screen names. In most cases, we should use the routes for * navigation. But there are situations where we may need to access screen names directly. */ -export default { +const PROTECTED_SCREENS = { HOME: 'Home', + CONCIERGE: 'Concierge', + REPORT_ATTACHMENTS: 'ReportAttachments', +} as const; + +export default { + ...PROTECTED_SCREENS, LOADING: 'Loading', REPORT: 'Report', - REPORT_ATTACHMENTS: 'ReportAttachments', NOT_FOUND: 'not-found', - TRANSITION_BETWEEN_APPS: 'TransitionBetweenApps', - VALIDATE_LOGIN: 'ValidateLogin', - CONCIERGE: 'Concierge', SETTINGS: { ROOT: 'Settings_Root', PREFERENCES: 'Settings_Preferences', @@ -21,9 +23,11 @@ export default { SAVE_THE_WORLD: { ROOT: 'SaveTheWorld_Root', }, + TRANSITION_BETWEEN_APPS: 'TransitionBetweenApps', SIGN_IN_WITH_APPLE_DESKTOP: 'AppleSignInDesktop', SIGN_IN_WITH_GOOGLE_DESKTOP: 'GoogleSignInDesktop', DESKTOP_SIGN_IN_REDIRECT: 'DesktopSignInRedirect', + VALIDATE_LOGIN: 'ValidateLogin', // Iframe screens from olddot HOME_OLDDOT: 'Home_OLDDOT', @@ -38,3 +42,5 @@ export default { GROUPS_WORKSPACES_OLDDOT: 'GroupWorkspaces_OLDDOT', CARDS_AND_DOMAINS_OLDDOT: 'CardsAndDomains_OLDDOT', } as const; + +export {PROTECTED_SCREENS}; diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 912e7b23b3dc..65ba40886fda 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -12,7 +12,7 @@ import originalGetTopmostReportId from './getTopmostReportId'; import originalGetTopMostCentralPaneRouteName from './getTopMostCentralPaneRouteName'; import originalGetTopmostReportActionId from './getTopmostReportActionID'; import getStateFromPath from './getStateFromPath'; -import SCREENS from '../../SCREENS'; +import SCREENS, {PROTECTED_SCREENS} from '../../SCREENS'; import CONST from '../../CONST'; let resolveNavigationIsReadyPromise; @@ -257,6 +257,61 @@ function setIsNavigationReady() { resolveNavigationIsReadyPromise(); } +/** + * Checks if the navigation state contains routes that are protected (over the auth wall). + * + * @function + * @param {Object} state - react-navigation state object + * + * @returns {Boolean} + */ +function navContainsProtectedRoutes(state) { + if (!state || !state.routeNames || !_.isArray(state.routeNames)) { + return false; + } + const protectedScreensNames = _.values(PROTECTED_SCREENS); + const difference = _.difference(protectedScreensNames, state.routeNames); + + return !difference.length; +} + +/** + * Waits for the navigation state to contain protected routes specified in PROTECTED_SCREENS constant + * If the navigation is in a state, where protected routes are available, the promise will resolve immediately. + * + * @function + * @returns {Promise} A promise that resolves to `true` when the Concierge route is present. + * Rejects with an error if the navigation is not ready. + * + * @example + * waitForProtectedRoutes() + * .then(() => console.log('Protected routes are present!')) + * .catch(error => console.error(error.message)); + */ +function waitForProtectedRoutes() { + return new Promise((resolve, reject) => { + const isReady = navigationRef.current && navigationRef.current.isReady(); + if (!isReady) { + reject(new Error('[Navigation] is not ready yet!')); + return; + } + const currentState = navigationRef.current.getState(); + if (navContainsProtectedRoutes(currentState)) { + resolve(); + return; + } + let unsubscribe; + const handleStateChange = ({data}) => { + const state = lodashGet(data, 'state'); + if (navContainsProtectedRoutes(state)) { + unsubscribe(); + resolve(); + } + }; + unsubscribe = navigationRef.current.addListener('state', handleStateChange); + }); +} + export default { setShouldPopAllStateOnUP, canNavigate, @@ -270,6 +325,7 @@ export default { setIsNavigationReady, getTopmostReportId, getRouteNameFromStateEvent, + waitForProtectedRoutes, getTopMostCentralPaneRouteName, getTopmostReportActionId, }; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 87b907872759..bbc5ddeadd82 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1883,7 +1883,6 @@ function toggleEmojiReaction(reportID, reportAction, reactionObject, existingRea * @param {Boolean} isAuthenticated */ function openReportFromDeepLink(url, isAuthenticated) { - const route = ReportUtils.getRouteFromLink(url); const reportID = ReportUtils.getReportIDFromLink(url); if (reportID && !isAuthenticated) { @@ -1902,11 +1901,16 @@ function openReportFromDeepLink(url, isAuthenticated) { // Navigate to the report after sign-in/sign-up. InteractionManager.runAfterInteractions(() => { Session.waitForUserSignIn().then(() => { - if (route === ROUTES.CONCIERGE) { - navigateToConciergeChat(true); - return; - } - Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH); + Navigation.waitForProtectedRoutes() + .then(() => { + const route = ReportUtils.getRouteFromLink(url); + if (route === ROUTES.CONCIERGE) { + navigateToConciergeChat(true); + return; + } + Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH); + }) + .catch((error) => Log.warn(error.message)); }); }); }