diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index 2360aff7e04b3..00f78d19ef551 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -8,7 +8,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {PersonalDetailsList, Policy, ReportAttributesDerivedValue} from '@src/types/onyx'; -let isFullyComputed = false; let previousDisplayNames: Record = {}; let previousPersonalDetails: OnyxEntry | undefined; let previousPolicies: OnyxCollection; @@ -58,7 +57,6 @@ const checkDisplayNamesChanged = (personalDetails: OnyxEntry 0) { - isFullyComputed = false; + if (Object.keys(previousPolicies ?? {}).length === 0 && Object.keys(policies ?? {}).length > 0) { + needsFullRecompute = true; } previousPolicies = policies; } + // Use incremental updates when currentValue is already populated and no full recompute is required. + // If currentValue has no reports (fresh install or cleared storage), fall back to a full scan. + const useIncrementalUpdates = !!currentValue?.reports && Object.keys(currentValue.reports).length > 0 && !needsFullRecompute; + // if we already computed the report attributes and there is no new reports data, return the current value - if ((isFullyComputed && !sourceValues) || !reports) { + if ((useIncrementalUpdates && !sourceValues) || !reports) { return currentValue ?? {reports: {}, locale: null}; } @@ -141,7 +142,7 @@ export default createOnyxDerivedValueConfig({ ...Array.from(reportUpdatesRelatedToReportActions), ]; - if (isFullyComputed) { + if (useIncrementalUpdates) { // if there are report-related updates, iterate over the updates if (updates.length > 0 || !!transactionsUpdates || !!transactionViolationsUpdates) { if (updates.length > 0) { @@ -187,6 +188,7 @@ export default createOnyxDerivedValueConfig({ return currentValue ?? {reports: {}, locale: null}; } } + const reportAttributes = dataToIterate.reduce((acc, key) => { // source value sends partial data, so we need an entire report object to do computations const report = reports[key]; @@ -281,11 +283,6 @@ export default createOnyxDerivedValueConfig({ reportAttributes[chatReportID].actionBadge = CONST.REPORT.ACTION_BADGE.FIX; } - // mark the report attributes as fully computed after first iteration to avoid unnecessary recomputation on all objects - if (!Object.keys(reportUpdates).length && Object.keys(reports ?? {}).length > 0 && !isFullyComputed) { - isFullyComputed = true; - } - return { reports: reportAttributes, locale: preferredLocale ?? null,