-
Notifications
You must be signed in to change notification settings - Fork 4k
Add a one-click deeplink that signs the user in and creates a Submit workspace #98887
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
base: main
Are you sure you want to change the base?
Changes from all commits
72e106e
1bf9098
06e18f0
f843d86
8b21e71
3215c16
7fad41a
8bb0054
22db33d
f1887b4
fca471c
120ac22
a6e5615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import {useInitialURLState} from '@components/InitialURLContextProvider'; | ||
|
|
||
| import getOnboardingIntentFromUrl from '@libs/getOnboardingIntentFromUrl'; | ||
| import getCurrentUrl from '@libs/Navigation/currentUrl'; | ||
|
|
||
| import type {OnboardingIntent} from '@src/CONST'; | ||
|
|
||
| import {useState} from 'react'; | ||
|
|
||
| /** | ||
| * Resolves the onboarding outcome requested by the deeplink this session was opened with, e.g. `onboarding?intent=submit`. | ||
| * | ||
| * The URL is latched at mount because the app rewrites it as soon as it navigates. The initial URL covers native, | ||
| * where the browser URL is empty, and links opened while the app is already running. | ||
| */ | ||
| function useOnboardingDeeplinkIntent(): OnboardingIntent | undefined { | ||
| const {initialURL} = useInitialURLState(); | ||
| const [urlAtMount] = useState(getCurrentUrl); | ||
|
|
||
| return getOnboardingIntentFromUrl(urlAtMount) ?? getOnboardingIntentFromUrl(initialURL); | ||
|
MelvinBot marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export default useOnboardingDeeplinkIntent; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import useAutoCreateSubmitWorkspace from '@hooks/useAutoCreateSubmitWorkspace'; | ||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
|
|
||
| import {setSubmitMigrationModalShown} from '@userActions/User'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
|
||
| import {hasCompletedGuidedSetupFlowSelector} from '@selectors/Onboarding'; | ||
| import {isSupportalSessionSelector} from '@selectors/Session'; | ||
| import {useEffect} from 'react'; | ||
|
|
||
| // Module scope rather than a ref so it survives this component remounting. The deeplink is read from the initial URL, | ||
| // which the provider above the navigator keeps for the life of the process, so signing out and into another account | ||
| // remounts this component with the same intent still readable and would create a workspace for that second account. | ||
| let hasAppliedIntent = false; | ||
|
|
||
| /** | ||
| * Creates the Submit workspace requested by an `intent=submit` onboarding deeplink. | ||
| * | ||
| * The link only goes to existing users, so it acts solely on recipients who have finished guided setup. Anyone who | ||
| * still has onboarding ahead of them is left to it, since that flow already offers the Submit outcome. | ||
| */ | ||
| function ApplySubmitOnboardingIntent() { | ||
| const {firstName, lastName} = useCurrentUserPersonalDetails(); | ||
| const autoCreateSubmitWorkspace = useAutoCreateSubmitWorkspace(); | ||
|
|
||
| // Waiting on HAS_LOADED_APP keeps the eligibility checks inside useAutoCreateSubmitWorkspace from running | ||
| // against a half-populated store, where they would miss an existing workspace and create a duplicate. | ||
| const [hasLoadedApp] = useOnyx(ONYXKEYS.HAS_LOADED_APP); | ||
| const [isOnboardingCompleted] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasCompletedGuidedSetupFlowSelector}); | ||
| const [isSupportalSession] = useOnyx(ONYXKEYS.SESSION, {selector: isSupportalSessionSelector}); | ||
|
|
||
| useEffect(() => { | ||
| if (hasAppliedIntent || !hasLoadedApp || isOnboardingCompleted === undefined || isSupportalSession) { | ||
| return; | ||
|
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a warm onboarding link is opened during a supportal session, this condition returns without setting Useful? React with 👍 / 👎. |
||
| } | ||
| hasAppliedIntent = true; | ||
|
|
||
| if (!isOnboardingCompleted) { | ||
| return; | ||
| } | ||
|
|
||
| // The deeplink delivers the same outcome as the Submit plan welcome modal, so keep that modal from opening too. | ||
| setSubmitMigrationModalShown(); | ||
|
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For recipients who also satisfy Useful? React with 👍 / 👎. |
||
|
|
||
| // `false` skips CompleteGuidedSetup, which is already done. The hook navigates to the user's existing Submit | ||
| // workspace rather than creating a second one, which is what makes repeat clicks idempotent. | ||
| autoCreateSubmitWorkspace(firstName ?? '', lastName ?? '', false); | ||
| }, [autoCreateSubmitWorkspace, firstName, hasLoadedApp, isOnboardingCompleted, isSupportalSession, lastName]); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export default ApplySubmitOnboardingIntent; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import useOnboardingDeeplinkIntent from '@hooks/useOnboardingDeeplinkIntent'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import ApplySubmitOnboardingIntent from './ApplySubmitOnboardingIntent'; | ||
|
|
||
| /** | ||
| * Recognizes the `intent=submit` onboarding deeplink and hands off to the component that acts on it. | ||
| */ | ||
| function SubmitIntentDeeplinkHandler() { | ||
| const onboardingDeeplinkIntent = useOnboardingDeeplinkIntent(); | ||
|
|
||
| if (onboardingDeeplinkIntent !== CONST.ONBOARDING_INTENTS.SUBMIT) { | ||
| return null; | ||
| } | ||
|
|
||
| return <ApplySubmitOnboardingIntent />; | ||
| } | ||
|
|
||
| export default SubmitIntentDeeplinkHandler; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * Reads the `intent` param of the onboarding deeplink (e.g. `onboarding?intent=submit`). | ||
| * | ||
| * It arrives either directly, or nested in the `exitTo` of a transition or magic link. Nesting it in `exitTo` is what | ||
| * carries the intent across the logged-out to logged-in transition, since the deeplink outlives the sign-in itself. | ||
| */ | ||
| import type {OnboardingIntent} from '@src/CONST'; | ||
| import CONST from '@src/CONST'; | ||
| import ROUTES from '@src/ROUTES'; | ||
|
|
||
| import {getRouteFromLink} from './ReportUtils'; | ||
| import {getSearchParamFromPath} from './Url'; | ||
|
|
||
| const ONBOARDING_INTENT_VALUES = new Set<string>(Object.values(CONST.ONBOARDING_INTENTS)); | ||
|
|
||
| function isOnboardingIntent(value: string | null): value is OnboardingIntent { | ||
| return !!value && ONBOARDING_INTENT_VALUES.has(value); | ||
| } | ||
|
|
||
| function getOnboardingIntentFromUrl(url: string | null | undefined): OnboardingIntent | undefined { | ||
| if (!url) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // getRouteFromLink strips whichever linking-config prefix matched, so web URLs, the desktop `app://-/` origin and | ||
| // the native scheme all reduce to the same route. It leaves the leading slash on in-app paths. | ||
| const pathWithQuery = getRouteFromLink(url).replace(/^\/+/, ''); | ||
| const onboardingPathWithQuery = pathWithQuery.startsWith(ROUTES.ONBOARDING_ROOT.route) ? pathWithQuery : getSearchParamFromPath(pathWithQuery, 'exitTo'); | ||
|
|
||
| if (!onboardingPathWithQuery?.startsWith(ROUTES.ONBOARDING_ROOT.route)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const intent = getSearchParamFromPath(onboardingPathWithQuery, 'intent'); | ||
| return isOnboardingIntent(intent) ? intent : undefined; | ||
| } | ||
|
|
||
| export default getOnboardingIntentFromUrl; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import getOnboardingIntentFromUrl from '@libs/getOnboardingIntentFromUrl'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| describe('getOnboardingIntentFromUrl', () => { | ||
| const encodedOnboardingRoute = encodeURIComponent('onboarding?intent=submit'); | ||
|
|
||
| it('reads the intent from a direct onboarding link', () => { | ||
| expect(getOnboardingIntentFromUrl('https://new.expensify.com/onboarding?intent=submit')).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from a custom scheme link, where the route sits where a host would', () => { | ||
| expect(getOnboardingIntentFromUrl('new-expensify://onboarding?intent=submit')).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from a custom scheme link with a placeholder host', () => { | ||
| expect(getOnboardingIntentFromUrl('app://-/onboarding?intent=submit')).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from the exitTo of a custom scheme magic link', () => { | ||
| expect(getOnboardingIntentFromUrl(`new-expensify://v/12345/678910?exitTo=${encodedOnboardingRoute}`)).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from a link served on a port, as the dev server does', () => { | ||
| expect(getOnboardingIntentFromUrl('https://dev.new.expensify.com:8082/onboarding?intent=submit')).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from an in-app path without an origin', () => { | ||
| expect(getOnboardingIntentFromUrl('/onboarding?intent=submit')).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| // HybridApp hands NewDot the deeplink as a bare path rather than a full URL, which is the shape the emailed | ||
| // magic link arrives in on mobile. | ||
| it('reads the intent from the exitTo of a magic link passed as a bare path', () => { | ||
| expect(getOnboardingIntentFromUrl(`v/12345/678910?exitTo=${encodedOnboardingRoute}`)).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from the exitTo of an OldDot transition link', () => { | ||
| const url = `https://new.expensify.com/transition?email=${encodeURIComponent('me@example.com')}&shortLivedAuthToken=abc123&exitTo=${encodedOnboardingRoute}`; | ||
|
|
||
| expect(getOnboardingIntentFromUrl(url)).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from the exitTo of a magic link', () => { | ||
| const url = `https://new.expensify.com/v/12345/678910?exitTo=${encodedOnboardingRoute}`; | ||
|
|
||
| expect(getOnboardingIntentFromUrl(url)).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it('reads the intent from an unencoded exitTo, which OldDot mobile does not encode', () => { | ||
| const url = 'https://new.expensify.com/transition?shortLivedAuthToken=abc123&exitTo=onboarding?intent=submit'; | ||
|
|
||
| expect(getOnboardingIntentFromUrl(url)).toBe(CONST.ONBOARDING_INTENTS.SUBMIT); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['no url', undefined], | ||
| ['an empty url', ''], | ||
| ['an onboarding link without an intent', 'https://new.expensify.com/onboarding'], | ||
| ['an unknown intent value', 'https://new.expensify.com/onboarding?intent=notARealIntent'], | ||
| ['an intent on a non-onboarding route', 'https://new.expensify.com/settings/profile?intent=submit'], | ||
| ['an intent on a non-onboarding exitTo', `https://new.expensify.com/transition?exitTo=${encodeURIComponent('workspace/new?intent=submit')}`], | ||
| ])('returns undefined for %s', (_description, url) => { | ||
| expect(getOnboardingIntentFromUrl(url)).toBeUndefined(); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.