11import { isSingleNewDotEntrySelector } from '@selectors/HybridApp' ;
22import { emailSelector } from '@selectors/Session' ;
3- import { useEffect , useRef } from 'react' ;
3+ import { useEffect , useMemo , useRef } from 'react' ;
44import { InteractionManager } from 'react-native' ;
55import { startOnboardingFlow } from '@libs/actions/Welcome/OnboardingFlow' ;
66import getCurrentUrl from '@libs/Navigation/currentUrl' ;
@@ -30,13 +30,14 @@ function useOnboardingFlowRouter() {
3030 } ) ;
3131 const [ currentOnboardingPurposeSelected ] = useOnyx ( ONYXKEYS . ONBOARDING_PURPOSE_SELECTED , { canBeMissing : true } ) ;
3232 const [ currentOnboardingCompanySize ] = useOnyx ( ONYXKEYS . ONBOARDING_COMPANY_SIZE , { canBeMissing : true } ) ;
33- const [ onboardingInitialPath , onboardingInitialPathResult ] = useOnyx ( ONYXKEYS . ONBOARDING_LAST_VISITED_PATH , { canBeMissing : true } ) ;
34- const isOnboardingInitialPathLoading = isLoadingOnyxValue ( onboardingInitialPathResult ) ;
33+ const [ onboardingInitialPath , onboardingInitialPathMetadata ] = useOnyx ( ONYXKEYS . ONBOARDING_LAST_VISITED_PATH , { canBeMissing : true } ) ;
34+ const [ account , accountMetadata ] = useOnyx ( ONYXKEYS . ACCOUNT , { canBeMissing : true } ) ;
35+ const isOnboardingLoading = isLoadingOnyxValue ( onboardingInitialPathMetadata , accountMetadata ) ;
3536
36- const [ account ] = useOnyx ( ONYXKEYS . ACCOUNT , { canBeMissing : true } ) ;
3737 const [ sessionEmail ] = useOnyx ( ONYXKEYS . SESSION , { canBeMissing : true , selector : emailSelector } ) ;
3838 const isLoggingInAsNewSessionUser = isLoggingInAsNewUser ( currentUrl , sessionEmail ) ;
3939 const startedOnboardingFlowRef = useRef ( false ) ;
40+ const started2FAFlowRef = useRef ( false ) ;
4041 const [ tryNewDot , tryNewDotMetadata ] = useOnyx ( ONYXKEYS . NVP_TRY_NEW_DOT , {
4142 selector : tryNewDotOnyxSelector ,
4243 canBeMissing : true ,
@@ -47,6 +48,10 @@ function useOnboardingFlowRouter() {
4748
4849 const [ isSingleNewDotEntry , isSingleNewDotEntryMetadata ] = useOnyx ( ONYXKEYS . HYBRID_APP , { selector : isSingleNewDotEntrySelector , canBeMissing : true } ) ;
4950 const { typeMenuSections} = useSearchTypeMenuSections ( ) ;
51+ const shouldShowRequire2FAPage = useMemo (
52+ ( ) => ( ! ! account ?. needsTwoFactorAuthSetup && ! account ?. requiresTwoFactorAuth ) || ( ! ! account ?. twoFactorAuthSetupInProgress && ! hasCompletedGuidedSetupFlowSelector ( onboardingValues ) ) ,
53+ [ account ?. needsTwoFactorAuthSetup , account ?. requiresTwoFactorAuth , account ?. twoFactorAuthSetupInProgress , onboardingValues ] ,
54+ ) ;
5055
5156 useEffect ( ( ) => {
5257 // This should delay opening the onboarding modal so it does not interfere with the ongoing ReportScreen params changes
@@ -56,7 +61,8 @@ function useOnboardingFlowRouter() {
5661 if ( currentUrl ?. includes ( ROUTES . TRANSITION_BETWEEN_APPS ) && isLoggingInAsNewSessionUser ) {
5762 return ;
5863 }
59- if ( isLoadingApp !== false || isOnboardingInitialPathLoading ) {
64+
65+ if ( isLoadingApp !== false || isOnboardingLoading ) {
6066 return ;
6167 }
6268
@@ -73,6 +79,16 @@ function useOnboardingFlowRouter() {
7379 return ;
7480 }
7581
82+ if ( shouldShowRequire2FAPage ) {
83+ if ( started2FAFlowRef . current ) {
84+ startedOnboardingFlowRef . current = false ;
85+ return ;
86+ }
87+ started2FAFlowRef . current = true ;
88+ Navigation . navigate ( ROUTES . REQUIRE_TWO_FACTOR_AUTH ) ;
89+ return ;
90+ }
91+
7692 if ( hasBeenAddedToNudgeMigration && ! isProductTrainingElementDismissed ( 'migratedUserWelcomeModal' , dismissedProductTraining ) ) {
7793 const navigationState = navigationRef . getRootState ( ) ;
7894 const lastRoute = navigationState . routes . at ( - 1 ) ;
@@ -149,11 +165,17 @@ function useOnboardingFlowRouter() {
149165 currentOnboardingCompanySize ,
150166 currentOnboardingPurposeSelected ,
151167 onboardingInitialPath ,
152- isOnboardingInitialPathLoading ,
168+ isOnboardingLoading ,
153169 typeMenuSections ,
170+ shouldShowRequire2FAPage ,
154171 ] ) ;
155172
156- return { isOnboardingCompleted : hasCompletedGuidedSetupFlowSelector ( onboardingValues ) , isHybridAppOnboardingCompleted} ;
173+ return {
174+ isOnboardingCompleted : hasCompletedGuidedSetupFlowSelector ( onboardingValues ) ,
175+ isHybridAppOnboardingCompleted,
176+ shouldShowRequire2FAPage,
177+ isOnboardingLoading : ! ! onboardingValues ?. isLoading ,
178+ } ;
157179}
158180
159181export default useOnboardingFlowRouter ;
0 commit comments