Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/hooks/useParticipantsPolicies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {OnyxCollection} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import {getEmptyObject} from '@src/types/utils/EmptyObject';
import useOnyx from './useOnyx';

type ParticipantWithPolicyID = {
policyID?: string;
};

function getPoliciesSelector(participants: ParticipantWithPolicyID[]): (allPolicies: OnyxCollection<Policy>) => Record<string, Policy> {
return (allPolicies: OnyxCollection<Policy>) => {
if (!participants) {
return {};
}
return participants.reduce<Record<string, Policy>>((acc, participant) => {
const key = `${ONYXKEYS.COLLECTION.POLICY}${participant.policyID}`;
if (allPolicies?.[key] && participant.policyID) {
acc[participant.policyID] = allPolicies[key];
}
return acc;
}, {});
};
}

/**
* Hook that extracts policies only for participants' policies.
*
* @param participants - Array of participants with optional policyID
* @returns Record mapping policyID to Policy
*/
function useParticipantsPolicies(participants: ParticipantWithPolicyID[]): Record<string, Policy> {
const [participantsPolicies = getEmptyObject<Record<string, Policy>>()] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: getPoliciesSelector(participants)}, [participants]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ PERF-11 (docs)

The selector on ONYXKEYS.COLLECTION.POLICY returns Record<string, Policy> containing full Policy objects. Since useOnyx runs deepEqual on selector output, it must deep-compare entire Policy objects on every Onyx update to the policy collection. Policy objects can be large, making this comparison expensive.

Consider one of these approaches:

  1. Narrow the selector output to only the fields actually needed downstream (e.g., name, type, id), so deepEqual compares a small object per entry.
  2. Remove the selector entirely and subscribe without one (using cheaper shallowEqual), then filter/transform inline:
function useParticipantsPolicies(participants: ParticipantWithPolicyID[]): Record<string, Policy> {
    const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);

    // Filter inline — shallowEqual on raw references is cheaper than deepEqual on full Policy objects
    const participantsPolicies = participants.reduce<Record<string, Policy>>((acc, participant) => {
        const key = `${ONYXKEYS.COLLECTION.POLICY}${participant.policyID}`;
        if (allPolicies?.[key] && participant.policyID) {
            acc[participant.policyID] = allPolicies[key];
        }
        return acc;
    }, {});

    return participantsPolicies;
}

Reviewed at: 12e1ee8 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.


return participantsPolicies;
}

export default useParticipantsPolicies;
8 changes: 6 additions & 2 deletions src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useNetwork from '@hooks/useNetwork';
import useOdometerReceiptStitcher from '@hooks/useOdometerReceiptStitcher';
import useOnyx from '@hooks/useOnyx';
import useOptimisticDraftTransactions from '@hooks/useOptimisticDraftTransactions';
import useParticipantsPolicies from '@hooks/useParticipantsPolicies';
import usePermissions from '@hooks/usePermissions';
import usePolicyForTransaction from '@hooks/usePolicyForTransaction';
import usePrivateIsArchivedMap from '@hooks/usePrivateIsArchivedMap';
Expand Down Expand Up @@ -247,6 +248,8 @@ function IOURequestStepConfirmation({
setMoneyRequestParticipantsFromReport(transaction.transactionID, transactionReport, currentUserPersonalDetails.accountID);
}, [transactionReport, currentUserPersonalDetails.accountID, transaction?.transactionID, iouType]);

const participantsPolicies = useParticipantsPolicies(transaction?.participants ?? []);

const participants = useMemo(
() =>
transaction?.participants?.map((participant) => {
Expand All @@ -255,11 +258,12 @@ function IOURequestStepConfirmation({
}
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${participant.reportID}`];
const participantReportDraft = reportDrafts?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${participant.reportID}`];
const participantPolicy = participant.policyID ? participantsPolicies[participant.policyID] : policy;
return participant.accountID
? getParticipantsOption(participant, personalDetails)
: getReportOption(participant, privateIsArchived, policy, personalDetails, conciergeReportID, reportAttributesDerived, participantReportDraft);
: getReportOption(participant, privateIsArchived, participantPolicy, personalDetails, conciergeReportID, reportAttributesDerived, participantReportDraft);
}) ?? [],
[transaction?.participants, iouType, personalDetails, reportAttributesDerived, privateIsArchivedMap, policy, conciergeReportID, reportDrafts],
[transaction?.participants, iouType, personalDetails, reportAttributesDerived, privateIsArchivedMap, participantsPolicies, policy, conciergeReportID, reportDrafts],
);

const sourceReportID = transaction?.reportID ?? reportID;
Expand Down
Loading