From ccd097261b73830514a44a657238b5ffb1d5ff36 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 12 Nov 2024 13:39:51 +0800 Subject: [PATCH 1/3] feat: open switch to expensify classic on the same tab for web --- src/libs/actions/Link.ts | 10 ++++++---- src/libs/asyncOpenURL/index.website.ts | 8 +++++--- src/libs/asyncOpenURL/types.ts | 2 +- src/libs/canOpenURLInSameTab/index.desktop.ts | 1 + src/libs/canOpenURLInSameTab/index.ts | 1 + src/libs/canOpenURLInSameTab/index.website.ts | 1 + .../settings/ExitSurvey/ExitSurveyConfirmPage.tsx | 2 +- 7 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 src/libs/canOpenURLInSameTab/index.desktop.ts create mode 100644 src/libs/canOpenURLInSameTab/index.ts create mode 100644 src/libs/canOpenURLInSameTab/index.website.ts diff --git a/src/libs/actions/Link.ts b/src/libs/actions/Link.ts index 4cda676d89e83..0250ea7b84a1a 100644 --- a/src/libs/actions/Link.ts +++ b/src/libs/actions/Link.ts @@ -65,13 +65,13 @@ function buildOldDotURL(url: string, shortLivedAuthToken?: string): Promise openExternalLink(oldDotURL)); + buildOldDotURL(url).then((oldDotURL) => openExternalLink(oldDotURL, undefined, shouldOpenInSameTab)); return; } @@ -82,6 +82,8 @@ function openOldDotLink(url: string) { .then((response) => (response ? buildOldDotURL(url, response.shortLivedAuthToken) : buildOldDotURL(url))) .catch(() => buildOldDotURL(url)), (oldDotURL) => oldDotURL, + undefined, + shouldOpenInSameTab, ); } diff --git a/src/libs/asyncOpenURL/index.website.ts b/src/libs/asyncOpenURL/index.website.ts index 4f6d95b76b8b3..923e5d74817c4 100644 --- a/src/libs/asyncOpenURL/index.website.ts +++ b/src/libs/asyncOpenURL/index.website.ts @@ -1,4 +1,6 @@ import {Linking} from 'react-native'; +import type {Linking as LinkingWeb} from 'react-native-web'; +import canOpenURLInSameTab from '@libs/canOpenURLInSameTab'; import Log from '@libs/Log'; import type AsyncOpenURL from './types'; @@ -6,17 +8,17 @@ import type AsyncOpenURL from './types'; * Prevents Safari from blocking pop-up window when opened within async call. * @param shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari. */ -const asyncOpenURL: AsyncOpenURL = (promise, url, shouldSkipCustomSafariLogic) => { +const asyncOpenURL: AsyncOpenURL = (promise, url, shouldSkipCustomSafariLogic, shouldOpenInSameTab) => { if (!url) { return; } const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); - if (!isSafari || shouldSkipCustomSafariLogic) { + if (!isSafari || shouldSkipCustomSafariLogic || shouldOpenInSameTab) { promise .then((params) => { - Linking.openURL(typeof url === 'string' ? url : url(params)); + (Linking.openURL as LinkingWeb['openURL'])(typeof url === 'string' ? url : url(params), shouldOpenInSameTab && canOpenURLInSameTab ? '_self' : undefined); }) .catch(() => { Log.warn('[asyncOpenURL] error occured while opening URL', {url}); diff --git a/src/libs/asyncOpenURL/types.ts b/src/libs/asyncOpenURL/types.ts index bf24756b0cc2a..320e2606222e1 100644 --- a/src/libs/asyncOpenURL/types.ts +++ b/src/libs/asyncOpenURL/types.ts @@ -1,3 +1,3 @@ -type AsyncOpenURL = (promise: Promise, url: string | ((params: T) => string), shouldSkipCustomSafariLogic?: boolean) => void; +type AsyncOpenURL = (promise: Promise, url: string | ((params: T) => string), shouldSkipCustomSafariLogic?: boolean, shouldOpenInSameTab?: boolean) => void; export default AsyncOpenURL; diff --git a/src/libs/canOpenURLInSameTab/index.desktop.ts b/src/libs/canOpenURLInSameTab/index.desktop.ts new file mode 100644 index 0000000000000..33136544dba26 --- /dev/null +++ b/src/libs/canOpenURLInSameTab/index.desktop.ts @@ -0,0 +1 @@ +export default false; diff --git a/src/libs/canOpenURLInSameTab/index.ts b/src/libs/canOpenURLInSameTab/index.ts new file mode 100644 index 0000000000000..33136544dba26 --- /dev/null +++ b/src/libs/canOpenURLInSameTab/index.ts @@ -0,0 +1 @@ +export default false; diff --git a/src/libs/canOpenURLInSameTab/index.website.ts b/src/libs/canOpenURLInSameTab/index.website.ts new file mode 100644 index 0000000000000..ff3177babdde0 --- /dev/null +++ b/src/libs/canOpenURLInSameTab/index.website.ts @@ -0,0 +1 @@ +export default true; diff --git a/src/pages/settings/ExitSurvey/ExitSurveyConfirmPage.tsx b/src/pages/settings/ExitSurvey/ExitSurveyConfirmPage.tsx index 00bfec6b05b0b..d5977827ef6a4 100644 --- a/src/pages/settings/ExitSurvey/ExitSurveyConfirmPage.tsx +++ b/src/pages/settings/ExitSurvey/ExitSurveyConfirmPage.tsx @@ -83,7 +83,7 @@ function ExitSurveyConfirmPage({route, navigation}: ExitSurveyConfirmPageProps) onPress={() => { ExitSurvey.switchToOldDot(); Navigation.dismissModal(); - Link.openOldDotLink(CONST.OLDDOT_URLS.INBOX); + Link.openOldDotLink(CONST.OLDDOT_URLS.INBOX, true); }} isDisabled={isOffline} /> From c78f113012d4db0566ac6d28df1b6b639ef91d26 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 12 Nov 2024 14:08:16 +0800 Subject: [PATCH 2/3] lint --- src/libs/asyncOpenURL/index.website.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/asyncOpenURL/index.website.ts b/src/libs/asyncOpenURL/index.website.ts index 923e5d74817c4..332619ad76a78 100644 --- a/src/libs/asyncOpenURL/index.website.ts +++ b/src/libs/asyncOpenURL/index.website.ts @@ -15,7 +15,7 @@ const asyncOpenURL: AsyncOpenURL = (promise, url, shouldSkipCustomSafariLogic, s const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); - if (!isSafari || shouldSkipCustomSafariLogic || shouldOpenInSameTab) { + if (!isSafari || !!shouldSkipCustomSafariLogic || !!shouldOpenInSameTab) { promise .then((params) => { (Linking.openURL as LinkingWeb['openURL'])(typeof url === 'string' ? url : url(params), shouldOpenInSameTab && canOpenURLInSameTab ? '_self' : undefined); From 9766df7d08a126e45f996437a29c901b1b1eb499 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 13 Nov 2024 20:11:07 +0800 Subject: [PATCH 3/3] simplify the logic --- src/libs/asyncOpenURL/index.website.ts | 4 +++- src/libs/canOpenURLInSameTab/index.desktop.ts | 1 - src/libs/canOpenURLInSameTab/index.ts | 1 - src/libs/canOpenURLInSameTab/index.website.ts | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 src/libs/canOpenURLInSameTab/index.desktop.ts delete mode 100644 src/libs/canOpenURLInSameTab/index.ts delete mode 100644 src/libs/canOpenURLInSameTab/index.website.ts diff --git a/src/libs/asyncOpenURL/index.website.ts b/src/libs/asyncOpenURL/index.website.ts index 332619ad76a78..ba7da73616c26 100644 --- a/src/libs/asyncOpenURL/index.website.ts +++ b/src/libs/asyncOpenURL/index.website.ts @@ -1,7 +1,8 @@ import {Linking} from 'react-native'; import type {Linking as LinkingWeb} from 'react-native-web'; -import canOpenURLInSameTab from '@libs/canOpenURLInSameTab'; +import getPlatform from '@libs/getPlatform'; import Log from '@libs/Log'; +import CONST from '@src/CONST'; import type AsyncOpenURL from './types'; /** @@ -14,6 +15,7 @@ const asyncOpenURL: AsyncOpenURL = (promise, url, shouldSkipCustomSafariLogic, s } const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); + const canOpenURLInSameTab = getPlatform() === CONST.PLATFORM.WEB; if (!isSafari || !!shouldSkipCustomSafariLogic || !!shouldOpenInSameTab) { promise diff --git a/src/libs/canOpenURLInSameTab/index.desktop.ts b/src/libs/canOpenURLInSameTab/index.desktop.ts deleted file mode 100644 index 33136544dba26..0000000000000 --- a/src/libs/canOpenURLInSameTab/index.desktop.ts +++ /dev/null @@ -1 +0,0 @@ -export default false; diff --git a/src/libs/canOpenURLInSameTab/index.ts b/src/libs/canOpenURLInSameTab/index.ts deleted file mode 100644 index 33136544dba26..0000000000000 --- a/src/libs/canOpenURLInSameTab/index.ts +++ /dev/null @@ -1 +0,0 @@ -export default false; diff --git a/src/libs/canOpenURLInSameTab/index.website.ts b/src/libs/canOpenURLInSameTab/index.website.ts deleted file mode 100644 index ff3177babdde0..0000000000000 --- a/src/libs/canOpenURLInSameTab/index.website.ts +++ /dev/null @@ -1 +0,0 @@ -export default true;