|
| 1 | +import {useCallback} from 'react'; |
| 2 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 3 | +import interceptAnonymousUser from '@libs/interceptAnonymousUser'; |
| 4 | +import Navigation from '@libs/Navigation/Navigation'; |
| 5 | +import {getDefaultChatEnabledPolicy, isPaidGroupPolicy} from '@libs/PolicyUtils'; |
| 6 | +import {generateReportID} from '@libs/ReportUtils'; |
| 7 | +import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; |
| 8 | +import CONST from '@src/CONST'; |
| 9 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 10 | +import ROUTES from '@src/ROUTES'; |
| 11 | +import type * as OnyxTypes from '@src/types/onyx'; |
| 12 | +import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; |
| 13 | +import useCreateEmptyReportConfirmation from './useCreateEmptyReportConfirmation'; |
| 14 | +import useHasEmptyReportsForPolicy from './useHasEmptyReportsForPolicy'; |
| 15 | +import useOnyx from './useOnyx'; |
| 16 | + |
| 17 | +type UseCreateReportParams = { |
| 18 | + /** Callback that creates the report and navigates after creation */ |
| 19 | + onCreateReport: (shouldDismissEmptyReportsConfirmation?: boolean) => void; |
| 20 | + /** Group paid policies with expense chat enabled */ |
| 21 | + groupPoliciesWithChatEnabled: readonly never[] | Array<OnyxEntry<OnyxTypes.Policy>>; |
| 22 | + /** Whether the empty-report confirmation modal should push a history entry so browser-back dismisses it (default: true) */ |
| 23 | + shouldHandleNavigationBack?: boolean; |
| 24 | +}; |
| 25 | + |
| 26 | +type UseCreateReportResult = { |
| 27 | + /** The callback to trigger when the user clicks "Create report" */ |
| 28 | + createReport: () => void; |
| 29 | + /** Whether the menu item/button should be visible */ |
| 30 | + isVisible: boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Hook that encapsulates the shared "create report" branching logic used across |
| 35 | + * the FAB, the search Create dropdown, and the empty reports state. |
| 36 | + * |
| 37 | + * Decision flow: |
| 38 | + * 1. Navigate to upgrade path if user has no valid group policies at all |
| 39 | + * 2. Navigate to workspace selector if default is personal AND there are at least 2 non-personal workspaces, or if the chosen default is billing-restricted and alternatives exist |
| 40 | + * 3. Show empty report confirmation or create directly if workspace is valid |
| 41 | + * 4. Navigate to restricted action if billing restricts the workspace |
| 42 | + */ |
| 43 | +export default function useCreateReport({onCreateReport, groupPoliciesWithChatEnabled, shouldHandleNavigationBack = true}: UseCreateReportParams): UseCreateReportResult { |
| 44 | + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); |
| 45 | + const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); |
| 46 | + const [, policiesLoadStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY); |
| 47 | + const [ownerBillingGracePeriodEnd] = useOnyx(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END); |
| 48 | + const [userBillingGracePeriodEnds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END); |
| 49 | + const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED); |
| 50 | + const [hasDismissedEmptyReportsConfirmation] = useOnyx(ONYXKEYS.NVP_EMPTY_REPORTS_CONFIRMATION_DISMISSED); |
| 51 | + const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID}); |
| 52 | + |
| 53 | + // Gate visibility and routing on policy hydration. Without this, during Onyx cold-start |
| 54 | + // groupPoliciesWithChatEnabled.length === 0 would be true even for users who actually have |
| 55 | + // workspaces, sending them to MONEY_REQUEST_UPGRADE as if they had none. |
| 56 | + const arePoliciesLoaded = !isLoadingOnyxValue(policiesLoadStatus); |
| 57 | + const isVisible = arePoliciesLoaded; |
| 58 | + const shouldNavigateToUpgradePath = groupPoliciesWithChatEnabled.length === 0; |
| 59 | + |
| 60 | + const defaultChatEnabledPolicy = getDefaultChatEnabledPolicy(groupPoliciesWithChatEnabled as Array<OnyxEntry<OnyxTypes.Policy>>, activePolicy); |
| 61 | + const defaultChatEnabledPolicyID = defaultChatEnabledPolicy?.id; |
| 62 | + |
| 63 | + const hasEmptyReport = useHasEmptyReportsForPolicy(defaultChatEnabledPolicyID); |
| 64 | + const shouldShowEmptyReportConfirmation = hasEmptyReport && hasDismissedEmptyReportsConfirmation !== true; |
| 65 | + |
| 66 | + const {openCreateReportConfirmation} = useCreateEmptyReportConfirmation({ |
| 67 | + policyID: defaultChatEnabledPolicyID, |
| 68 | + policyName: defaultChatEnabledPolicy?.name ?? '', |
| 69 | + onConfirm: onCreateReport, |
| 70 | + shouldHandleNavigationBack, |
| 71 | + }); |
| 72 | + |
| 73 | + const createReport = useCallback(() => { |
| 74 | + interceptAnonymousUser(() => { |
| 75 | + if (!arePoliciesLoaded) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // No valid policy at all → upgrade + create workspace flow |
| 80 | + if (shouldNavigateToUpgradePath) { |
| 81 | + const freshReportID = generateReportID(); |
| 82 | + const freshTransactionID = generateReportID(); |
| 83 | + Navigation.navigate( |
| 84 | + ROUTES.MONEY_REQUEST_UPGRADE.getRoute({ |
| 85 | + action: CONST.IOU.ACTION.CREATE, |
| 86 | + iouType: CONST.IOU.TYPE.CREATE, |
| 87 | + transactionID: freshTransactionID, |
| 88 | + reportID: freshReportID, |
| 89 | + upgradePath: CONST.UPGRADE_PATHS.REPORTS, |
| 90 | + }), |
| 91 | + ); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const workspaceIDForReportCreation = defaultChatEnabledPolicyID; |
| 96 | + |
| 97 | + // Show the workspace selector only when the default workspace is personal and there are |
| 98 | + // at least 2 non-personal workspaces to choose between. Also fall back to the selector if |
| 99 | + // the default is billing-restricted and alternatives exist, so the user isn't dead-ended |
| 100 | + // on the restricted-action page. |
| 101 | + const isDefaultPersonal = !activePolicy || activePolicy.type === CONST.POLICY.TYPE.PERSONAL || !isPaidGroupPolicy(activePolicy); |
| 102 | + const hasMultipleNonPersonalWorkspaces = groupPoliciesWithChatEnabled.length > 1; |
| 103 | + const isDefaultBillingRestricted = |
| 104 | + !!workspaceIDForReportCreation && shouldRestrictUserBillableActions(defaultChatEnabledPolicy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, accountID); |
| 105 | + |
| 106 | + if (!workspaceIDForReportCreation || (isDefaultPersonal && hasMultipleNonPersonalWorkspaces) || (isDefaultBillingRestricted && hasMultipleNonPersonalWorkspaces)) { |
| 107 | + Navigation.navigate(ROUTES.NEW_REPORT_WORKSPACE_SELECTION.getRoute()); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Default workspace is not restricted → create report directly (or show empty-report confirmation) |
| 112 | + if (!shouldRestrictUserBillableActions(defaultChatEnabledPolicy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, accountID)) { |
| 113 | + if (shouldShowEmptyReportConfirmation) { |
| 114 | + openCreateReportConfirmation(); |
| 115 | + } else { |
| 116 | + onCreateReport(false); |
| 117 | + } |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(workspaceIDForReportCreation)); |
| 122 | + }); |
| 123 | + }, [ |
| 124 | + arePoliciesLoaded, |
| 125 | + shouldNavigateToUpgradePath, |
| 126 | + activePolicy, |
| 127 | + defaultChatEnabledPolicy, |
| 128 | + defaultChatEnabledPolicyID, |
| 129 | + ownerBillingGracePeriodEnd, |
| 130 | + userBillingGracePeriodEnds, |
| 131 | + amountOwed, |
| 132 | + accountID, |
| 133 | + groupPoliciesWithChatEnabled.length, |
| 134 | + shouldShowEmptyReportConfirmation, |
| 135 | + openCreateReportConfirmation, |
| 136 | + onCreateReport, |
| 137 | + ]); |
| 138 | + |
| 139 | + return {createReport, isVisible}; |
| 140 | +} |
0 commit comments