From 62e9cf4d7e157abc2f5bd7b84c2cfd26337c66b9 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 28 May 2026 15:08:18 +0200 Subject: [PATCH 1/3] perf: narrow REPORT_NAME_VALUE_PAIRS subscription in ReportField --- .../sections/ReportField.tsx | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx index 836390bd2c92..ef5098799aef 100644 --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; @@ -16,9 +16,27 @@ import ROUTES from '@src/ROUTES'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; +type OutstandingReportsForPolicy = OnyxTypes.OutstandingReportsByPolicyIDDerivedValue[string]; + const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry) => derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID]; +const createOutstandingReportsNVPsSelector = + (outstandingReports: OutstandingReportsForPolicy | undefined) => + (allNVPs: OnyxCollection): OnyxCollection | undefined => { + if (!outstandingReports || !allNVPs) { + return undefined; + } + const result: OnyxCollection = {}; + for (const reportID of Object.keys(outstandingReports)) { + const key = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}` as const; + if (allNVPs[key] !== undefined) { + result[key] = allNVPs[key]; + } + } + return result; + }; + type ReportFieldProps = { /** The selected participants */ selectedParticipants: Participant[]; @@ -54,8 +72,10 @@ function ReportField({selectedParticipants, iouType, reportID, reportActionID, a const reportAttributes = useReportAttributes(); const policyID = selectedParticipants?.at(0)?.policyID; - const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS); const [outstandingReportsForPolicy] = useOnyx(ONYXKEYS.DERIVED.OUTSTANDING_REPORTS_BY_POLICY_ID, {selector: createOutstandingReportsForPolicySelector(policyID)}, [policyID]); + const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {selector: createOutstandingReportsNVPsSelector(outstandingReportsForPolicy)}, [ + outstandingReportsForPolicy, + ]); // Per-key report subscriptions instead of full COLLECTION.REPORT const [transactionReportEntry] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`); From 2f1be2d675ed70b0861de8eb614e6d034f484c05 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 28 May 2026 15:49:41 +0200 Subject: [PATCH 2/3] move helper to selectors --- .../sections/ReportField.tsx | 24 ++----------------- .../sections/selectors.ts | 22 +++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx index ef5098799aef..a1c2505aa0b8 100644 --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; @@ -15,27 +15,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; - -type OutstandingReportsForPolicy = OnyxTypes.OutstandingReportsByPolicyIDDerivedValue[string]; - -const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry) => - derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID]; - -const createOutstandingReportsNVPsSelector = - (outstandingReports: OutstandingReportsForPolicy | undefined) => - (allNVPs: OnyxCollection): OnyxCollection | undefined => { - if (!outstandingReports || !allNVPs) { - return undefined; - } - const result: OnyxCollection = {}; - for (const reportID of Object.keys(outstandingReports)) { - const key = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}` as const; - if (allNVPs[key] !== undefined) { - result[key] = allNVPs[key]; - } - } - return result; - }; +import {createOutstandingReportsForPolicySelector, createOutstandingReportsNVPsSelector} from './selectors'; type ReportFieldProps = { /** The selected participants */ diff --git a/src/components/MoneyRequestConfirmationList/sections/selectors.ts b/src/components/MoneyRequestConfirmationList/sections/selectors.ts index ecd6cf509c63..1d3b4bfa6587 100644 --- a/src/components/MoneyRequestConfirmationList/sections/selectors.ts +++ b/src/components/MoneyRequestConfirmationList/sections/selectors.ts @@ -14,6 +14,7 @@ import { willFieldBeAutomaticallyFilled, } from '@libs/TransactionUtils'; import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; @@ -194,6 +195,7 @@ const taxSliceSelector = (t: OnyxEntry): TaxSlice | undefined => { // --- ReportField --- type ReportFieldTransactionState = {reportID: Transaction['reportID']; isFromGlobalCreate: boolean}; +type OutstandingReportsForPolicy = OnyxTypes.OutstandingReportsByPolicyIDDerivedValue[string]; const reportFieldTransactionStateSelector = (t: OnyxEntry): ReportFieldTransactionState | undefined => { if (!t) { @@ -208,6 +210,25 @@ const reportFieldTransactionStateSelector = (t: OnyxEntry): ReportF const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry) => derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID]; +const createOutstandingReportsNVPsSelector = + (outstandingReports: OutstandingReportsForPolicy | undefined) => + (allNVPs: OnyxCollection): OnyxCollection | undefined => { + if (!outstandingReports || !allNVPs) { + return undefined; + } + const result: OnyxCollection = {}; + for (const report of Object.values(outstandingReports)) { + if (!report?.reportID) { + continue; + } + const key = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}` as const; + if (allNVPs[key] !== undefined) { + result[key] = allNVPs[key]; + } + } + return result; + }; + // --- InvoiceSenderField --- type InvoiceSenderWorkspace = {id: string | undefined; name: string | undefined; avatarURL: string | undefined} | undefined; @@ -231,6 +252,7 @@ export { attendeeSliceSelector, categoryStateSelector, createCanUpdateSenderWorkspaceSelector, + createOutstandingReportsNVPsSelector, createOutstandingReportsForPolicySelector, createTagDisplaySelector, dateStateSelector, From 74cecf63555f4ec11ba1741c3c0fb2e009d6cd4d Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 28 May 2026 16:52:40 +0200 Subject: [PATCH 3/3] scope outstanding report NVP selectors --- .../sections/selectors.ts | 14 +----- src/hooks/useOutstandingReports.ts | 43 +++++++++++++++++-- src/libs/ReportNameValuePairsUtils.ts | 19 ++++++++ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/libs/ReportNameValuePairsUtils.ts diff --git a/src/components/MoneyRequestConfirmationList/sections/selectors.ts b/src/components/MoneyRequestConfirmationList/sections/selectors.ts index 1d3b4bfa6587..82b8218d3127 100644 --- a/src/components/MoneyRequestConfirmationList/sections/selectors.ts +++ b/src/components/MoneyRequestConfirmationList/sections/selectors.ts @@ -1,6 +1,7 @@ /** Onyx selectors used by the confirmation field leaves. */ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {canSendInvoice} from '@libs/PolicyUtils'; +import getReportNameValuePairsForReports from '@libs/ReportNameValuePairsUtils'; import { getCategory, getCreated, @@ -14,7 +15,6 @@ import { willFieldBeAutomaticallyFilled, } from '@libs/TransactionUtils'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; @@ -216,17 +216,7 @@ const createOutstandingReportsNVPsSelector = if (!outstandingReports || !allNVPs) { return undefined; } - const result: OnyxCollection = {}; - for (const report of Object.values(outstandingReports)) { - if (!report?.reportID) { - continue; - } - const key = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}` as const; - if (allNVPs[key] !== undefined) { - result[key] = allNVPs[key]; - } - } - return result; + return getReportNameValuePairsForReports(outstandingReports, allNVPs); }; // --- InvoiceSenderField --- diff --git a/src/hooks/useOutstandingReports.ts b/src/hooks/useOutstandingReports.ts index b23b255bf3b7..43bce9cbcd09 100644 --- a/src/hooks/useOutstandingReports.ts +++ b/src/hooks/useOutstandingReports.ts @@ -1,27 +1,60 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import getReportNameValuePairsForReports from '@libs/ReportNameValuePairsUtils'; import {getOutstandingReportsForUser, isSelfDM} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy} from '@src/types/onyx'; +import type {OutstandingReportsByPolicyIDDerivedValue, Policy, ReportNameValuePairs} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import useMappedPolicies from './useMappedPolicies'; import useOnyx from './useOnyx'; const policyIdMapper = (policy: OnyxEntry) => policy?.id; +const createOutstandingReportsNVPsSelector = + ( + outstandingReportsByPolicyID: OnyxEntry, + selectedPolicyID: string | undefined, + personalPolicyID: string | undefined, + allPoliciesID: OnyxCollection, + shouldUseAllPolicies: boolean, + ) => + (allNVPs: OnyxCollection): OnyxCollection | undefined => { + if (!outstandingReportsByPolicyID || !allNVPs) { + return undefined; + } + + const result: OnyxCollection = {}; + if (shouldUseAllPolicies) { + for (const policyID of Object.values(allPoliciesID ?? {})) { + if (!policyID || policyID === personalPolicyID) { + continue; + } + Object.assign(result, getReportNameValuePairsForReports(outstandingReportsByPolicyID[policyID], allNVPs)); + } + return result; + } + + return getReportNameValuePairsForReports(outstandingReportsByPolicyID[selectedPolicyID ?? CONST.DEFAULT_NUMBER_ID], allNVPs); + }; + export default function useOutstandingReports(selectedReportID: string | undefined, selectedPolicyID: string | undefined, ownerAccountID: number | undefined, isEditing: boolean) { const [outstandingReportsByPolicyID] = useOnyx(ONYXKEYS.DERIVED.OUTSTANDING_REPORTS_BY_POLICY_ID); const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID); const [allPoliciesID] = useMappedPolicies(policyIdMapper); - const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS); const [selectedReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${selectedReportID}`); + const shouldUseAllPolicies = !selectedPolicyID || selectedPolicyID === personalPolicyID || isSelfDM(selectedReport); + const [reportNameValuePairs] = useOnyx( + ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + {selector: createOutstandingReportsNVPsSelector(outstandingReportsByPolicyID, selectedPolicyID, personalPolicyID, allPoliciesID, shouldUseAllPolicies)}, + [outstandingReportsByPolicyID, selectedPolicyID, personalPolicyID, allPoliciesID, shouldUseAllPolicies], + ); // Early return if no reports are available to prevent useless loop if (!outstandingReportsByPolicyID || isEmptyObject(outstandingReportsByPolicyID)) { return []; } - if (!selectedPolicyID || selectedPolicyID === personalPolicyID || isSelfDM(selectedReport)) { + if (shouldUseAllPolicies) { const result = []; for (const policyID of Object.values(allPoliciesID ?? {})) { if (!policyID || policyID === personalPolicyID) { @@ -36,3 +69,5 @@ export default function useOutstandingReports(selectedReportID: string | undefin return getOutstandingReportsForUser(selectedPolicyID, ownerAccountID, outstandingReportsByPolicyID?.[selectedPolicyID ?? CONST.DEFAULT_NUMBER_ID] ?? {}, reportNameValuePairs, isEditing); } + +export {createOutstandingReportsNVPsSelector}; diff --git a/src/libs/ReportNameValuePairsUtils.ts b/src/libs/ReportNameValuePairsUtils.ts new file mode 100644 index 000000000000..cd06cbe79767 --- /dev/null +++ b/src/libs/ReportNameValuePairsUtils.ts @@ -0,0 +1,19 @@ +import type {OnyxCollection} from 'react-native-onyx'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, ReportNameValuePairs} from '@src/types/onyx'; + +function getReportNameValuePairsForReports(reports: OnyxCollection | undefined, allNVPs: NonNullable>): OnyxCollection { + const result: OnyxCollection = {}; + for (const report of Object.values(reports ?? {})) { + if (!report?.reportID) { + continue; + } + const key = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}` as const; + if (allNVPs[key] !== undefined) { + result[key] = allNVPs[key]; + } + } + return result; +} + +export default getReportNameValuePairsForReports;