Skip to content

Commit

Permalink
Merge pull request #11614 from vladamx/vladamx-open-old-dot-link-offline
Browse files Browse the repository at this point in the history
Open old dot link when offline or there is network error
  • Loading branch information
sketchydroide authored Oct 13, 2022
2 parents 8aec877 + b6b9c6a commit 44c75aa
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/libs/actions/Link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import {Linking} from 'react-native';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';
import Growl from '../Growl';
import * as Localize from '../Localize';
Expand Down Expand Up @@ -35,28 +36,36 @@ function showGrowlIfOffline() {
* @param {String} url
*/
function openOldDotLink(url) {
if (showGrowlIfOffline()) {
return;
}

/**
* @param {String} [shortLivedAuthToken]
* @returns {String}
*/
function buildOldDotURL(shortLivedAuthToken) {
const hasHashParams = url.indexOf('#') !== -1;
const hasURLParams = url.indexOf('?') !== -1;

const authTokenParam = shortLivedAuthToken ? `authToken=${shortLivedAuthToken}` : '';
const emailParam = `email=${encodeURIComponent(currentUserEmail)}`;

const params = _.compact([authTokenParam, emailParam]).join('&');

// If the URL contains # or ?, we can assume they don't need to have the `?` token to start listing url parameters.
return `${CONFIG.EXPENSIFY.EXPENSIFY_URL}${url}${hasHashParams || hasURLParams ? '&' : '?'}authToken=${shortLivedAuthToken}&email=${encodeURIComponent(currentUserEmail)}`;
return `${CONFIG.EXPENSIFY.EXPENSIFY_URL}${url}${hasHashParams || hasURLParams ? '&' : '?'}${params}`;
}

if (isNetworkOffline) {
Linking.openURL(buildOldDotURL());
return;
}

// We use makeRequestWithSideEffects here because we need to block until after we get the shortLivedAuthToken back from the server (link won't work without it!).
// If shortLivedAuthToken is not accessible, fallback to opening the link without the token.
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(
'OpenOldDotLink', {}, {},
).then((response) => {
if (response.jsonCode === 200) {
Linking.openURL(buildOldDotURL(response.shortLivedAuthToken));
} else {
Growl.show(response.message, CONST.GROWL.WARNING);
}
Linking.openURL(buildOldDotURL(response.shortLivedAuthToken));
}).catch(() => {
Linking.openURL(buildOldDotURL());
});
}

Expand Down

0 comments on commit 44c75aa

Please sign in to comment.