|
| 1 | +import React from 'react'; |
| 2 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 3 | +import useOnyx from '@hooks/useOnyx'; |
| 4 | +import useReportIsArchived from '@hooks/useReportIsArchived'; |
| 5 | +import {isPolicyExpenseChat} from '@libs/ReportUtils'; |
| 6 | +import CONST from '@src/CONST'; |
| 7 | +import type {IOUAction, IOUType} from '@src/CONST'; |
| 8 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 9 | +import type {Route} from '@src/ROUTES'; |
| 10 | +import type {Policy, Report} from '@src/types/onyx'; |
| 11 | +import type Transaction from '@src/types/onyx/Transaction'; |
| 12 | +import MultiScanGate from './components/MultiScanGate'; |
| 13 | +import ScanEditReceipt from './components/ScanEditReceipt'; |
| 14 | +import ScanFromReport from './components/ScanFromReport'; |
| 15 | +import ScanGlobalCreate from './components/ScanGlobalCreate'; |
| 16 | +import ScanSkipConfirmation from './components/ScanSkipConfirmation'; |
| 17 | + |
| 18 | +type ScanRouterProps = { |
| 19 | + report: OnyxEntry<Report>; |
| 20 | + action: IOUAction; |
| 21 | + iouType: IOUType; |
| 22 | + reportID: string; |
| 23 | + transactionID: string; |
| 24 | + transaction: OnyxEntry<Transaction>; |
| 25 | + backTo: Route | undefined; |
| 26 | + backToReport: string | undefined; |
| 27 | +}; |
| 28 | + |
| 29 | +type NonGlobalCreateProps = { |
| 30 | + report: OnyxEntry<Report>; |
| 31 | + iouType: IOUType; |
| 32 | + reportID: string; |
| 33 | + transactionID: string; |
| 34 | + transaction: OnyxEntry<Transaction>; |
| 35 | + backToReport: string | undefined; |
| 36 | +}; |
| 37 | + |
| 38 | +type NewReceiptProps = NonGlobalCreateProps; |
| 39 | + |
| 40 | +const policyRequiresTagOrCategorySelector = (policy: OnyxEntry<Policy>) => !!policy?.requiresCategory || !!policy?.requiresTag; |
| 41 | + |
| 42 | +/** |
| 43 | + * Owns the policy + skip-confirmation subscriptions so the edit and global-create branches don't pay for them. |
| 44 | + * Decides between SkipConfirmation (quick action) and FromReport (report (+) entry). |
| 45 | + */ |
| 46 | +function ScanNonGlobalCreate({report, iouType, reportID, transactionID, transaction, backToReport}: NonGlobalCreateProps) { |
| 47 | + const [policyRequiresTagOrCategory] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {selector: policyRequiresTagOrCategorySelector}); |
| 48 | + const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`); |
| 49 | + const shouldSkipConfirmation = !!skipConfirmation && !!report?.reportID && !(isPolicyExpenseChat(report) && policyRequiresTagOrCategory); |
| 50 | + |
| 51 | + if (shouldSkipConfirmation) { |
| 52 | + return ( |
| 53 | + <ScanSkipConfirmation |
| 54 | + report={report} |
| 55 | + iouType={iouType} |
| 56 | + reportID={reportID} |
| 57 | + transactionID={transactionID} |
| 58 | + transaction={transaction} |
| 59 | + backToReport={backToReport} |
| 60 | + /> |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <ScanFromReport |
| 66 | + report={report} |
| 67 | + iouType={iouType} |
| 68 | + reportID={reportID} |
| 69 | + transactionID={transactionID} |
| 70 | + transaction={transaction} |
| 71 | + backToReport={backToReport} |
| 72 | + /> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +ScanNonGlobalCreate.displayName = 'ScanNonGlobalCreate'; |
| 77 | + |
| 78 | +/** |
| 79 | + * Splits new-receipt flows: global-create (FAB) vs. report-scoped (FromReport / SkipConfirm). |
| 80 | + * The archived-report check lives here so neither global-create nor non-global-create variants need to subscribe. |
| 81 | + */ |
| 82 | +function ScanNewReceipt({report, iouType, reportID, transactionID, transaction, backToReport}: NewReceiptProps) { |
| 83 | + const isArchived = useReportIsArchived(report?.reportID); |
| 84 | + const isFromGlobalCreate = !!transaction?.isFromGlobalCreate; |
| 85 | + |
| 86 | + if (!isFromGlobalCreate && !isArchived && iouType !== CONST.IOU.TYPE.CREATE) { |
| 87 | + return ( |
| 88 | + <ScanNonGlobalCreate |
| 89 | + report={report} |
| 90 | + iouType={iouType} |
| 91 | + reportID={reportID} |
| 92 | + transactionID={transactionID} |
| 93 | + transaction={transaction} |
| 94 | + backToReport={backToReport} |
| 95 | + /> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <ScanGlobalCreate |
| 101 | + iouType={iouType} |
| 102 | + reportID={reportID} |
| 103 | + transactionID={transactionID} |
| 104 | + transaction={transaction} |
| 105 | + backToReport={backToReport} |
| 106 | + /> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +ScanNewReceipt.displayName = 'ScanNewReceipt'; |
| 111 | + |
| 112 | +/** |
| 113 | + * ScanRouter — selects the appropriate scan variant based on route params and transaction state. |
| 114 | + * |
| 115 | + * Edit branch is a fast-path that subscribes to nothing extra. Non-edit branches go through MultiScanGate |
| 116 | + * and the layered ScanNewReceipt/ScanNonGlobalCreate components, which scope their subscriptions to the |
| 117 | + * narrowest variant that needs them. |
| 118 | + */ |
| 119 | +function ScanRouter({report, action, iouType, reportID, transactionID, transaction, backTo, backToReport}: ScanRouterProps) { |
| 120 | + const isEditing = action === CONST.IOU.ACTION.EDIT; |
| 121 | + |
| 122 | + if (backTo || isEditing) { |
| 123 | + return ( |
| 124 | + <ScanEditReceipt |
| 125 | + report={report} |
| 126 | + transactionID={transactionID} |
| 127 | + backTo={backTo} |
| 128 | + isEditing={isEditing} |
| 129 | + /> |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + return ( |
| 134 | + <MultiScanGate> |
| 135 | + <ScanNewReceipt |
| 136 | + report={report} |
| 137 | + iouType={iouType} |
| 138 | + reportID={reportID} |
| 139 | + transactionID={transactionID} |
| 140 | + transaction={transaction} |
| 141 | + backToReport={backToReport} |
| 142 | + /> |
| 143 | + </MultiScanGate> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +ScanRouter.displayName = 'ScanRouter'; |
| 148 | + |
| 149 | +export default ScanRouter; |
0 commit comments