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

[No QA] [TS migration] Migrate 'Link.js' lib to TypeScript #28257

Merged
merged 10 commits into from
Nov 6, 2023
39 changes: 12 additions & 27 deletions src/libs/actions/Link.js → src/libs/actions/Link.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';
import asyncOpenURL from '../asyncOpenURL';
import * as API from '../API';
import * as Environment from '../Environment/Environment';
import * as Url from '../Url';

let isNetworkOffline = false;
let isNetworkOffline: boolean | undefined = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => (isNetworkOffline = lodashGet(val, 'isOffline', false)),
callback: (value) => (isNetworkOffline = value?.isOffline ?? false),
});

let currentUserEmail;
let currentUserEmail: string | undefined;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => (currentUserEmail = lodashGet(val, 'email', '')),
callback: (value) => (currentUserEmail = value?.email ?? ''),
});

/**
* @param {String} [url] the url path
* @param {String} [shortLivedAuthToken]
*
* @returns {Promise<string>}
*/
function buildOldDotURL(url, shortLivedAuthToken) {
const hasHashParams = url.indexOf('#') !== -1;
const hasURLParams = url.indexOf('?') !== -1;
function buildOldDotURL(url?: string, shortLivedAuthToken?: string): Promise<string> {
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
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('&');
const emailParam = `email=${encodeURIComponent(currentUserEmail ?? '')}`;
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
const paramsArray = [authTokenParam, emailParam];
const params = paramsArray.filter(Boolean).join('&');
Copy link
Contributor

Choose a reason for hiding this comment

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

i love this pattern, congrats @kubabutkiewicz 🔥

Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB: I don't see the need of introducing new variable

Suggested change
const paramsArray = [authTokenParam, emailParam];
const params = paramsArray.filter(Boolean).join('&');
const params = [authTokenParam, emailParam].filter(Boolean).join('&');

Copy link
Contributor

Choose a reason for hiding this comment

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

Any feedback?

Copy link
Contributor

Choose a reason for hiding this comment

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

bump, though not blocker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for me it was looking more clear when did that, should I revert that?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine


return Environment.getOldDotEnvironmentURL().then((environmentURL) => {
const oldDotDomain = Url.addTrailingForwardSlash(environmentURL);
Expand All @@ -42,18 +34,11 @@ function buildOldDotURL(url, shortLivedAuthToken) {
});
}

/**
* @param {String} url
* @param {Boolean} shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave this note in? It might be useful for the future!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added @dangrous 😄

*/
function openExternalLink(url, shouldSkipCustomSafariLogic = false) {
function openExternalLink(url: string, shouldSkipCustomSafariLogic = false) {
asyncOpenURL(Promise.resolve(), url, shouldSkipCustomSafariLogic);
}

/**
* @param {String} url the url path
*/
function openOldDotLink(url) {
function openOldDotLink(url: string) {
if (isNetworkOffline) {
buildOldDotURL(url).then((oldDotURL) => openExternalLink(oldDotURL));
return;
Expand Down