From a2d3c8b0010bc9e73d9de8365bea6abc33232c0e Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 21 Aug 2024 10:44:39 +0700 Subject: [PATCH 1/2] fix: Deeplink [Desktop] - Copied conversation URL opens another chat --- src/Expensify.tsx | 1 + src/components/DeeplinkWrapper/index.website.tsx | 11 ++++++----- src/components/DeeplinkWrapper/types.ts | 2 ++ src/libs/Browser/index.website.ts | 4 ++-- src/libs/actions/App.ts | 4 ++-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Expensify.tsx b/src/Expensify.tsx index 620243440384..8a2ef4a2b2f4 100644 --- a/src/Expensify.tsx +++ b/src/Expensify.tsx @@ -256,6 +256,7 @@ function Expensify({ {shouldInit && ( <> diff --git a/src/components/DeeplinkWrapper/index.website.tsx b/src/components/DeeplinkWrapper/index.website.tsx index 649e66ccefa8..b395eb12c5fe 100644 --- a/src/components/DeeplinkWrapper/index.website.tsx +++ b/src/components/DeeplinkWrapper/index.website.tsx @@ -5,6 +5,7 @@ import Navigation from '@libs/Navigation/Navigation'; import navigationRef from '@libs/Navigation/navigationRef'; import shouldPreventDeeplinkPrompt from '@libs/Navigation/shouldPreventDeeplinkPrompt'; import * as App from '@userActions/App'; +import * as Link from '@userActions/Link'; import * as Session from '@userActions/Session'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; @@ -15,7 +16,7 @@ function isMacOSWeb(): boolean { return !Browser.isMobile() && typeof navigator === 'object' && typeof navigator.userAgent === 'string' && /Mac/i.test(navigator.userAgent) && !/Electron/i.test(navigator.userAgent); } -function promptToOpenInDesktopApp() { +function promptToOpenInDesktopApp(initialUrl = '') { // If the current url path is /transition..., meaning it was opened from oldDot, during this transition period: // 1. The user session may not exist, because sign-in has not been completed yet. // 2. There may be non-idempotent operations (e.g. create a new workspace), which obviously should not be executed again in the desktop app. @@ -26,11 +27,11 @@ function promptToOpenInDesktopApp() { // Match any magic link (/v//<6 digit code>) const isMagicLink = CONST.REGEX.ROUTES.VALIDATE_LOGIN.test(window.location.pathname); - App.beginDeepLinkRedirect(!isMagicLink); + App.beginDeepLinkRedirect(!isMagicLink, Link.getInternalNewExpensifyPath(initialUrl)); } } -function DeeplinkWrapper({children, isAuthenticated, autoAuthState}: DeeplinkWrapperProps) { +function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: DeeplinkWrapperProps) { const [currentScreen, setCurrentScreen] = useState(); const [hasShownPrompt, setHasShownPrompt] = useState(false); const removeListener = useRef<() => void>(); @@ -77,7 +78,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState}: DeeplinkWra // Otherwise, we want to wait until the navigation state is set up // and we know the user is on a screen that supports deeplinks. if (isAuthenticated) { - promptToOpenInDesktopApp(); + promptToOpenInDesktopApp(initialUrl); setHasShownPrompt(true); } else { // Navigation state is not set up yet, we're unsure if we should show the deep link prompt or not @@ -93,7 +94,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState}: DeeplinkWra promptToOpenInDesktopApp(); setHasShownPrompt(true); } - }, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState]); + }, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl]); return children; } diff --git a/src/components/DeeplinkWrapper/types.ts b/src/components/DeeplinkWrapper/types.ts index db61e5b01c24..23e096d6a093 100644 --- a/src/components/DeeplinkWrapper/types.ts +++ b/src/components/DeeplinkWrapper/types.ts @@ -6,6 +6,8 @@ type DeeplinkWrapperProps = ChildrenProps & { /** The auto authentication status */ autoAuthState?: string; + + initialUrl?: string; }; export default DeeplinkWrapperProps; diff --git a/src/libs/Browser/index.website.ts b/src/libs/Browser/index.website.ts index b89190dc7f78..2007f2c6cbc0 100644 --- a/src/libs/Browser/index.website.ts +++ b/src/libs/Browser/index.website.ts @@ -79,12 +79,12 @@ const isSafari: IsSafari = () => getBrowser() === 'safari' || isMobileSafari(); /** * The session information needs to be passed to the Desktop app, and the only way to do that is by using query params. There is no other way to transfer the data. */ -const openRouteInDesktopApp: OpenRouteInDesktopApp = (shortLivedAuthToken = '', email = '') => { +const openRouteInDesktopApp: OpenRouteInDesktopApp = (shortLivedAuthToken = '', email = '', initialRoute = '') => { const params = new URLSearchParams(); // If the user is opening the desktop app through a third party signin flow, we need to manually add the exitTo param // so that the desktop app redirects to the correct home route after signin is complete. const openingFromDesktopRedirect = window.location.pathname === `/${ROUTES.DESKTOP_SIGN_IN_REDIRECT}`; - params.set('exitTo', `${openingFromDesktopRedirect ? '/r' : window.location.pathname}${window.location.search}${window.location.hash}`); + params.set('exitTo', `${openingFromDesktopRedirect ? '/r' : initialRoute || window.location.pathname}${window.location.search}${window.location.hash}`); if (email && shortLivedAuthToken) { params.set('email', email); params.set('shortLivedAuthToken', shortLivedAuthToken); diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index 7be767d73f48..532d93d2b264 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -449,7 +449,7 @@ function redirectThirdPartyDesktopSignIn() { /** * @param shouldAuthenticateWithCurrentAccount Optional, indicates whether default authentication method (shortLivedAuthToken) should be used */ -function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true) { +function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, initialRoute?: string) { // There's no support for anonymous users on desktop if (Session.isAnonymousUser()) { return; @@ -458,7 +458,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true) { // If the route that is being handled is a magic link, email and shortLivedAuthToken should not be attached to the url // to prevent signing into the wrong account if (!currentUserAccountID || !shouldAuthenticateWithCurrentAccount) { - Browser.openRouteInDesktopApp(); + Browser.openRouteInDesktopApp(initialRoute); return; } From 575033065f40877b8e6f97095b6bc0c4b89aa479 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 21 Aug 2024 15:26:11 +0700 Subject: [PATCH 2/2] remove useless code --- src/libs/Browser/types.ts | 2 +- src/libs/actions/App.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/Browser/types.ts b/src/libs/Browser/types.ts index cb242d3729aa..ff0de91e7b78 100644 --- a/src/libs/Browser/types.ts +++ b/src/libs/Browser/types.ts @@ -12,6 +12,6 @@ type IsChromeIOS = () => boolean; type IsSafari = () => boolean; -type OpenRouteInDesktopApp = (shortLivedAuthToken?: string, email?: string) => void; +type OpenRouteInDesktopApp = (shortLivedAuthToken?: string, email?: string, initialRoute?: string) => void; export type {GetBrowser, IsMobile, IsMobileSafari, IsMobileChrome, IsMobileWebKit, IsSafari, IsChromeIOS, OpenRouteInDesktopApp}; diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index 532d93d2b264..0c1a157f76f3 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -458,7 +458,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, init // If the route that is being handled is a magic link, email and shortLivedAuthToken should not be attached to the url // to prevent signing into the wrong account if (!currentUserAccountID || !shouldAuthenticateWithCurrentAccount) { - Browser.openRouteInDesktopApp(initialRoute); + Browser.openRouteInDesktopApp(); return; } @@ -475,7 +475,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, init return; } - Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail); + Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, initialRoute); }); }