-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathuseMoneyReportHeaderStatusBar.ts
More file actions
156 lines (136 loc) · 7.73 KB
/
Copy pathuseMoneyReportHeaderStatusBar.ts
File metadata and controls
156 lines (136 loc) · 7.73 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
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getAllNonDeletedTransactions} from '@libs/MoneyRequestReportUtils';
import {getFilteredReportActionsForReportView, getOneTransactionThreadReportID, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {isMarkAsResolvedAction} from '@libs/ReportPrimaryActionUtils';
import {hasOnlyHeldExpenses as hasOnlyHeldExpensesReportUtils, isSettled as isSettledReportUtils} from '@libs/ReportUtils';
import {
allHavePendingRTERViolation,
hasDuplicateTransactions,
isBrokenConnectionViolation,
hasReceipt,
isPayAtEndExpense as isPayAtEndExpenseTransactionUtils,
isPending,
isScanning,
shouldSuppressBrokenConnectionStatus,
shouldShowBrokenConnectionViolationForMultipleTransactions,
} from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ValueOf} from 'type-fest';
import {personalDetailsLoginSelector} from '@selectors/PersonalDetails';
import {getArchiveReason} from '@selectors/Report';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useNetwork from './useNetwork';
import useOnyx from './useOnyx';
import usePaginatedReportActions from './usePaginatedReportActions';
import useReportIsArchived from './useReportIsArchived';
import useReportTransactionsCollection from './useReportTransactionsCollection';
import useTransactionsAndViolationsForReport from './useTransactionsAndViolationsForReport';
import useTransactionViolations from './useTransactionViolations';
type StatusBarType = ValueOf<typeof CONST.REPORT.STATUS_BAR_TYPE>;
type StatusBarResult = {
shouldShowStatusBar: boolean;
statusBarType: StatusBarType | undefined;
};
function useMoneyReportHeaderStatusBar(reportID: string | undefined, chatReportID: string | undefined): StatusBarResult {
const {isOffline} = useNetwork();
const {accountID, email} = useCurrentUserPersonalDetails();
const [moneyRequestReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [ownerLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(moneyRequestReport?.ownerAccountID)});
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(moneyRequestReport?.policyID)}`);
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
const {reportActions: unfilteredReportActions} = usePaginatedReportActions(moneyRequestReport?.reportID);
const reportActions = getFilteredReportActionsForReportView(unfilteredReportActions);
const allReportTransactions = useReportTransactionsCollection(reportID);
const nonDeletedTransactions = getAllNonDeletedTransactions(allReportTransactions, reportActions, isOffline, true);
const visibleTransactions = nonDeletedTransactions?.filter((t) => isOffline || t.pendingAction !== 'delete');
const reportTransactionIDs = visibleTransactions?.map((t) => t.transactionID);
const transactionThreadReportID = getOneTransactionThreadReportID(moneyRequestReport, chatReport, reportActions ?? [], isOffline, reportTransactionIDs);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);
const requestParentReportAction = (() => {
if (!reportActions || !transactionThreadReport?.parentReportActionID) {
return null;
}
return reportActions.find((action) => action.reportActionID === transactionThreadReport.parentReportActionID);
})();
const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined;
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`);
const transactionViolations = useTransactionViolations(iouTransactionID);
const {transactions: reportTransactionsMap, violations} = useTransactionsAndViolationsForReport(moneyRequestReport?.reportID);
const transactions = Object.values(reportTransactionsMap);
const isArchivedReport = useReportIsArchived(moneyRequestReport?.reportID);
const [archiveReason] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport?.reportID}`, {selector: getArchiveReason});
const hasScanningReceipt = transactions.filter((t) => hasReceipt(t)).some(isScanning);
const hasOnlyPendingTransactions = transactions.length > 0 && transactions.every((t) => isPending(t));
const hasAllPendingRTERViolations = allHavePendingRTERViolation(transactions, violations, email ?? '', accountID, moneyRequestReport, ownerLogin, policy);
const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationForMultipleTransactions(
transactions,
moneyRequestReport,
ownerLogin,
policy,
violations,
email ?? '',
accountID,
);
const hasOnlyHeldExpenses = hasOnlyHeldExpensesReportUtils(transactions);
const isPayAtEndExpense = isPayAtEndExpenseTransactionUtils(transaction);
const isReportSettled = isSettledReportUtils(moneyRequestReport);
const hasDuplicates = !isReportSettled && hasDuplicateTransactions(email ?? '', accountID, moneyRequestReport, ownerLogin, policy, allTransactionViolations, transactions);
const shouldShowMarkAsResolved = isMarkAsResolvedAction(moneyRequestReport, transactionViolations);
const shouldShowStatusBar =
hasAllPendingRTERViolations ||
shouldShowBrokenConnectionViolation ||
hasOnlyHeldExpenses ||
hasScanningReceipt ||
isPayAtEndExpense ||
hasOnlyPendingTransactions ||
hasDuplicates ||
shouldShowMarkAsResolved;
const getStatusBarType = (): StatusBarType | undefined => {
if (shouldShowMarkAsResolved) {
return CONST.REPORT.STATUS_BAR_TYPE.MARK_AS_RESOLVED;
}
if (isPayAtEndExpense) {
if (!isArchivedReport) {
return CONST.REPORT.STATUS_BAR_TYPE.BOOKING_PENDING;
}
if (archiveReason === CONST.REPORT.ARCHIVE_REASON.BOOKING_END_DATE_HAS_PASSED) {
return CONST.REPORT.STATUS_BAR_TYPE.BOOKING_ARCHIVED;
}
}
if (hasOnlyHeldExpenses) {
return CONST.REPORT.STATUS_BAR_TYPE.ON_HOLD;
}
if (hasDuplicates) {
return CONST.REPORT.STATUS_BAR_TYPE.DUPLICATES;
}
if (shouldShowBrokenConnectionViolation) {
const brokenConnectionViolations = transactionViolations.length
? transactionViolations.filter(isBrokenConnectionViolation)
: (visibleTransactions?.flatMap((t) => violations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${t.transactionID}`] ?? []).filter(isBrokenConnectionViolation) ?? []);
// A report must retain a status if any violation needs an actionable or retry-later message.
if (shouldSuppressBrokenConnectionStatus(brokenConnectionViolations, cardList)) {
return undefined;
}
return CONST.REPORT.STATUS_BAR_TYPE.BROKEN_CONNECTION;
}
if (hasAllPendingRTERViolations) {
return CONST.REPORT.STATUS_BAR_TYPE.PENDING_RTER;
}
if (hasOnlyPendingTransactions) {
return CONST.REPORT.STATUS_BAR_TYPE.PENDING_TRANSACTIONS;
}
if (hasScanningReceipt) {
return CONST.REPORT.STATUS_BAR_TYPE.SCANNING_RECEIPT;
}
return undefined;
};
const statusBarType = getStatusBarType();
return {
shouldShowStatusBar,
statusBarType,
};
}
export default useMoneyReportHeaderStatusBar;