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
32 changes: 10 additions & 22 deletions src/libs/actions/Link.js → src/libs/actions/Link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import * as API from '@libs/API';
import asyncOpenURL from '@libs/asyncOpenURL';
import * as Environment from '@libs/Environment/Environment';
Expand All @@ -10,29 +8,23 @@ import ONYXKEYS from '@src/ONYXKEYS';
let isNetworkOffline = false;
Onyx.connect({
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let isNetworkOffline = false;
let isNetworkOffline: boolean | undefined = false;

Same.

Copy link
Contributor

Choose a reason for hiding this comment

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

actually here it would stay false before this callback fires 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right @adhorodyski! Sorry @kubabutkiewicz , but could you revert this one?

key: ONYXKEYS.NETWORK,
callback: (val) => (isNetworkOffline = lodashGet(val, 'isOffline', false)),
callback: (value) => (isNetworkOffline = value?.isOffline ?? false),
});

let currentUserEmail;
let currentUserEmail = '';
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) {
function buildOldDotURL(url: string, shortLivedAuthToken?: string): Promise<string> {
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 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 @@ -43,17 +35,13 @@ function buildOldDotURL(url, shortLivedAuthToken) {
}

/**
* @param {String} url
* @param {Boolean} shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari.
* @param 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 All @@ -63,7 +51,7 @@ function openOldDotLink(url) {
asyncOpenURL(
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects('OpenOldDotLink', {}, {})
.then((response) => buildOldDotURL(url, response.shortLivedAuthToken))
.then((response) => (response ? buildOldDotURL(url, response.shortLivedAuthToken) : buildOldDotURL(url)))
.catch(() => buildOldDotURL(url)),
(oldDotURL) => oldDotURL,
);
Expand Down
1 change: 1 addition & 0 deletions src/types/onyx/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Response = {
authToken?: string;
encryptedAuthToken?: string;
message?: string;
shortLivedAuthToken?: string;
};

export default Response;
Loading