diff --git a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx index 836390bd2c92..a1c2505aa0b8 100644 --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx @@ -15,9 +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'; - -const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry) => - derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID]; +import {createOutstandingReportsForPolicySelector, createOutstandingReportsNVPsSelector} from './selectors'; type ReportFieldProps = { /** The selected participants */ @@ -54,8 +52,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}`); diff --git a/src/components/MoneyRequestConfirmationList/sections/selectors.ts b/src/components/MoneyRequestConfirmationList/sections/selectors.ts index ecd6cf509c63..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, @@ -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,15 @@ 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; + } + return getReportNameValuePairsForReports(outstandingReports, allNVPs); + }; + // --- InvoiceSenderField --- type InvoiceSenderWorkspace = {id: string | undefined; name: string | undefined; avatarURL: string | undefined} | undefined; @@ -231,6 +242,7 @@ export { attendeeSliceSelector, categoryStateSelector, createCanUpdateSenderWorkspaceSelector, + createOutstandingReportsNVPsSelector, createOutstandingReportsForPolicySelector, createTagDisplaySelector, dateStateSelector, 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;