Skip to content

Commit

Permalink
refactor asyncOpenUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins committed Oct 13, 2021
1 parent 4c82581 commit 75b398f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
23 changes: 4 additions & 19 deletions src/libs/actions/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {translateLocal} from '../translate';
import CONST from '../../CONST';
import * as API from '../API';
import CONFIG from '../../CONFIG';
import asyncOpenURL from '../asyncOpenURL';

let isNetworkOffline = false;
Onyx.connect({
Expand Down Expand Up @@ -35,25 +36,9 @@ function showGrowlIfOffline() {
*/
function openOldDotLink(url) {
if (!showGrowlIfOffline()) {
// Safari blocks opening new windows inside async calls.
// The workaround is to open a new window before the async call and then update its location to the correct url
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
let windowRef;

if (isSafari) {
windowRef = window.open();
}

API.GetShortLivedAuthToken().then(({shortLivedAuthToken}) => {
// eslint-disable-next-line max-len
const oldDotURL = `${CONFIG.EXPENSIFY.URL_EXPENSIFY_COM}${url}${url.indexOf('?') === -1 ? '?' : '&'}authToken=${shortLivedAuthToken}&email=${encodeURIComponent(currentUserEmail)}`;

if (isSafari) {
windowRef.location = oldDotURL;
} else {
Linking.openURL(oldDotURL);
}
});
// eslint-disable-next-line max-len
const buildOldDotURL = ({shortLivedAuthToken}) => `${CONFIG.EXPENSIFY.URL_EXPENSIFY_COM}${url}${url.indexOf('?') === -1 ? '?' : '&'}authToken=${shortLivedAuthToken}&email=${encodeURIComponent(currentUserEmail)}`;
asyncOpenURL(API.GetShortLivedAuthToken(), buildOldDotURL);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libs/asyncOpenURL/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function asyncOpenURL(promise, url) {
return;
}

promise.then(() => {
Linking.openURL(url);
promise.then((params) => {
Linking.openURL(typeof url === 'string' ? url : url(params));
});
}
8 changes: 4 additions & 4 deletions src/libs/asyncOpenURL/index.website.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default function asyncOpenURL(promise, url) {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

if (!isSafari) {
promise.then(() => {
Linking.openURL(url);
promise.then((params) => {
Linking.openURL(typeof url === 'string' ? url : url(params));
});
} else {
const windowRef = window.open();
promise
.then(() => {
windowRef.location = url;
.then((params) => {
windowRef.location = typeof url === 'string' ? url : url(params);
})
.catch(() => windowRef.close());
}
Expand Down

0 comments on commit 75b398f

Please sign in to comment.