diff --git a/src/SCREENS.ts b/src/SCREENS.ts index f7de8cfab4b6..afc368858f55 100644 --- a/src/SCREENS.ts +++ b/src/SCREENS.ts @@ -2,15 +2,20 @@ * 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', @@ -28,3 +33,5 @@ export default { DESKTOP_SIGN_IN_REDIRECT: 'DesktopSignInRedirect', SAML_SIGN_IN: 'SAMLSignIn', } as const; + +export {PROTECTED_SCREENS}; diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index e12cb5545240..7a2c61ea7b53 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -6,7 +6,7 @@ import Log from '@libs/Log'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import ROUTES from '@src/ROUTES'; -import SCREENS from '@src/SCREENS'; +import SCREENS, {PROTECTED_SCREENS} from '@src/SCREENS'; import getStateFromPath from './getStateFromPath'; import originalGetTopmostReportActionId from './getTopmostReportActionID'; import originalGetTopmostReportId from './getTopmostReportId'; @@ -305,6 +305,57 @@ 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 protectedScreensName = _.values(PROTECTED_SCREENS); + const difference = _.difference(protectedScreensName, state.routeNames); + + return !difference.length; +} + +/** + * Waits for the navitgation state to contain protected routes specified in PROTECTED_SCREENS constant. + * If the navigation is in a state, where protected routes are avilable, the promise resolve immediately. + * + * @function + * @returns {Promise} A promise that resolves when the one of the PROTECTED_SCREENS screen is available in the nav tree. + * + * @example + * waitForProtectedRoutes() + * .then(()=> console.log('Protected routes are present!')) + */ +function waitForProtectedRoutes() { + return new Promise((resolve) => { + isNavigationReady().then(() => { + 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, @@ -320,6 +371,8 @@ export default { getTopmostReportId, getRouteNameFromStateEvent, getTopmostReportActionId, + waitForProtectedRoutes, + navContainsProtectedRoutes, }; export {navigationRef}; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index ffb68203cad0..c225bdf5b65d 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -20,7 +20,6 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as Pusher from '@libs/Pusher/pusher'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; -import SidebarUtils from '@libs/SidebarUtils'; import * as UserUtils from '@libs/UserUtils'; import Visibility from '@libs/Visibility'; import CONFIG from '@src/CONFIG'; @@ -1986,7 +1985,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) { @@ -2004,18 +2002,19 @@ function openReportFromDeepLink(url, isAuthenticated) { // Navigate to the report after sign-in/sign-up. InteractionManager.runAfterInteractions(() => { - SidebarUtils.isSidebarLoadedReady().then(() => { - if (route === ROUTES.CONCIERGE) { - navigateToConciergeChat(true); - return; - } - if (Session.isAnonymousUser() && !Session.canAccessRouteByAnonymousUser(route)) { - Navigation.isNavigationReady().then(() => { + Session.waitForUserSignIn().then(() => { + Navigation.waitForProtectedRoutes().then(() => { + const route = ReportUtils.getRouteFromLink(url); + if (route === ROUTES.CONCIERGE) { + navigateToConciergeChat(true); + return; + } + if (Session.isAnonymousUser() && !Session.canAccessRouteByAnonymousUser(route)) { Session.signOutAndRedirectToSignIn(); - }); - return; - } - Navigation.navigate(route, CONST.NAVIGATION.ACTION_TYPE.PUSH); + return; + } + Navigation.navigate(route, CONST.NAVIGATION.ACTION_TYPE.PUSH); + }); }); }); }