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

fix: Deeplink [Desktop] - Copied conversation URL opens another chat #47766

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Expensify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ function Expensify({
<DeeplinkWrapper
isAuthenticated={isAuthenticated}
autoAuthState={autoAuthState}
initialUrl={initialUrl ?? ''}
>
{shouldInit && (
<>
Expand Down
11 changes: 6 additions & 5 deletions src/components/DeeplinkWrapper/index.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand All @@ -26,11 +27,11 @@ function promptToOpenInDesktopApp() {
// Match any magic link (/v/<account id>/<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<string | undefined>();
const [hasShownPrompt, setHasShownPrompt] = useState(false);
const removeListener = useRef<() => void>();
Expand Down Expand Up @@ -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
Expand All @@ -93,7 +94,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState}: DeeplinkWra
promptToOpenInDesktopApp();
setHasShownPrompt(true);
}
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState]);
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl]);

return children;
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/DeeplinkWrapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type DeeplinkWrapperProps = ChildrenProps & {

/** The auto authentication status */
autoAuthState?: string;

initialUrl?: string;
};

export default DeeplinkWrapperProps;
4 changes: 2 additions & 2 deletions src/libs/Browser/index.website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
4 changes: 2 additions & 2 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -475,7 +475,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true) {
return;
}

Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail);
Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, initialRoute);
});
}

Expand Down
Loading