-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathuseSelectionModeReportActions.ts
More file actions
302 lines (273 loc) · 13 KB
/
Copy pathuseSelectionModeReportActions.ts
File metadata and controls
302 lines (273 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import {useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types';
import type {PopoverMenuItem} from '@components/PopoverMenu';
import type {ActionHandledType} from '@components/ProcessMoneyReportHoldMenu';
import {useSearchSelectionActions} from '@components/Search/SearchContext';
import {canIOUBePaid as canIOUBePaidAction} from '@libs/actions/IOU/ReportWorkflow';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {getTotalAmountForIOUReportPreviewButton} from '@libs/MoneyRequestReportUtils';
import TransitionTracker from '@libs/Navigation/TransitionTracker';
import type {KYCFlowEvent, TriggerKYCFlow} from '@libs/PaymentUtils';
import {isSubmitPolicy} from '@libs/PolicyUtils';
import {getReportPrimaryAction} from '@libs/ReportPrimaryActionUtils';
import {getSecondaryReportActions} from '@libs/ReportSecondaryActionUtils';
import {getNonHeldAndFullAmount, hasOnlyHeldExpenses as hasOnlyHeldExpensesReportUtils, hasUpdatedTotal, shouldShowMarkAsDone} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import type * as OnyxTypes from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import {useCurrencyListActions} from './useCurrencyList';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useEnvironment from './useEnvironment';
import {useMemoizedLazyExpensifyIcons} from './useLazyAsset';
import useLifecycleActions from './useLifecycleActions';
import useLocalize from './useLocalize';
import useNetwork from './useNetwork';
import useOnyx from './useOnyx';
import useReportIsArchived from './useReportIsArchived';
import useSelectionModePayment from './useSelectionModePayment';
type UseSelectionModeReportActionsParams = {
report: OnyxEntry<OnyxTypes.Report>;
chatReport: OnyxEntry<OnyxTypes.Report>;
policy: OnyxEntry<OnyxTypes.Policy>;
reportActions: OnyxTypes.ReportAction[];
reportNameValuePairs: OnyxEntry<OnyxTypes.ReportNameValuePairs>;
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>;
transactions: OnyxTypes.Transaction[];
selectedTransactionIDs: string[];
};
function useSelectionModeReportActions({
report,
chatReport,
policy,
reportActions,
reportNameValuePairs,
reportMetadata,
transactions,
selectedTransactionIDs,
}: UseSelectionModeReportActionsParams) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {convertToDisplayString} = useCurrencyListActions();
const {accountID: currentUserAccountID, login: currentUserLogin} = useCurrentUserPersonalDetails();
const {clearSelectedTransactions} = useSearchSelectionActions();
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const [outstandingReportsByPolicyID] = useOnyx(ONYXKEYS.DERIVED.OUTSTANDING_REPORTS_BY_POLICY_ID);
const [submitterLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(report?.ownerAccountID)}, [report?.ownerAccountID]);
const [invoiceReceiverPolicy] = useOnyx(
`${ONYXKEYS.COLLECTION.POLICY}${chatReport?.invoiceReceiver && 'policyID' in chatReport.invoiceReceiver ? chatReport.invoiceReceiver.policyID : undefined}`,
);
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});
const {isProduction} = useEnvironment();
const isChatReportArchived = useReportIsArchived(chatReport?.reportID);
const expensifyIcons = useMemoizedLazyExpensifyIcons(['Send', 'ThumbsUp', 'Cash', 'ArrowRight'] as const);
// Hold menu state (managed locally on mobile; desktop uses modals context)
const [isHoldMenuVisible, setIsHoldMenuVisible] = useState(false);
const [paymentType, setPaymentType] = useState<PaymentMethodType>();
const [requestType, setRequestType] = useState<ActionHandledType>();
const [selectedVBBAToPayFromHoldMenu, setSelectedVBBAToPayFromHoldMenu] = useState<number | undefined>(undefined);
// Submit/approve via shared useLifecycleActions (no animations on mobile, skipAnimation=true)
const {
confirmApproval: lifecycleConfirmApproval,
handleSubmitReport: lifecycleHandleSubmitReport,
shouldBlockSubmit,
isBlockSubmitDueToPreventSelfApproval,
} = useLifecycleActions({
reportID: report?.reportID,
startApprovedAnimation: () => {},
startAnimation: () => {},
startSubmittingAnimation: () => {},
onHoldMenuOpen: (rt, _onConfirm, pt) => {
setRequestType(rt);
if (pt) {
setPaymentType(pt);
}
setIsHoldMenuVisible(true);
},
onCleanup: turnOffMobileSelectionMode,
});
const handleSubmitReport = () => {
lifecycleHandleSubmitReport(true);
};
const confirmApproval = () => {
lifecycleConfirmApproval(true);
};
// Payment flow
const nonPendingDeleteTransactions = transactions.filter((t): t is OnyxTypes.Transaction => !!t && (isOffline || t.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE));
const getCanIOUBePaid = (onlyShowPayElsewhere = false) =>
canIOUBePaidAction(report, chatReport, policy, bankAccountList, currentUserLogin ?? '', currentUserAccountID, transactions, onlyShowPayElsewhere, undefined, invoiceReceiverPolicy);
const canIOUBePaid = getCanIOUBePaid();
const onlyShowPayElsewhere = !canIOUBePaid && getCanIOUBePaid(true);
const shouldShowPayButton = canIOUBePaid || onlyShowPayElsewhere;
const canAllowSettlement = hasUpdatedTotal(report, policy);
const totalAmount = getTotalAmountForIOUReportPreviewButton(report, policy, CONST.REPORT.PRIMARY_ACTIONS.PAY, nonPendingDeleteTransactions, convertToDisplayString);
const {nonHeldAmount, fullAmount, hasValidNonHeldAmount} = getNonHeldAndFullAmount(report, shouldShowPayButton, transactions);
// Primary/secondary action detection
const currentUserEmail = currentUserLogin ?? '';
const primaryAction = getReportPrimaryAction({
currentUserLogin: currentUserEmail,
currentUserAccountID,
report,
chatReport,
reportTransactions: transactions,
violations: allTransactionViolations,
bankAccountList,
policy,
reportNameValuePairs,
reportActions,
reportMetadata,
isChatReportArchived,
invoiceReceiverPolicy,
ownerLogin: submitterLogin,
});
const secondaryActions = (() => {
if (!report) {
return [];
}
return getSecondaryReportActions({
currentUserLogin: currentUserEmail,
currentUserAccountID,
submitterLogin,
report,
chatReport,
reportTransactions: transactions,
originalTransaction: undefined,
violations: allTransactionViolations,
bankAccountList,
policy,
reportNameValuePairs,
reportActions,
reportMetadata,
policies,
outstandingReportsByPolicyID,
isChatReportArchived,
isProduction,
});
})();
const hasSubmitAction = primaryAction === CONST.REPORT.PRIMARY_ACTIONS.SUBMIT || secondaryActions.includes(CONST.REPORT.SECONDARY_ACTIONS.SUBMIT);
const hasApproveAction = primaryAction === CONST.REPORT.PRIMARY_ACTIONS.APPROVE || secondaryActions.includes(CONST.REPORT.SECONDARY_ACTIONS.APPROVE);
const hasPayAction = primaryAction === CONST.REPORT.PRIMARY_ACTIONS.PAY || secondaryActions.includes(CONST.REPORT.SECONDARY_ACTIONS.PAY);
const allExpensesSelected = selectedTransactionIDs.length > 0 && selectedTransactionIDs.length === transactions.length;
const selectedTransactions = transactions.filter((transaction) => selectedTransactionIDs.includes(transaction.transactionID));
const hasSelectedTransactionsOnSubmitPolicy = isSubmitPolicy(policy) && selectedTransactions.length > 0;
const isBlockSubmitDueToSelectedTransactionsOnSubmitPolicy = hasSelectedTransactionsOnSubmitPolicy && selectedTransactions.length > 1;
const effectiveShouldBlockSubmit = shouldBlockSubmit || isBlockSubmitDueToSelectedTransactionsOnSubmitPolicy;
// Shared payment hook
const {confirmPayment, shouldBlockAction, invokePaymentSelect, selectionModeKYCSuccess, paymentSubMenuItems, hasPayInSelectionMode, isAnyTransactionOnHold, isInvoiceReport, kycWallRef} =
useSelectionModePayment({
reportID: report?.reportID,
transactions,
formattedAmount: totalAmount,
shouldHidePaymentOptions: !shouldShowPayButton,
onlyShowPayElsewhere,
hasPayAction,
allExpensesSelected,
onHoldMenuOpen: ({requestType: rt, paymentType: pt, methodID}) => {
setRequestType(rt);
setPaymentType(pt);
setSelectedVBBAToPayFromHoldMenu(methodID);
setIsHoldMenuVisible(true);
},
onPaymentComplete: () => {
clearSelectedTransactions(true);
turnOffMobileSelectionMode();
},
confirmApproval,
});
// Defer payment select until the popover dismiss animation completes. Blocking modals are shown
// synchronously inside the callback (popover already closed) to avoid double-defer on Android.
const onSelectionModePaymentSelect = (event: KYCFlowEvent, iouPaymentType: PaymentMethodType, triggerKYCFlow: TriggerKYCFlow) => {
TransitionTracker.runAfterTransitions({
callback: () => {
if (shouldBlockAction(iouPaymentType)) {
return;
}
invokePaymentSelect(event, iouPaymentType, triggerKYCFlow);
},
waitForUpcomingTransition: true,
});
};
// Build report-level action menu
const selectionModeReportLevelActions = (() => {
const actions: Array<DropdownOption<string> & Pick<PopoverMenuItem, 'backButtonText' | 'rightIcon' | 'subMenuItems'>> = [];
let idx = 0;
if (hasSubmitAction && !effectiveShouldBlockSubmit) {
actions[idx++] = {
text: shouldShowMarkAsDone({policy, report, isTrackIntentUser}) ? translate('common.markAsDone') : translate('common.submit'),
icon: expensifyIcons.Send,
value: CONST.REPORT.PRIMARY_ACTIONS.SUBMIT,
onSelected: handleSubmitReport,
};
}
if (hasApproveAction && !isBlockSubmitDueToPreventSelfApproval) {
actions[idx++] = {
text: translate('iou.approve'),
icon: expensifyIcons.ThumbsUp,
value: CONST.REPORT.PRIMARY_ACTIONS.APPROVE,
onSelected: confirmApproval,
};
}
if (hasPayAction && !(isOffline && !canAllowSettlement)) {
actions[idx++] = {
text: translate('iou.settlePayment', totalAmount),
icon: expensifyIcons.Cash,
value: CONST.REPORT.PRIMARY_ACTIONS.PAY,
rightIcon: expensifyIcons.ArrowRight,
backButtonText: translate('iou.settlePayment', totalAmount),
subMenuItems: paymentSubMenuItems,
onSelected: () => {},
};
}
return actions;
})();
const handleHoldMenuClose = () => {
setSelectedVBBAToPayFromHoldMenu(undefined);
setIsHoldMenuVisible(false);
};
const handleHoldMenuConfirm = () => {
clearSelectedTransactions(true);
turnOffMobileSelectionMode();
};
return {
selectionModeReportLevelActions,
allExpensesSelected,
shouldBlockSubmit: effectiveShouldBlockSubmit,
isBlockSubmitDueToPreventSelfApproval,
// Hold menu state
isHoldMenuVisible,
requestType,
paymentType,
selectedVBBAToPayFromHoldMenu,
handleHoldMenuClose,
handleHoldMenuConfirm,
confirmPayment,
confirmApproval,
shouldBlockAction,
// Pay-related
hasPayAction,
hasPayInSelectionMode,
hasSubmitAction,
hasSelectedTransactionsOnSubmitPolicy,
hasApproveAction,
totalAmount,
canAllowSettlement,
isAnyTransactionOnHold,
isInvoiceReport,
hasOnlyHeldExpenses: hasOnlyHeldExpensesReportUtils(transactions),
nonHeldAmount,
fullAmount,
hasValidNonHeldAmount,
// KYC dropdown integration
onSelectionModePaymentSelect,
selectionModeKYCSuccess,
// Data for external use
primaryAction,
kycWallRef,
};
}
export default useSelectionModeReportActions;