Skip to content

fix: fix problem with redirecting to desktop #60487

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

Merged
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
18 changes: 13 additions & 5 deletions src/components/DeeplinkWrapper/index.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function isMacOSWeb(): boolean {
return !isMobile() && typeof navigator === 'object' && typeof navigator.userAgent === 'string' && /Mac/i.test(navigator.userAgent) && !/Electron/i.test(navigator.userAgent);
}

function promptToOpenInDesktopApp(initialUrl = '') {
function promptToOpenInDesktopApp(currentUserAccountID?: number, 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 @@ -35,16 +35,24 @@ function promptToOpenInDesktopApp(initialUrl = '') {
} else {
// Match any magic link (/v/<account id>/<6 digit code>)
const isMagicLink = CONST.REGEX.ROUTES.VALIDATE_LOGIN.test(window.location.pathname);
const shouldAuthenticateWithCurrentAccount = !isMagicLink || (isMagicLink && !!currentUserAccountID && window.location.pathname.includes(currentUserAccountID.toString()));

beginDeepLinkRedirect(!isMagicLink, getInternalNewExpensifyPath(initialUrl));
beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount, isMagicLink, getInternalNewExpensifyPath(initialUrl));
}
}

function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}: DeeplinkWrapperProps) {
const [currentScreen, setCurrentScreen] = useState<string | undefined>();
const [hasShownPrompt, setHasShownPrompt] = useState(false);
const removeListener = useRef<() => void>();
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => !!account?.delegatedAccess?.delegate});
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {
selector: (account) => !!account?.delegatedAccess?.delegate,
canBeMissing: true,
});
const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {
selector: (session) => session?.accountID,
canBeMissing: true,
});
const isActingAsDelegateRef = useRef(isActingAsDelegate);
const delegatorEmailRef = useRef(getSearchParamFromUrl(getCurrentUrl(), 'delegatorEmail'));

Expand Down Expand Up @@ -95,7 +103,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}:
// 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(initialUrl);
promptToOpenInDesktopApp(currentUserAccountID, 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 @@ -111,7 +119,7 @@ function DeeplinkWrapper({children, isAuthenticated, autoAuthState, initialUrl}:
promptToOpenInDesktopApp();
setHasShownPrompt(true);
}
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl]);
}, [currentScreen, hasShownPrompt, isAuthenticated, autoAuthState, initialUrl, currentUserAccountID]);

return children;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ function redirectThirdPartyDesktopSignIn() {
/**
* @param shouldAuthenticateWithCurrentAccount Optional, indicates whether default authentication method (shortLivedAuthToken) should be used
*/
function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, initialRoute?: string) {
function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, isMagicLink?: boolean, initialRoute?: string) {
// There's no support for anonymous users on desktop
if (isAnonymousUser()) {
return;
Expand All @@ -560,7 +560,7 @@ function beginDeepLinkRedirect(shouldAuthenticateWithCurrentAccount = true, init
return;
}

Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, initialRoute);
Browser.openRouteInDesktopApp(response.shortLivedAuthToken, currentUserEmail, isMagicLink ? '/r' : initialRoute);
});
}

Expand Down