Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

27168 Deeplinking not working after logout #31316

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -28,3 +33,5 @@ export default {
DESKTOP_SIGN_IN_REDIRECT: 'DesktopSignInRedirect',
SAML_SIGN_IN: 'SAMLSignIn',
} as const;

export {PROTECTED_SCREENS};
55 changes: 54 additions & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<void>} 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,
Expand All @@ -320,6 +371,8 @@ export default {
getTopmostReportId,
getRouteNameFromStateEvent,
getTopmostReportActionId,
waitForProtectedRoutes,
navContainsProtectedRoutes,
};

export {navigationRef};
4 changes: 2 additions & 2 deletions src/libs/actions/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ function extractFirstAndLastNameFromAvailableDetails({login, displayName, firstN
return {firstName: '', lastName: ''};
}

const firstSpaceIndex = displayName.indexOf(' ');
const lastSpaceIndex = displayName.lastIndexOf(' ');
const firstSpaceIndex = displayName?.indexOf(' ') ?? -1;
const lastSpaceIndex = displayName?.lastIndexOf(' ') ?? -1;
if (firstSpaceIndex === -1) {
return {firstName: displayName, lastName: ''};
}
Expand Down
22 changes: 11 additions & 11 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,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) {
Expand All @@ -2003,17 +2002,18 @@ 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;
}
if (Session.isAnonymousUser() && !Session.canAccessRouteByAnonymousUser(route)) {
Navigation.isNavigationReady().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);
});
});
});
}
Expand Down
Loading