-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make onboarding continue from last visited onboarding page #48137
Changes from 4 commits
e6e271d
b862e13
514d56f
d425363
5eed809
16862d2
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,127 @@ | ||
import {findFocusedRoute, getStateFromPath} from '@react-navigation/native'; | ||
import type {NavigationState, PartialState} from '@react-navigation/native'; | ||
import Onyx from 'react-native-onyx'; | ||
import linkingConfig from '@libs/Navigation/linkingConfig'; | ||
import getAdaptedStateFromPath from '@libs/Navigation/linkingConfig/getAdaptedStateFromPath'; | ||
import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; | ||
import type {NavigationPartialRoute, RootStackParamList} from '@libs/Navigation/types'; | ||
import CONST from '@src/CONST'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import SCREENS from '@src/SCREENS'; | ||
|
||
let selectedPurpose: string | undefined = ''; | ||
Onyx.connect({ | ||
key: ONYXKEYS.ONBOARDING_PURPOSE_SELECTED, | ||
callback: (value) => { | ||
selectedPurpose = value; | ||
}, | ||
}); | ||
|
||
let onboardingInitialPath = ''; | ||
const onboardingLastVisitedPathConnection = Onyx.connect({ | ||
key: ONYXKEYS.ONBOARDING_LAST_VISITED_PATH, | ||
callback: (value) => { | ||
if (value === undefined) { | ||
return; | ||
} | ||
onboardingInitialPath = value; | ||
Onyx.disconnect(onboardingLastVisitedPathConnection); | ||
}, | ||
}); | ||
|
||
/** | ||
* Build the correct stack order for `onboardingModalNavigator`, | ||
* based on onboarding data (currently from the selected purpose). | ||
* The correct stack order will ensure that navigation and | ||
* the `goBack` navigatoin work properly. | ||
*/ | ||
function adaptOnboardingRouteState() { | ||
const currentRoute: NavigationPartialRoute | undefined = navigationRef.getCurrentRoute(); | ||
if (!currentRoute || currentRoute?.name === SCREENS.ONBOARDING.PURPOSE) { | ||
return; | ||
} | ||
|
||
const rootState = navigationRef.getRootState(); | ||
const adaptedState = rootState; | ||
const lastRouteIndex = (adaptedState?.routes?.length ?? 0) - 1; | ||
const onBoardingModalNavigatorState = adaptedState?.routes[lastRouteIndex]?.state; | ||
if (!onBoardingModalNavigatorState || onBoardingModalNavigatorState?.routes?.length > 1) { | ||
return; | ||
} | ||
|
||
let adaptedOnboardingModalNavigatorState = {} as Readonly<PartialState<NavigationState>>; | ||
if (currentRoute?.name === SCREENS.ONBOARDING.PERSONAL_DETAILS && selectedPurpose === CONST.ONBOARDING_CHOICES.MANAGE_TEAM) { | ||
adaptedOnboardingModalNavigatorState = { | ||
index: 2, | ||
routes: [ | ||
{ | ||
name: SCREENS.ONBOARDING.PURPOSE, | ||
params: currentRoute?.params, | ||
}, | ||
{ | ||
name: SCREENS.ONBOARDING.WORK, | ||
params: currentRoute?.params, | ||
path: '/onboarding/works', | ||
}, | ||
{...currentRoute}, | ||
], | ||
} as Readonly<PartialState<NavigationState>>; | ||
} else { | ||
adaptedOnboardingModalNavigatorState = { | ||
index: 1, | ||
routes: [ | ||
{ | ||
name: SCREENS.ONBOARDING.PURPOSE, | ||
params: currentRoute?.params, | ||
}, | ||
{...currentRoute}, | ||
], | ||
} as Readonly<PartialState<NavigationState>>; | ||
} | ||
|
||
adaptedState.routes[lastRouteIndex].state = adaptedOnboardingModalNavigatorState; | ||
navigationRef.resetRoot(adaptedState); | ||
} | ||
|
||
/** | ||
* Start a new onboarding flow or continue from the last visited onboarding page. | ||
*/ | ||
function startOnboardingFlow() { | ||
const currentRoute = navigationRef.getCurrentRoute(); | ||
const {adaptedState} = getAdaptedStateFromPath(getOnboardingInitialPath(), linkingConfig.config, false); | ||
const focusedRoute = findFocusedRoute(adaptedState as PartialState<NavigationState<RootStackParamList>>); | ||
if (focusedRoute?.name === currentRoute?.name) { | ||
return; | ||
} | ||
navigationRef.resetRoot(adaptedState); | ||
} | ||
|
||
function getOnboardingInitialPath(): string { | ||
const state = getStateFromPath(onboardingInitialPath, linkingConfig.config); | ||
if (state?.routes?.at(-1)?.name !== NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR) { | ||
return `/${ROUTES.ONBOARDING_ROOT.route}`; | ||
} | ||
|
||
return onboardingInitialPath; | ||
} | ||
|
||
function clearInitialPath() { | ||
onboardingInitialPath = ''; | ||
} | ||
|
||
/** | ||
* Onboarding flow: Go back to the previous page. | ||
* Since there is no `initialRoute` for `onBoardingModalNavigator`, | ||
* firstly, adjust the current onboarding modal navigator to establish the correct stack order. | ||
* Then, navigate to the previous onboarding page using the usual `goBack` function. | ||
*/ | ||
function goBack() { | ||
adaptOnboardingRouteState(); | ||
Navigation.isNavigationReady().then(() => { | ||
Navigation.goBack(); | ||
Comment on lines
+119
to
+122
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. Coming from the BZ of #50177 |
||
}); | ||
} | ||
|
||
export {getOnboardingInitialPath, startOnboardingFlow, clearInitialPath, goBack}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import Navigation from '@libs/Navigation/Navigation'; | |
import * as ValidationUtils from '@libs/ValidationUtils'; | ||
import * as Policy from '@userActions/Policy/Policy'; | ||
import * as Welcome from '@userActions/Welcome'; | ||
import * as OnboardingFlow from '@userActions/Welcome/OnboardingFlow'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
|
@@ -81,7 +82,7 @@ function BaseOnboardingWork({shouldUseNativeStyles, onboardingPurposeSelected, o | |
<HeaderWithBackButton | ||
shouldShowBackButton | ||
progressBarPercentage={OPEN_WORK_PAGE_PURPOSES.includes(onboardingPurposeSelected ?? '') ? 50 : 75} | ||
onBackButtonPress={Navigation.goBack} | ||
onBackButtonPress={OnboardingFlow.goBack} | ||
/> | ||
<FormProvider | ||
style={[styles.flexGrow1, isMediumOrLargerScreenWidth && styles.mt5, isMediumOrLargerScreenWidth ? styles.mh8 : styles.mh5]} | ||
|
@@ -111,7 +112,6 @@ function BaseOnboardingWork({shouldUseNativeStyles, onboardingPurposeSelected, o | |
shouldSaveDraft | ||
maxLength={CONST.TITLE_CHARACTER_LIMIT} | ||
spellCheck={false} | ||
autoFocus | ||
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. I am removing this line because the autofocus is handled by |
||
/> | ||
</View> | ||
</FormProvider> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path is
work
singular. I think it's better we usegetRoute()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mollfpr actually we don’t need the path field, I forgot to remove it.