Skip to content
11 changes: 10 additions & 1 deletion src/hooks/useOnboardingStepCounter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import {getOnboardingStepCounter} from '@libs/getOnboardingStepCounter';
import type {OnboardingScreen, OnboardingStepResult} from '@libs/getOnboardingStepCounter';
import {getVisibleJoinablePoliciesCount} from '@libs/OnboardingUtils';
import {expensifyLoginsSelector, isCurrentUserValidated} from '@libs/UserUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

import useOnyx from './useOnyx';
import usePermissions from './usePermissions';

function useOnboardingStepCounter(page: OnboardingScreen): OnboardingStepResult | undefined {
const [onboarding] = useOnyx(ONYXKEYS.NVP_ONBOARDING);
const [purposeSelected] = useOnyx(ONYXKEYS.ONBOARDING_PURPOSE_SELECTED);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [joinablePolicies] = useOnyx(ONYXKEYS.JOINABLE_POLICIES);
const [loginList] = useOnyx(ONYXKEYS.LOGINS, {selector: expensifyLoginsSelector});
const [session] = useOnyx(ONYXKEYS.SESSION);
const {isBetaEnabled} = usePermissions();

return getOnboardingStepCounter(page, {
signupQualifier: onboarding?.signupQualifier,
isFromPublicDomain: account?.isFromPublicDomain,
hasAccessibleDomainPolicies: account?.hasAccessibleDomainPolicies,
purposeSelected: purposeSelected ?? undefined,
isMergeAccountStepSkipped: onboarding?.isMergeAccountStepSkipped,
isAccountValidated: !!account?.validated,
isAccountValidated: isCurrentUserValidated(loginList, session?.email),
hasJoinablePolicies: getVisibleJoinablePoliciesCount(joinablePolicies, isBetaEnabled(CONST.BETAS.SUBMIT_2026)) > 0,
});
}

Expand Down
15 changes: 12 additions & 3 deletions src/libs/OnboardingUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CONST from '@src/CONST';
import type {OnboardingInvite} from '@src/CONST';
import type {IntroSelected, OnboardingPurpose} from '@src/types/onyx';
import type {IntroSelected, JoinablePolicies, OnboardingPurpose} from '@src/types/onyx';

import type {OnyxEntry} from 'react-native-onyx';

Expand All @@ -21,6 +21,16 @@ function isTrackOnboardingChoice(choice: OnyxEntry<OnboardingPurpose>): choice i
return choice === CONST.ONBOARDING_CHOICES.TRACK_BUSINESS || choice === CONST.ONBOARDING_CHOICES.TRACK_PERSONAL || choice === CONST.ONBOARDING_CHOICES.PERSONAL_SPEND;
}

/**
* Counts the joinable policies that are actually surfaced during onboarding. SUBMIT-type policies are hidden
* unless the SUBMIT_2026 beta is enabled, mirroring the filter the Workspaces and PrivateDomain screens use to
* render/skip. Centralizing it keeps the step counter and EMPLOYEES back button in sync with what the user sees,
* so a user whose only joinable policies are hidden SUBMIT policies never gets a phantom WORKSPACES step.
*/
function getVisibleJoinablePoliciesCount(joinablePolicies: OnyxEntry<JoinablePolicies>, canUseSubmit2026: boolean): number {
return Object.values(joinablePolicies ?? {}).filter((policy) => policy.policyType !== CONST.POLICY.TYPE.SUBMIT || canUseSubmit2026).length;
}

function isSupportedInviteOnboardingChoice(choice: OnyxEntry<OnboardingPurpose>): choice is SupportedInviteOnboardingChoice {
return choice === CONST.ONBOARDING_CHOICES.ADMIN || choice === CONST.ONBOARDING_CHOICES.SUBMIT || choice === CONST.ONBOARDING_CHOICES.CHAT_SPLIT;
}
Expand All @@ -38,6 +48,5 @@ function isSupportedPendingInviteOnboarding(introSelected: OnyxEntry<IntroSelect

return isSupportedInviteOnboardingChoice(introSelected.choice) && !isInviteIOUorInvoice;
}

export default isTrackOnboardingChoice;
export {isSupportedInviteOnboardingChoice, isSupportedPendingInviteOnboarding};
export {getVisibleJoinablePoliciesCount, isSupportedInviteOnboardingChoice, isSupportedPendingInviteOnboarding};
11 changes: 10 additions & 1 deletion src/libs/getOnboardingStepCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type OnboardingFlowContext = {
purposeSelected?: ValueOf<typeof CONST.ONBOARDING_CHOICES>;
isMergeAccountStepSkipped?: boolean;
isAccountValidated?: boolean;
hasJoinablePolicies?: boolean;
};

type OnboardingStepResult = {
Expand Down Expand Up @@ -86,7 +87,15 @@ function getDomainPrefix(context: OnboardingFlowContext): OnboardingScreen[] {
return [ONBOARDING.WORK_EMAIL, ONBOARDING.WORK_EMAIL_VALIDATION];
}
if (context.hasAccessibleDomainPolicies) {
return [ONBOARDING.PERSONAL_DETAILS, ONBOARDING.PRIVATE_DOMAIN, ONBOARDING.WORKSPACES];
// A private-domain user reaches exactly one of PRIVATE_DOMAIN / WORKSPACES before EMPLOYEES, and only when it
// is actually shown. Unvalidated users see PRIVATE_DOMAIN and skip straight to EMPLOYEES on the magic-code
// screen. Validated users skip PRIVATE_DOMAIN and see WORKSPACES only when joinable workspaces exist; with
// none, that screen auto-skips too. Keeping a never-traversed screen in the flow makes the EMPLOYEES back
// button resolve to a blank, never-visited screen and inflates the step counter.
if (!context.isAccountValidated) {
return [ONBOARDING.PERSONAL_DETAILS, ONBOARDING.PRIVATE_DOMAIN];
}
Comment on lines +95 to +97

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the same validation signal as the onboarding branch

This branch treats private-domain users as unvalidated unless account.validated is true, but the screen that sends them to the previous step uses isCurrentUserValidated(loginList, session?.email) to choose WORKSPACES vs PRIVATE_DOMAIN (BaseOnboardingPersonalDetails). When the current login has a validatedDate before account.validated is populated, a user can legitimately go Personal details → Workspaces → Employees, but the Employees back route is computed as PRIVATE_DOMAIN, recreating the never-visited/blank modal path this change is trying to avoid. The step counter/back-route context needs to use the same current-login validation signal as the navigation branch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to verify this comment to determine whether it's a legitimate bug, but I can't reproduce any issue. I'm also not sure how validatedDate would be set in order to test this scenario.

@dilshodmackbook-sketch Are you seeing any issue when testing this flow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayeshmangwani you're right, I couldn't reproduce it either. This flow only runs for the signed-in primary login, and its validatedDate and account.validated reflect the same validation state, so I couldn't find a path where the two diverge before this screen renders — the back route stays in sync with navigation.

If we want to be defensive about it, I can point the step-counter context at the same isCurrentUserValidated helper the navigation already uses, so the two can never drift later. It's a one-line change — happy to add it if you think it's worth it, otherwise fine to leave as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went ahead and added the defensive change we discussed — the step-counter/back-route context now derives validation from isCurrentUserValidated(loginList, session?.email), the same signal BaseOnboardingPersonalDetails navigates on, instead of account.validated. Even though we couldn't reproduce a divergence, this guarantees the back route and navigation can't drift apart later. It's in b024a340b76, touching only the two context builders (useOnboardingStepCounter and BaseOnboardingEmployees). Ready for another look whenever you get a chance.

return context.hasJoinablePolicies ? [ONBOARDING.PERSONAL_DETAILS, ONBOARDING.WORKSPACES] : [ONBOARDING.PERSONAL_DETAILS];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep Workspaces in the flow until it actually auto-skips

Dropping WORKSPACES whenever hasJoinablePolicies is false does not match the current validated private-domain flow: BaseOnboardingPersonalDetails still navigates validated VSB/SMB users to ONBOARDING_WORKSPACES, and BaseOnboardingWorkspaces only fetches/renders the Skip button when the list is empty; it does not auto-skip. In that no-visible-policy case, pressing Skip takes the user to Employees, but this flow makes the Employees back button go to Personal details instead of the Workspaces screen they just visited.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already check isPrivateDomainAndHasAccessiblePolicies in BaseOnboardingPersonalDetails before navigating, so I don't think this will cause any bug.

cc: @dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayeshmangwani agreed. Excluding WORKSPACES when there are no visible joinable policies is intentional — it uses the same getVisibleJoinablePoliciesCount visibility filter as the rest of the flow, so the back route and step counter only account for screens with something to show. That empty state is what made the back button land on a blank modal in the original report, so keeping the flow consistent with the visible screens is what avoids it. And as you noted, the isPrivateDomainAndHasAccessiblePolicies gate already scopes this to private-domain accounts, so nothing else reaches this branch.

}
return [];
}
Expand Down
18 changes: 15 additions & 3 deletions src/pages/OnboardingEmployees/BaseOnboardingEmployees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useOnboardingStepCounter from '@hooks/useOnboardingStepCounter';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';

import type {OnboardingCompanySize} from '@libs/actions/Welcome/OnboardingFlow';
import {getPreviousOnboardingRoute} from '@libs/getOnboardingStepCounter';
import Navigation from '@libs/Navigation/Navigation';
import {getVisibleJoinablePoliciesCount} from '@libs/OnboardingUtils';
import {expensifyLoginsSelector, isCurrentUserValidated} from '@libs/UserUtils';

import {setOnboardingCompanySize} from '@userActions/Welcome';

Expand Down Expand Up @@ -45,6 +48,11 @@ function BaseOnboardingEmployees({shouldUseNativeStyles, route}: BaseOnboardingE
const [onboardingValues] = useOnyx(ONYXKEYS.NVP_ONBOARDING);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [purposeSelected] = useOnyx(ONYXKEYS.ONBOARDING_PURPOSE_SELECTED);
const [joinablePolicies] = useOnyx(ONYXKEYS.JOINABLE_POLICIES);
const [loginList] = useOnyx(ONYXKEYS.LOGINS, {selector: expensifyLoginsSelector});
const [session] = useOnyx(ONYXKEYS.SESSION);
const {isBetaEnabled} = usePermissions();
const canUseSubmit2026 = isBetaEnabled(CONST.BETAS.SUBMIT_2026);

const onboardingFlowContext = useMemo(
() => ({
Expand All @@ -53,23 +61,27 @@ function BaseOnboardingEmployees({shouldUseNativeStyles, route}: BaseOnboardingE
hasAccessibleDomainPolicies: account?.hasAccessibleDomainPolicies,
purposeSelected: purposeSelected ?? undefined,
isMergeAccountStepSkipped: onboardingValues?.isMergeAccountStepSkipped,
isAccountValidated: !!account?.validated,
isAccountValidated: isCurrentUserValidated(loginList, session?.email),
hasJoinablePolicies: getVisibleJoinablePoliciesCount(joinablePolicies, canUseSubmit2026) > 0,
}),
[
account?.hasAccessibleDomainPolicies,
account?.isFromPublicDomain,
account?.validated,
canUseSubmit2026,
joinablePolicies,
loginList,
onboardingValues?.isMergeAccountStepSkipped,
onboardingValues?.signupQualifier,
purposeSelected,
session?.email,
],
);

const handleBackButtonPress = useCallback(() => {
const previousRoute = getPreviousOnboardingRoute(SCREENS.ONBOARDING.EMPLOYEES, onboardingFlowContext, route.params?.backTo);

if (previousRoute) {
Navigation.navigate(previousRoute);
Navigation.goBack(previousRoute);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useThemeStyles from '@hooks/useThemeStyles';

import {updateOnboardingValuesAndNavigation} from '@libs/actions/Welcome';
import Navigation from '@libs/Navigation/Navigation';
import {getVisibleJoinablePoliciesCount} from '@libs/OnboardingUtils';
import {expensifyLoginsSelector, isCurrentUserValidated} from '@libs/UserUtils';

import {clearGetAccessiblePoliciesErrors, getAccessiblePolicies} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -46,7 +47,7 @@ function BaseOnboardingPrivateDomain({shouldUseNativeStyles, route}: BaseOnboard

const [getAccessiblePoliciesAction] = useOnyx(ONYXKEYS.VALIDATE_USER_AND_GET_ACCESSIBLE_POLICIES);
const [joinablePolicies] = useOnyx(ONYXKEYS.JOINABLE_POLICIES);
const joinablePoliciesLength = Object.values(joinablePolicies ?? {}).filter((policy) => policy.policyType !== CONST.POLICY.TYPE.SUBMIT || canUseSubmit2026).length;
const joinablePoliciesLength = getVisibleJoinablePoliciesCount(joinablePolicies, canUseSubmit2026);

const {onboardingIsMediumOrLargerScreenWidth} = useResponsiveLayout();
const onboardingStep = useOnboardingStepCounter(SCREENS.ONBOARDING.PRIVATE_DOMAIN);
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/OnboardingUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import CONST from '@src/CONST';
import {getVisibleJoinablePoliciesCount} from '@src/libs/OnboardingUtils';
import type {JoinablePolicies} from '@src/types/onyx';
import type {JoinablePolicy} from '@src/types/onyx/JoinablePolicies';

function createJoinablePolicy(policyID: string, policyType: JoinablePolicy['policyType']): JoinablePolicy {
return {
policyID,
policyType,
policyOwner: `${policyID}@example.com`,
policyName: `Workspace ${policyID}`,
employeeCount: 1,
hasPendingAccess: false,
automaticJoiningEnabled: false,
};
}

describe('getVisibleJoinablePoliciesCount', () => {
it('returns 0 for undefined or empty joinable policies', () => {
expect(getVisibleJoinablePoliciesCount(undefined, false)).toBe(0);
expect(getVisibleJoinablePoliciesCount({}, false)).toBe(0);
});

it('counts non-SUBMIT policies regardless of the SUBMIT_2026 beta', () => {
const joinablePolicies: JoinablePolicies = {
a: createJoinablePolicy('a', CONST.POLICY.TYPE.TEAM),
b: createJoinablePolicy('b', CONST.POLICY.TYPE.CORPORATE),
};
expect(getVisibleJoinablePoliciesCount(joinablePolicies, false)).toBe(2);
expect(getVisibleJoinablePoliciesCount(joinablePolicies, true)).toBe(2);
});

// Repro guard: SUBMIT policies are hidden while the beta is off, so a user whose only joinable policies are
// SUBMIT must not get a phantom WORKSPACES step (inflated counter + blank back navigation from EMPLOYEES).
it('hides SUBMIT policies when the SUBMIT_2026 beta is disabled', () => {
const joinablePolicies: JoinablePolicies = {
a: createJoinablePolicy('a', CONST.POLICY.TYPE.SUBMIT),
b: createJoinablePolicy('b', CONST.POLICY.TYPE.SUBMIT),
};
expect(getVisibleJoinablePoliciesCount(joinablePolicies, false)).toBe(0);
});

it('counts SUBMIT policies when the SUBMIT_2026 beta is enabled', () => {
const joinablePolicies: JoinablePolicies = {
a: createJoinablePolicy('a', CONST.POLICY.TYPE.SUBMIT),
b: createJoinablePolicy('b', CONST.POLICY.TYPE.SUBMIT),
};
expect(getVisibleJoinablePoliciesCount(joinablePolicies, true)).toBe(2);
});

it('counts only the visible policies in a mixed set when the beta is off', () => {
const joinablePolicies: JoinablePolicies = {
a: createJoinablePolicy('a', CONST.POLICY.TYPE.TEAM),
b: createJoinablePolicy('b', CONST.POLICY.TYPE.SUBMIT),
c: createJoinablePolicy('c', CONST.POLICY.TYPE.CORPORATE),
};
expect(getVisibleJoinablePoliciesCount(joinablePolicies, false)).toBe(2);
expect(getVisibleJoinablePoliciesCount(joinablePolicies, true)).toBe(3);
});
});
Loading
Loading