Skip to content

Commit 835b40a

Browse files
authored
Merge pull request #79832 from ShridharGoel/onyxConnect2.2
[Part 2] Onyx.connect() removal for ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS in ReportUtils.ts
2 parents d1f84f0 + e6407bc commit 835b40a

30 files changed

Lines changed: 253 additions & 246 deletions

src/components/MoneyRequestConfirmationList/sections/ReportField.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import type {OnyxEntry} from 'react-native-onyx';
33
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
4+
import useArchivedReportsIDSet from '@hooks/useArchivedReportsIDSet';
45
import useLocalize from '@hooks/useLocalize';
56
import useOnyx from '@hooks/useOnyx';
67
import useOutstandingReports from '@hooks/useOutstandingReports';
@@ -15,7 +16,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
1516
import ROUTES from '@src/ROUTES';
1617
import type * as OnyxTypes from '@src/types/onyx';
1718
import type {Participant} from '@src/types/onyx/IOU';
18-
import {createOutstandingReportsForPolicySelector, createOutstandingReportsNVPsSelector, reportFieldTransactionStateSelector} from './selectors';
19+
import {createOutstandingReportsForPolicySelector, reportFieldTransactionStateSelector} from './selectors';
1920
import useTransactionSelector from './useTransactionSelector';
2021

2122
type ReportFieldProps = {
@@ -51,9 +52,7 @@ function ReportField({selectedParticipants, iouType, reportID, reportActionID, a
5152
const reportAttributes = useReportAttributes();
5253
const policyID = selectedParticipants?.at(0)?.policyID;
5354
const [outstandingReportsForPolicy] = useOnyx(ONYXKEYS.DERIVED.OUTSTANDING_REPORTS_BY_POLICY_ID, {selector: createOutstandingReportsForPolicySelector(policyID)}, [policyID]);
54-
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {selector: createOutstandingReportsNVPsSelector(outstandingReportsForPolicy)}, [
55-
outstandingReportsForPolicy,
56-
]);
55+
const archivedReportsIDSet = useArchivedReportsIDSet();
5756

5857
// Self-resolved narrow slice of the transaction; replaces the previously prop-drilled `transaction` object.
5958
const transactionState = useTransactionSelector(transactionID, reportFieldTransactionStateSelector);
@@ -73,11 +72,11 @@ function ReportField({selectedParticipants, iouType, reportID, reportActionID, a
7372
* We need to check if the transaction report exists first in order to prevent the outstanding reports from being used.
7473
* Also we need to check if transaction report exists in outstanding reports in order to show a correct report name.
7574
*/
76-
const shouldUseTransactionReport = (!!transactionReportEntry && isReportOutstanding(transactionReportEntry, policyID, undefined, false)) || isUnreported;
75+
const shouldUseTransactionReport = (!!transactionReportEntry && isReportOutstanding(transactionReportEntry, policyID, archivedReportsIDSet, false)) || isUnreported;
7776

7877
const ownerAccountID = selectedParticipants?.at(0)?.ownerAccountID;
7978

80-
const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, outstandingReportsForPolicy ?? {}, reportNameValuePairs, false).sort((a, b) =>
79+
const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, archivedReportsIDSet, outstandingReportsForPolicy ?? {}, false).sort((a, b) =>
8180
localeCompare(a?.reportName?.toLowerCase() ?? '', b?.reportName?.toLowerCase() ?? ''),
8281
);
8382

src/components/MoneyRequestConfirmationList/sections/selectors.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** Onyx selectors used by the confirmation field leaves. */
22
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
33
import {canSendInvoice} from '@libs/PolicyUtils';
4-
import getReportNameValuePairsForReports from '@libs/ReportNameValuePairsUtils';
54
import {
65
getCategory,
76
getCreated,
@@ -302,7 +301,6 @@ type ReportFieldTransactionState = {
302301
isFromGlobalCreate: boolean;
303302
participantReportID: string | undefined;
304303
};
305-
type OutstandingReportsForPolicy = OnyxTypes.OutstandingReportsByPolicyIDDerivedValue[string];
306304

307305
const reportFieldTransactionStateSelector = (t: OnyxEntry<Transaction>): ReportFieldTransactionState | undefined => {
308306
if (!t) {
@@ -318,15 +316,6 @@ const reportFieldTransactionStateSelector = (t: OnyxEntry<Transaction>): ReportF
318316
const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry<OnyxTypes.OutstandingReportsByPolicyIDDerivedValue>) =>
319317
derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID];
320318

321-
const createOutstandingReportsNVPsSelector =
322-
(outstandingReports: OutstandingReportsForPolicy | undefined) =>
323-
(allNVPs: OnyxCollection<OnyxTypes.ReportNameValuePairs>): OnyxCollection<OnyxTypes.ReportNameValuePairs> | undefined => {
324-
if (!outstandingReports || !allNVPs) {
325-
return undefined;
326-
}
327-
return getReportNameValuePairsForReports(outstandingReports, allNVPs);
328-
};
329-
330319
// --- InvoiceSenderField ---
331320

332321
type InvoiceSenderWorkspace = {id: string | undefined; name: string | undefined; avatarURL: string | undefined} | undefined;
@@ -350,7 +339,6 @@ export {
350339
attendeeSliceSelector,
351340
categoryStateSelector,
352341
createCanUpdateSenderWorkspaceSelector,
353-
createOutstandingReportsNVPsSelector,
354342
createOutstandingReportsForPolicySelector,
355343
createTagDisplaySelector,
356344
dateStateSelector,

src/components/Search/index.tsx

Lines changed: 98 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {SelectionListHandle} from '@components/SelectionList/types';
1212
import SearchRowSkeleton from '@components/Skeletons/SearchRowSkeleton';
1313
import {useWideRHPActions} from '@components/WideRHPContextProvider';
1414
import useActionLoadingReportIDs from '@hooks/useActionLoadingReportIDs';
15-
import useArchivedReportsIdSet from '@hooks/useArchivedReportsIdSet';
15+
import useArchivedReportsIDSet from '@hooks/useArchivedReportsIDSet';
1616
import {useCurrencyListActions} from '@hooks/useCurrencyList';
1717
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
1818
import useEnvironment from '@hooks/useEnvironment';
@@ -67,6 +67,7 @@ import {
6767
shouldShowEmptyState,
6868
shouldShowYear as shouldShowYearUtil,
6969
} from '@libs/SearchUIUtils';
70+
import type {ArchivedReportsIDSet} from '@libs/SearchUIUtils';
7071
import {cancelSpan, endSpanWithAttributes, getSpan, startSpan} from '@libs/telemetry/activeSpans';
7172
import {
7273
cancelNavigateToReportsSpans,
@@ -126,18 +127,33 @@ type HoldMenuCallback = (item: TransactionReportGroupListItemType, requestType:
126127

127128
const hashToString = (queryHash?: number) => (queryHash || queryHash === 0 ? String(queryHash) : undefined);
128129

129-
function mapTransactionItemToSelectedEntry(
130-
item: TransactionListItemType,
131-
itemTransaction: OnyxEntry<Transaction>,
132-
originalItemTransaction: OnyxEntry<Transaction>,
133-
currentUserLogin: string,
134-
currentUserAccountID: number,
135-
outstandingReportsByPolicyID: OutstandingReportsByPolicyIDDerivedValue | undefined,
136-
allowNegativeAmount: boolean,
137-
parentReport: OnyxEntry<Report> | undefined,
138-
selfDMReport: OnyxEntry<Report> | undefined,
139-
isProduction: boolean,
140-
): [string, SelectedTransactionInfo] {
130+
type MapTransactionItemToSelectedEntryParams = {
131+
item: TransactionListItemType;
132+
itemTransaction: OnyxEntry<Transaction>;
133+
originalItemTransaction: OnyxEntry<Transaction>;
134+
currentUserLogin: string;
135+
currentUserAccountID: number;
136+
archivedReportsIDSet: ArchivedReportsIDSet;
137+
outstandingReportsByPolicyID: OutstandingReportsByPolicyIDDerivedValue | undefined;
138+
selfDMReport: OnyxEntry<Report>;
139+
isProduction: boolean;
140+
allowNegativeAmount: boolean;
141+
parentReport: OnyxEntry<Report> | undefined;
142+
};
143+
144+
function mapTransactionItemToSelectedEntry({
145+
item,
146+
itemTransaction,
147+
originalItemTransaction,
148+
currentUserLogin,
149+
currentUserAccountID,
150+
archivedReportsIDSet,
151+
outstandingReportsByPolicyID,
152+
selfDMReport,
153+
isProduction,
154+
allowNegativeAmount,
155+
parentReport,
156+
}: MapTransactionItemToSelectedEntryParams): [string, SelectedTransactionInfo] {
141157
const {canHoldRequest, canUnholdRequest} = canHoldUnholdReportAction(item.report, item.reportAction, item.holdReportAction, item, item.policy, currentUserAccountID);
142158
const canRejectRequest = item.report ? canRejectReportAction(currentUserLogin, item.report) : false;
143159
const amount = hasValidModifiedAmount(item) ? Number(item.modifiedAmount) : item.amount;
@@ -162,6 +178,7 @@ function mapTransactionItemToSelectedEntry(
162178
transaction: item,
163179
report: item.report,
164180
policy: item.policy,
181+
archivedReportsIDSet,
165182
}),
166183
action: item.action,
167184
groupCurrency: item.groupCurrency,
@@ -229,36 +246,52 @@ function mapEmptyReportToSelectedEntry(item: TransactionReportGroupListItemType
229246
];
230247
}
231248

232-
function prepareTransactionsList(
233-
item: TransactionListItemType,
234-
itemTransaction: OnyxEntry<Transaction>,
235-
originalItemTransaction: OnyxEntry<Transaction>,
236-
selectedTransactions: SelectedTransactions,
237-
currentUserLogin: string,
238-
currentUserAccountID: number,
239-
outstandingReportsByPolicyID: OutstandingReportsByPolicyIDDerivedValue | undefined,
240-
parentReport: OnyxEntry<Report> | undefined,
241-
selfDMReport: OnyxEntry<Report> | undefined,
242-
isProduction: boolean,
243-
) {
249+
type PrepareTransactionsListParams = {
250+
item: TransactionListItemType;
251+
itemTransaction: OnyxEntry<Transaction>;
252+
originalItemTransaction: OnyxEntry<Transaction>;
253+
selectedTransactions: SelectedTransactions;
254+
currentUserLogin: string;
255+
currentUserAccountID: number;
256+
archivedReportsIDSet: ArchivedReportsIDSet;
257+
outstandingReportsByPolicyID: OutstandingReportsByPolicyIDDerivedValue | undefined;
258+
selfDMReport: OnyxEntry<Report>;
259+
isProduction: boolean;
260+
parentReport: OnyxEntry<Report> | undefined;
261+
};
262+
263+
function prepareTransactionsList({
264+
item,
265+
itemTransaction,
266+
originalItemTransaction,
267+
selectedTransactions,
268+
currentUserLogin,
269+
currentUserAccountID,
270+
archivedReportsIDSet,
271+
outstandingReportsByPolicyID,
272+
selfDMReport,
273+
isProduction,
274+
parentReport,
275+
}: PrepareTransactionsListParams) {
244276
if (selectedTransactions[item.keyForList]?.isSelected) {
245277
const {[item.keyForList]: omittedTransaction, ...transactions} = selectedTransactions;
246278

247279
return transactions;
248280
}
249281

250-
const [key, selectedInfo] = mapTransactionItemToSelectedEntry(
282+
const [key, selectedInfo] = mapTransactionItemToSelectedEntry({
251283
item,
252284
itemTransaction,
253285
originalItemTransaction,
254286
currentUserLogin,
255287
currentUserAccountID,
288+
archivedReportsIDSet,
256289
outstandingReportsByPolicyID,
257-
false,
258-
parentReport,
259290
selfDMReport,
260291
isProduction,
261-
);
292+
allowNegativeAmount: false,
293+
parentReport,
294+
});
262295

263296
return {
264297
...selectedTransactions,
@@ -333,7 +366,7 @@ function Search({
333366

334367
const isExpenseReportType = type === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT;
335368

336-
const archivedReportsIdSet = useArchivedReportsIdSet();
369+
const archivedReportsIDSet = useArchivedReportsIDSet();
337370

338371
const [exportReportActions] = useOnyx<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS, Record<string, ReportAction[]> | undefined>(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {
339372
selector: selectFilteredReportActions,
@@ -566,7 +599,7 @@ function Search({
566599
groupBy: validGroupBy,
567600
reportActions: exportReportActions,
568601
currentSearch: currentSearchKey,
569-
archivedReportsIDList: archivedReportsIdSet,
602+
archivedReportsIDList: archivedReportsIDSet,
570603
queryJSON,
571604
isActionLoadingSet,
572605
cardFeeds,
@@ -597,7 +630,7 @@ function Search({
597630
searchDataWithOptimisticTransaction,
598631
searchResults,
599632
type,
600-
archivedReportsIdSet,
633+
archivedReportsIDSet,
601634
translate,
602635
formatPhoneNumber,
603636
accountID,
@@ -1062,18 +1095,19 @@ function Search({
10621095
const itemTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${item.transactionID}`] as OnyxEntry<Transaction>;
10631096
const originalItemTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${itemTransaction?.comment?.originalTransactionID}`];
10641097
const itemParentReport = searchResults?.data?.[`${ONYXKEYS.COLLECTION.REPORT}${item.report?.parentReportID}`] as OnyxEntry<Report>;
1065-
const updatedTransactions = prepareTransactionsList(
1098+
const updatedTransactions = prepareTransactionsList({
10661099
item,
10671100
itemTransaction,
10681101
originalItemTransaction,
10691102
selectedTransactions,
1070-
email ?? '',
1071-
accountID,
1103+
currentUserLogin: email ?? '',
1104+
currentUserAccountID: accountID,
1105+
archivedReportsIDSet,
10721106
outstandingReportsByPolicyID,
1073-
itemParentReport,
10741107
selfDMReport,
10751108
isProduction,
1076-
);
1109+
parentReport: itemParentReport,
1110+
});
10771111

10781112
// Tag individual transactions with their parent group key so export filtering can derive the group when needed.
10791113
if (areItemsGrouped) {
@@ -1145,18 +1179,19 @@ function Search({
11451179
searchResults?.data?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${itemTransaction?.comment?.originalTransactionID}`] ??
11461180
transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${itemTransaction?.comment?.originalTransactionID}`];
11471181
const itemParentReport = searchResults?.data?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionItem.report?.parentReportID}`] as OnyxEntry<Report>;
1148-
const [key, entry] = mapTransactionItemToSelectedEntry(
1149-
transactionItem,
1182+
const [key, entry] = mapTransactionItemToSelectedEntry({
1183+
item: transactionItem,
11501184
itemTransaction,
11511185
originalItemTransaction,
1152-
email ?? '',
1153-
accountID,
1186+
currentUserLogin: email ?? '',
1187+
currentUserAccountID: accountID,
1188+
archivedReportsIDSet,
11541189
outstandingReportsByPolicyID,
1155-
true,
1156-
itemParentReport,
11571190
selfDMReport,
11581191
isProduction,
1159-
);
1192+
allowNegativeAmount: true,
1193+
parentReport: itemParentReport,
1194+
});
11601195
return [key, {...entry, groupKey: item.keyForList}];
11611196
}),
11621197
),
@@ -1172,6 +1207,7 @@ function Search({
11721207
searchResults?.data,
11731208
email,
11741209
accountID,
1210+
archivedReportsIDSet,
11751211
outstandingReportsByPolicyID,
11761212
selfDMReport,
11771213
isProduction,
@@ -1492,18 +1528,19 @@ function Search({
14921528
const itemTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`] as OnyxEntry<Transaction>;
14931529
const originalItemTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${itemTransaction?.comment?.originalTransactionID}`];
14941530
const itemParentReport = searchResults?.data?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionItem.report?.parentReportID}`] as OnyxEntry<Report>;
1495-
const [key, entry] = mapTransactionItemToSelectedEntry(
1496-
transactionItem,
1531+
const [key, entry] = mapTransactionItemToSelectedEntry({
1532+
item: transactionItem,
14971533
itemTransaction,
14981534
originalItemTransaction,
1499-
email ?? '',
1500-
accountID,
1535+
currentUserLogin: email ?? '',
1536+
currentUserAccountID: accountID,
1537+
archivedReportsIDSet,
15011538
outstandingReportsByPolicyID,
1502-
true,
1503-
itemParentReport,
15041539
selfDMReport,
15051540
isProduction,
1506-
);
1541+
allowNegativeAmount: true,
1542+
parentReport: itemParentReport,
1543+
});
15071544
return [key, {...entry, groupKey: item.keyForList}] as [string, SelectedTransactionInfo];
15081545
});
15091546
});
@@ -1517,18 +1554,19 @@ function Search({
15171554
const itemTransaction = searchResults?.data?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`] as OnyxEntry<Transaction>;
15181555
const originalItemTransaction = searchResults?.data?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${itemTransaction?.comment?.originalTransactionID}`];
15191556
const itemParentReport = searchResults?.data?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionItem.report?.parentReportID}`] as OnyxEntry<Report>;
1520-
return mapTransactionItemToSelectedEntry(
1521-
transactionItem,
1557+
return mapTransactionItemToSelectedEntry({
1558+
item: transactionItem,
15221559
itemTransaction,
15231560
originalItemTransaction,
1524-
email ?? '',
1525-
accountID,
1561+
currentUserLogin: email ?? '',
1562+
currentUserAccountID: accountID,
1563+
archivedReportsIDSet,
15261564
outstandingReportsByPolicyID,
1527-
true,
1528-
itemParentReport,
15291565
selfDMReport,
15301566
isProduction,
1531-
);
1567+
allowNegativeAmount: true,
1568+
parentReport: itemParentReport,
1569+
});
15321570
}),
15331571
);
15341572
}
@@ -1542,10 +1580,11 @@ function Search({
15421580
updateSelectAllMatchingItemsState,
15431581
clearSelectedTransactions,
15441582
transactions,
1583+
searchResults?.data,
15451584
email,
15461585
accountID,
1586+
archivedReportsIDSet,
15471587
outstandingReportsByPolicyID,
1548-
searchResults?.data,
15491588
selfDMReport,
15501589
isProduction,
15511590
]);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {buildArchivedReportsIDSet} from '@libs/ReportUtils';
2+
import type {ArchivedReportsIDSet} from '@libs/SearchUIUtils';
3+
import ONYXKEYS from '@src/ONYXKEYS';
4+
import useOnyx from './useOnyx';
5+
6+
/**
7+
* Hook that returns a Set of archived report IDs
8+
*/
9+
function useArchivedReportsIDSet(): ArchivedReportsIDSet {
10+
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS);
11+
12+
return buildArchivedReportsIDSet(reportNameValuePairs);
13+
}
14+
15+
export default useArchivedReportsIDSet;

0 commit comments

Comments
 (0)