Skip to content

Commit 04a89d4

Browse files
authored
Merge pull request Expensify#66053 from callstack-internal/perf/improve-canEditFieldOfMoneyRequest
perf: use reports by policy ID in canEditFieldOfMoneyRequest
2 parents 8add48d + c002eae commit 04a89d4

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ type GetReportNameParams = {
863863
policies?: SearchPolicy[];
864864
};
865865

866-
type ReportByPolicyMap = Record<string, Report[]>;
866+
type ReportByPolicyMap = Record<string, OnyxCollection<Report>>;
867867

868868
let currentUserEmail: string | undefined;
869869
let currentUserPrivateDomain: string | undefined;
@@ -949,7 +949,7 @@ Onyx.connect({
949949
return;
950950
}
951951

952-
reportsByPolicyID = Object.values(value).reduce<ReportByPolicyMap>((acc, report) => {
952+
reportsByPolicyID = Object.entries(value).reduce<ReportByPolicyMap>((acc, [reportID, report]) => {
953953
if (!report) {
954954
return acc;
955955
}
@@ -962,9 +962,13 @@ Onyx.connect({
962962
// - Belong to the same workspace
963963
if (report.policyID && report.ownerAccountID === currentUserAccountID && (report.stateNum ?? 0) <= 1) {
964964
if (!acc[report.policyID]) {
965-
acc[report.policyID] = [];
965+
acc[report.policyID] = {};
966966
}
967-
acc[report.policyID].push(report);
967+
968+
acc[report.policyID] = {
969+
...acc[report.policyID],
970+
[reportID]: report,
971+
};
968972
}
969973

970974
return acc;
@@ -4228,9 +4232,12 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>
42284232
// Unreported transaction from OldDot can have the reportID as an empty string
42294233
const isUnreported = !transaction?.reportID || transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;
42304234
return isUnreported
4231-
? Object.values(allPolicies ?? {}).flatMap((currentPolicy) => getOutstandingReportsForUser(currentPolicy?.id, currentUserAccountID, allReports ?? {})).length > 0
4232-
: Object.values(allPolicies ?? {}).flatMap((currentPolicy) => getOutstandingReportsForUser(currentPolicy?.id, moneyRequestReport?.ownerAccountID, allReports ?? {})).length > 1 ||
4233-
isOwner;
4235+
? Object.values(allPolicies ?? {}).flatMap((currentPolicy) =>
4236+
getOutstandingReportsForUser(currentPolicy?.id, currentUserAccountID, reportsByPolicyID?.[currentPolicy?.id ?? CONST.DEFAULT_NUMBER_ID] ?? {}),
4237+
).length > 0
4238+
: Object.values(allPolicies ?? {}).flatMap((currentPolicy) =>
4239+
getOutstandingReportsForUser(currentPolicy?.id, moneyRequestReport?.ownerAccountID, reportsByPolicyID?.[currentPolicy?.id ?? CONST.DEFAULT_NUMBER_ID] ?? {}),
4240+
).length > 1 || isOwner;
42344241
}
42354242

42364243
return true;
@@ -7932,8 +7939,12 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
79327939
}
79337940

79347941
// If any report has a violation, then it should have a RBR
7935-
const potentialReports = reportsByPolicyID[report.policyID] ?? [];
7942+
const potentialReports = Object.values(reportsByPolicyID[report.policyID] ?? {}) ?? [];
79367943
return potentialReports.some((potentialReport) => {
7944+
if (!potentialReport) {
7945+
return false;
7946+
}
7947+
79377948
return (
79387949
!isInvoiceReport(potentialReport) &&
79397950
(hasViolations(potentialReport.reportID, transactionViolations, true) ||

0 commit comments

Comments
 (0)