-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48137 from tsa321/makeOnboardContinue
Make onboarding continue from last visited onboarding page
- Loading branch information
Showing
11 changed files
with
169 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
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, | ||
}, | ||
{...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(); | ||
}); | ||
} | ||
|
||
export {getOnboardingInitialPath, startOnboardingFlow, clearInitialPath, goBack}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters