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

Android - Deeplink navigation does not work for RHN links. #28278

Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -38,3 +42,5 @@ export default {
GROUPS_WORKSPACES_OLDDOT: 'GroupWorkspaces_OLDDOT',
CARDS_AND_DOMAINS_OLDDOT: 'CardsAndDomains_OLDDOT',
} as const;

export {PROTECTED_SCREENS};
58 changes: 57 additions & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<void>} 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,
Expand All @@ -270,6 +325,7 @@ export default {
setIsNavigationReady,
getTopmostReportId,
getRouteNameFromStateEvent,
waitForProtectedRoutes,
getTopMostCentralPaneRouteName,
getTopmostReportActionId,
};
Expand Down
16 changes: 10 additions & 6 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
});
});
}
Expand Down