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
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnyxTypes.OutstandingReportsByPolicyIDDerivedValue>) =>
derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID];
import {createOutstandingReportsForPolicySelector, createOutstandingReportsNVPsSelector} from './selectors';

type ReportFieldProps = {
/** The selected participants */
Expand Down Expand Up @@ -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,
]);
Comment on lines +56 to +58

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Narrow the remaining ReportField NVP subscription

This new scoped subscription does not actually stop unrelated report NVP updates from re-rendering ReportField, because the same component still calls useOutstandingReports(...) below, and that hook subscribes to ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS without a selector (src/hooks/useOutstandingReports.ts:16). In the scenario described by this PR—mutating an NVP on a report in another workspace—the hook still receives the full collection update and re-renders this row, so the intended perf fix remains ineffective unless the hook is also scoped or reused with the narrowed NVPs.

Useful? React with 👍 / 👎.


// Per-key report subscriptions instead of full COLLECTION.REPORT
const [transactionReportEntry] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -194,6 +195,7 @@ const taxSliceSelector = (t: OnyxEntry<Transaction>): TaxSlice | undefined => {
// --- ReportField ---

type ReportFieldTransactionState = {reportID: Transaction['reportID']; isFromGlobalCreate: boolean};
type OutstandingReportsForPolicy = OnyxTypes.OutstandingReportsByPolicyIDDerivedValue[string];

const reportFieldTransactionStateSelector = (t: OnyxEntry<Transaction>): ReportFieldTransactionState | undefined => {
if (!t) {
Expand All @@ -208,6 +210,15 @@ const reportFieldTransactionStateSelector = (t: OnyxEntry<Transaction>): ReportF
const createOutstandingReportsForPolicySelector = (policyID: string | undefined) => (derived: OnyxEntry<OnyxTypes.OutstandingReportsByPolicyIDDerivedValue>) =>
derived?.[policyID ?? CONST.DEFAULT_NUMBER_ID];

const createOutstandingReportsNVPsSelector =
(outstandingReports: OutstandingReportsForPolicy | undefined) =>
(allNVPs: OnyxCollection<OnyxTypes.ReportNameValuePairs>): OnyxCollection<OnyxTypes.ReportNameValuePairs> | undefined => {
if (!outstandingReports || !allNVPs) {
return undefined;
}
return getReportNameValuePairsForReports(outstandingReports, allNVPs);
};

// --- InvoiceSenderField ---

type InvoiceSenderWorkspace = {id: string | undefined; name: string | undefined; avatarURL: string | undefined} | undefined;
Expand All @@ -231,6 +242,7 @@ export {
attendeeSliceSelector,
categoryStateSelector,
createCanUpdateSenderWorkspaceSelector,
createOutstandingReportsNVPsSelector,
createOutstandingReportsForPolicySelector,
createTagDisplaySelector,
dateStateSelector,
Expand Down
43 changes: 39 additions & 4 deletions src/hooks/useOutstandingReports.ts
Original file line number Diff line number Diff line change
@@ -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>) => policy?.id;

const createOutstandingReportsNVPsSelector =
(
outstandingReportsByPolicyID: OnyxEntry<OutstandingReportsByPolicyIDDerivedValue>,
selectedPolicyID: string | undefined,
personalPolicyID: string | undefined,
allPoliciesID: OnyxCollection<string>,
shouldUseAllPolicies: boolean,
) =>
(allNVPs: OnyxCollection<ReportNameValuePairs>): OnyxCollection<ReportNameValuePairs> | undefined => {
if (!outstandingReportsByPolicyID || !allNVPs) {
return undefined;
}

const result: OnyxCollection<ReportNameValuePairs> = {};
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) {
Expand All @@ -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};
19 changes: 19 additions & 0 deletions src/libs/ReportNameValuePairsUtils.ts
Original file line number Diff line number Diff line change
@@ -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<Report> | undefined, allNVPs: NonNullable<OnyxCollection<ReportNameValuePairs>>): OnyxCollection<ReportNameValuePairs> {
const result: OnyxCollection<ReportNameValuePairs> = {};
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;
Loading