From cd23b3f834accb5992bb9966ccfc6e1ac23c8b7e Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Tue, 10 Feb 2026 17:14:33 +0100 Subject: [PATCH 1/3] avoid recreating policies array on each call --- src/libs/ReportUtils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d362f99098358..3e8e935dcc93d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1040,12 +1040,14 @@ Onyx.connect({ let allPolicies: OnyxCollection; let hasPolicies: boolean; +let policiesArray: Policy[]; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, callback: (value) => { allPolicies = value; hasPolicies = !isEmptyObject(value); + policiesArray = Object.values(value); }, }); @@ -4849,7 +4851,7 @@ function canEditFieldOfMoneyRequest( // Check if there are multiple outstanding reports across policies let outstandingReportsCount = 0; - for (const currentPolicy of Object.values(allPolicies ?? {})) { + for (const currentPolicy of policiesArray) { const reports = getOutstandingReportsForUser( currentPolicy?.id, moneyRequestReport?.ownerAccountID, @@ -5932,7 +5934,7 @@ function getReportSubtitlePrefix(report: OnyxEntry): string { } let policyCount = 0; - for (const policy of Object.values(allPolicies ?? {})) { + for (const policy of policiesArray) { if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { continue; } @@ -11051,7 +11053,7 @@ function createDraftTransactionAndNavigateToParticipantSelector( let firstPolicy: Policy | undefined; let filteredPoliciesCount = 0; - for (const policy of Object.values(allPolicies ?? {})) { + for (const policy of policiesArray) { if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { continue; } From 1a1401ed87f6571ce2aef76d03143033445f7a37 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Tue, 10 Feb 2026 17:39:50 +0100 Subject: [PATCH 2/3] fix types and remove unnecessary logic --- src/libs/ReportUtils.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3e8e935dcc93d..82844b3d79444 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1047,7 +1047,7 @@ Onyx.connect({ callback: (value) => { allPolicies = value; hasPolicies = !isEmptyObject(value); - policiesArray = Object.values(value); + policiesArray = Object.values(value ?? {}).filter((policy): policy is Policy => !!policy); }, }); @@ -4852,11 +4852,7 @@ function canEditFieldOfMoneyRequest( // Check if there are multiple outstanding reports across policies let outstandingReportsCount = 0; for (const currentPolicy of policiesArray) { - const reports = getOutstandingReportsForUser( - currentPolicy?.id, - moneyRequestReport?.ownerAccountID, - outstandingReportsByPolicyID?.[currentPolicy?.id ?? CONST.DEFAULT_NUMBER_ID] ?? {}, - ); + const reports = getOutstandingReportsForUser(currentPolicy.id, moneyRequestReport?.ownerAccountID, outstandingReportsByPolicyID?.[currentPolicy?.id] ?? {}); outstandingReportsCount += reports.length; // Short-circuit once we find more than 1 @@ -5935,7 +5931,7 @@ function getReportSubtitlePrefix(report: OnyxEntry): string { let policyCount = 0; for (const policy of policiesArray) { - if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { + if (!shouldShowPolicy(policy, false, currentUserEmail)) { continue; } From 05811fd3b1d7c2fc59e120159742c6d10db9748b Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Tue, 10 Feb 2026 17:49:17 +0100 Subject: [PATCH 3/3] address comments --- src/libs/ReportUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 82844b3d79444..c0687aa497d39 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1040,7 +1040,7 @@ Onyx.connect({ let allPolicies: OnyxCollection; let hasPolicies: boolean; -let policiesArray: Policy[]; +let policiesArray: Policy[] = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, @@ -11050,7 +11050,7 @@ function createDraftTransactionAndNavigateToParticipantSelector( let firstPolicy: Policy | undefined; let filteredPoliciesCount = 0; for (const policy of policiesArray) { - if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { + if (!shouldShowPolicy(policy, false, currentUserEmail)) { continue; }