diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index bb52686c4bd4..60d602838c70 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -244,17 +244,6 @@ Onyx.connect({ }, }); -const policyExpenseReports: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!ReportUtils.isPolicyExpenseChat(report)) { - return; - } - policyExpenseReports[key] = report; - }, -}); - let allTransactions: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION, @@ -763,13 +752,13 @@ function getReportOption(participant: Participant): ReportUtils.OptionData { /** * Get the option for a policy expense report. */ -function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { - const expenseReport = policyExpenseReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]; +function getPolicyExpenseReportOption(participant: Participant | ReportUtils.OptionData): ReportUtils.OptionData { + const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? ReportUtils.getReport(participant.reportID) : null; const option = createOption( expenseReport?.visibleChatMemberAccountIDs ?? [], allPersonalDetails ?? {}, - expenseReport ?? null, + !isEmptyObject(expenseReport) ? expenseReport : null, {}, { showChatPreviewLine: false, @@ -780,8 +769,8 @@ function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { // Update text & alternateText because createOption returns workspace name only if report is owned by the user option.text = ReportUtils.getPolicyName(expenseReport); option.alternateText = Localize.translateLocal('workspace.common.workspace'); - option.selected = report.selected; - option.isSelected = report.selected; + option.selected = participant.selected; + option.isSelected = participant.selected; return option; } diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 11f870b891b6..43145dc3c7a1 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -50,17 +50,12 @@ type MemberChangeMessageElement = MessageTextElement | MemberChangeMessageUserMe const policyChangeActionsSet = new Set(Object.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG)); -const allReports: OnyxCollection = {}; - +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - - const reportID = CollectionUtils.extractCollectionItemID(key); - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (reports) => { + allReports = reports; }, }); diff --git a/src/libs/TaskUtils.ts b/src/libs/TaskUtils.ts index 81a079003d0e..7cbc9f2dc1f8 100644 --- a/src/libs/TaskUtils.ts +++ b/src/libs/TaskUtils.ts @@ -1,22 +1,18 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; import type {Message} from '@src/types/onyx/ReportAction'; import type ReportAction from '@src/types/onyx/ReportAction'; -import * as CollectionUtils from './CollectionUtils'; import * as Localize from './Localize'; -const allReports: Record = {}; +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - const reportID = CollectionUtils.extractCollectionItemID(key); - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (reports) => { + allReports = reports; }, }); @@ -42,11 +38,11 @@ function getTaskReportActionMessage(action: OnyxEntry): Pick) { diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 7ae174ac8606..77347967b6cd 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -1,7 +1,6 @@ import debounce from 'lodash/debounce'; import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import * as CollectionUtils from '@libs/CollectionUtils'; import Log from '@libs/Log'; import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; @@ -36,21 +35,12 @@ Onyx.connect({ // eslint-disable-next-line @typescript-eslint/no-use-before-define const autoSwitchToFocusMode = debounce(tryFocusModeUpdate, 300, {leading: true}); -let allReports: OnyxCollection | undefined; +let allReports: OnyxCollection | undefined = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - - if (!allReports) { - allReports = {}; - } - - const reportID = CollectionUtils.extractCollectionItemID(key); - - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (reports) => { + allReports = reports; // Each time a new report is added we will check to see if the user should be switched autoSwitchToFocusMode(); diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 8cea9cbd0dae..225022665ddc 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -136,6 +136,8 @@ Onyx.connect({ } const reportID = CollectionUtils.extractCollectionItemID(key); currentReportData[reportID] = report; + // eslint-disable-next-line @typescript-eslint/no-use-before-define + handleReportChanged(report); }, }); @@ -1126,11 +1128,6 @@ function handleReportChanged(report: OnyxEntry) { } } -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - callback: handleReportChanged, -}); - /** Deletes a comment from the report, basically sets it as empty string */ function deleteReportComment(reportID: string, reportAction: ReportAction) { const originalReportID = ReportUtils.getOriginalReportID(reportID, reportAction); diff --git a/src/libs/actions/Welcome.ts b/src/libs/actions/Welcome.ts index 89d607645fb6..150b23a4a0d9 100644 --- a/src/libs/actions/Welcome.ts +++ b/src/libs/actions/Welcome.ts @@ -82,16 +82,13 @@ Onyx.connect({ }, }); -const allReports: OnyxCollection = {}; +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, initWithStoredValues: false, - callback: (val, key) => { - if (!val || !key) { - return; - } - - allReports[key] = {...allReports[key], ...val}; + waitForCollectionCallback: true, + callback: (reports) => { + allReports = reports; }, }); diff --git a/src/setup/index.ts b/src/setup/index.ts index f79e6a461a3e..581dbd28056f 100644 --- a/src/setup/index.ts +++ b/src/setup/index.ts @@ -27,7 +27,7 @@ export default function () { keys: ONYXKEYS, // Increase the cached key count so that the app works more consistently for accounts with large numbers of reports - maxCachedKeysCount: 10000, + maxCachedKeysCount: 20000, safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], captureMetrics: Metrics.canCaptureOnyxMetrics(), initialKeyStates: {