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 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
46 changes: 46 additions & 0 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,51 @@ function setIsNavigationReady() {
resolveNavigationIsReadyPromise();
}

function navContainsConcierge(state) {
if (!state || !state.routeNames || !_.isArray(state.routeNames)) {
return false;
}

return _.includes(state.routeNames, SCREENS.CONCIERGE);
}

/**
* Waits for the navigation state to contain protected routes (specifically 'Concierge').
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments need to be updated now

* If the navigation is in a state, where protected routes are available, the promise will resolve immediately.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure I understand, is "protected route" a new concept we're introducing? I'm not sure I understand what it has to do with Concierge specifically.

Copy link
Contributor Author

@lukemorawski lukemorawski Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it's just the way routes hidden behind the auth process are called. Since Concierge is the one of the most important screens that is available to users after logging in, I'm using it to detect the change and readiness of the navigation state. In simple words - when Concierge is present as one of the available routes in the nav state, this means that app is showing protected routes.
Main cause of the whole bug was, that the app was trying to navigate to a protected route immediately after logging in, but the navigation was not ready yet and it was throwing an error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way we can wait for navigation to finish? I'm not sure I like the idea of checking if Concierge is an available route before allowing navigation. There might be situations in the future where the Concierge route is not available (I.e anonymous accounts)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, though with current nav setup it's hard to come up with a better way. If the navigation was done using protected route guard components, that would be easier (or even unnecessary), but there's a conditional nav tree rendering in <AppNavigator /> component and the nav state is not immediately following the rendering state. The other way around this problem would be to introduce some global (app wide) state, a flag perhaps, that would indicate the readiness of the nav tree. This would require every protected page, and every public page to update this flag on it's mounting. So when a protected page (screen) is loaded then it updates this new flag to, lets say true, and when a public screen is loaded it updates this flag to false. But that requires a lot of additional code all over the app. That would be the most robust approach I guess.
The other would be to create a separate const of protected routes, that would be spreaded to the const of other screens in the SCREENS.ts, but it's a bit more tricky. Currently checking that option.

*
* @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 (navContainsConcierge(currentState)) {
resolve();
return;
}
let unsubscribe;
const handleStateChange = ({data}) => {
const state = lodashGet(data, 'state');
if (navContainsConcierge(state)) {
unsubscribe();
resolve();
}
};
unsubscribe = navigationRef.current.addListener('state', handleStateChange);
});
}

export default {
setShouldPopAllStateOnUP,
canNavigate,
Expand All @@ -272,6 +317,7 @@ export default {
setIsNavigationReady,
getTopmostReportId,
getRouteNameFromStateEvent,
waitForProtectedRoutes,
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 @@ -1839,7 +1839,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 @@ -1858,11 +1857,16 @@ function openReportFromDeepLink(url, isAuthenticated) {
// Navigate to the report after sign-in/sign-up.
InteractionManager.runAfterInteractions(() => {
Session.waitForUserSignIn().then(() => {
if (route === ROUTES.CONCIERGE) {
navigateToConciergeChat();
return;
}
Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH);
Navigation.waitForProtectedRoutes()
.then(() => {
const route = ReportUtils.getRouteFromLink(url);
if (route === ROUTES.CONCIERGE) {
navigateToConciergeChat();
return;
}
Navigation.navigate(route, CONST.NAVIGATION.TYPE.PUSH);
})
.catch((error) => Log.warn(error.message));
});
});
}
Expand Down
Loading