-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Link.js
97 lines (84 loc) · 2.94 KB
/
Link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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';
import CONST from '../../CONST';
import asyncOpenURL from '../asyncOpenURL';
import * as API from '../API';
import * as Environment from '../Environment/Environment';
import * as Url from '../Url';
let isNetworkOffline = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: val => isNetworkOffline = lodashGet(val, 'isOffline', false),
});
let currentUserEmail;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => currentUserEmail = lodashGet(val, 'email', ''),
});
/**
* @returns {Boolean}
*/
function showGrowlIfOffline() {
if (isNetworkOffline) {
Growl.show(Localize.translateLocal('session.offlineMessageRetry'), CONST.GROWL.WARNING);
}
return isNetworkOffline;
}
/**
* @param {String} url
*/
function openOldDotLink(url) {
/**
* @param {String} [shortLivedAuthToken]
* @returns {Promise<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('&');
return Environment.getOldDotEnvironmentURL()
.then((environmentURL) => {
const oldDotDomain = Url.addTrailingForwardSlash(environmentURL);
// If the URL contains # or ?, we can assume they don't need to have the `?` token to start listing url parameters.
return `${oldDotDomain}${url}${hasHashParams || hasURLParams ? '&' : '?'}${params}`;
});
}
if (isNetworkOffline) {
buildOldDotURL().then(oldDotURL => Linking.openURL(oldDotURL));
return;
}
// 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) => {
buildOldDotURL(response.shortLivedAuthToken).then((oldDotUrl) => {
Linking.openURL(oldDotUrl);
});
}).catch(() => {
buildOldDotURL().then((oldDotUrl) => {
Linking.openURL(oldDotUrl);
});
});
}
/**
* @param {String} url
* @param {Boolean} shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari.
*/
function openExternalLink(url, shouldSkipCustomSafariLogic = false) {
if (showGrowlIfOffline()) {
return;
}
asyncOpenURL(Promise.resolve(), url, shouldSkipCustomSafariLogic);
}
export {
openOldDotLink,
openExternalLink,
};