From 3ce60848acb415034d848ecad3f449f61c87f4cc Mon Sep 17 00:00:00 2001 From: Ezra Ellette Date: Mon, 15 Apr 2024 07:11:02 -0500 Subject: [PATCH 1/4] use waitForCollectionCallback --- src/libs/ReportUtils.ts | 11 ++++---- src/libs/SidebarUtils.ts | 44 ++++++++++++++++++----------- src/libs/WorkspacesSettingsUtils.ts | 13 ++++----- src/libs/actions/IOU.ts | 11 ++++---- src/libs/actions/ReportActions.ts | 11 ++++---- 5 files changed, 48 insertions(+), 42 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 1b60cd611d57..abe6f57671c5 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -534,16 +534,15 @@ Onyx.connect({ }, }); -const reportActionsByReport: OnyxCollection = {}; +let reportActionsByReport: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; + reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); }, }); diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 3f1b354d70fc..18ff4063650c 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -23,31 +23,41 @@ import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; import * as UserUtils from './UserUtils'; -const reportActionsByReport: OnyxCollection = {}; -const visibleReportActionItems: ReportActions = {}; +let reportActionsByReport: OnyxCollection = {}; +let visibleReportActionItems: ReportActions = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; - - const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(actions)); + reportActionsByReport = Object.fromEntries( + Object.entries(actions).map(([key, reportActions]) => { + const reportID = CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`); + return [reportID, reportActions]; + }), + ); - // The report is only visible if it is the last action not deleted that - // does not match a closed or created state. - const reportActionsForDisplay = actionsArray.filter( - (reportAction, actionKey) => - ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && - !ReportActionsUtils.isWhisperAction(reportAction) && - reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && - reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + visibleReportActionItems = Object.fromEntries( + Object.entries(reportActionsByReport).map(([reportID, reportActions]) => { + const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(reportActions ?? {})); + + // The report is only visible if it is the last action not deleted that + // does not match a closed or created state. + const reportActionsForDisplay = actionsArray.filter( + (reportAction, actionKey) => + ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && + !ReportActionsUtils.isWhisperAction(reportAction) && + reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && + reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ); + + return [reportID, reportActionsForDisplay[reportActionsForDisplay.length - 1]]; + }), ); - visibleReportActionItems[reportID] = reportActionsForDisplay[reportActionsForDisplay.length - 1]; }, }); diff --git a/src/libs/WorkspacesSettingsUtils.ts b/src/libs/WorkspacesSettingsUtils.ts index 995bcba06a5c..54ebe0ef9ab7 100644 --- a/src/libs/WorkspacesSettingsUtils.ts +++ b/src/libs/WorkspacesSettingsUtils.ts @@ -52,16 +52,15 @@ Onyx.connect({ }, }); -const reportActionsByReport: OnyxCollection = {}; +let reportActionsByReport: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; + reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); }, }); @@ -80,7 +79,7 @@ const getBrickRoadForPolicy = (report: Report): BrickRoad => { // To determine if the report requires attention from the current user, we need to load the parent report action let itemParentReportAction = {}; if (report.parentReportID) { - const itemParentReportActions = reportActionsByReport[report.parentReportID] ?? {}; + const itemParentReportActions = reportActionsByReport?.[report.parentReportID] ?? {}; itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : {}; } const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)}; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index cd0264ddb6ea..d33829ef41a6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -259,16 +259,15 @@ Onyx.connect({ callback: (value) => (allPolicies = value), }); -const reportActionsByReport: OnyxCollection = {}; +let reportActionsByReport: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; + reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); }, }); diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index 610c5c67a0fa..84aba3eca4fd 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -59,16 +59,15 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k }); } -const reportActionsByReport: OnyxCollection = {}; +let reportActionsByReport: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; + reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); }, }); From 3b501792ba7005a074e1f36897b61bd246b46823 Mon Sep 17 00:00:00 2001 From: Ezra Ellette Date: Mon, 15 Apr 2024 18:52:13 -0500 Subject: [PATCH 2/4] Update SidebarUtils reportActionsProcessing --- src/libs/SidebarUtils.ts | 44 +++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 18ff4063650c..60a177c8fea9 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -23,8 +23,8 @@ import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; import * as UserUtils from './UserUtils'; -let reportActionsByReport: OnyxCollection = {}; -let visibleReportActionItems: ReportActions = {}; +const reportActionsByReport: OnyxCollection = {}; +const visibleReportActionItems: ReportActions = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -34,30 +34,24 @@ Onyx.connect({ return; } - reportActionsByReport = Object.fromEntries( - Object.entries(actions).map(([key, reportActions]) => { - const reportID = CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`); - return [reportID, reportActions]; - }), - ); + Object.entries(actions).forEach(([reportKey, reportActions]) => { + const reportID = CollectionUtils.extractCollectionItemID(reportKey as `reportActions_${string}`); - visibleReportActionItems = Object.fromEntries( - Object.entries(reportActionsByReport).map(([reportID, reportActions]) => { - const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(reportActions ?? {})); - - // The report is only visible if it is the last action not deleted that - // does not match a closed or created state. - const reportActionsForDisplay = actionsArray.filter( - (reportAction, actionKey) => - ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && - !ReportActionsUtils.isWhisperAction(reportAction) && - reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && - reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, - ); - - return [reportID, reportActionsForDisplay[reportActionsForDisplay.length - 1]]; - }), - ); + reportActionsByReport[reportID] = reportActions; + + const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(reportActions ?? {})); + // The report is only visible if it is the last action not deleted that + // does not match a closed or created state. + const reportActionsForDisplay = actionsArray.filter( + (reportAction, actionKey) => + ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && + !ReportActionsUtils.isWhisperAction(reportAction) && + reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && + reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ); + + visibleReportActionItems[reportID] = reportActionsForDisplay[reportActionsForDisplay.length - 1]; + }); }, }); From 9fa396f4b489b566f8530fef8daeabdc9fd3d380 Mon Sep 17 00:00:00 2001 From: Ezra Ellette Date: Tue, 16 Apr 2024 15:27:19 -0500 Subject: [PATCH 3/4] useWaitForCollectionCallback no iter --- src/libs/ReportActionsUtils.ts | 25 +++++++++--------- src/libs/ReportUtils.ts | 22 ++++++++-------- src/libs/SidebarUtils.ts | 41 ++++++++++++----------------- src/libs/WorkspacesSettingsUtils.ts | 9 +++---- src/libs/actions/IOU.ts | 9 +++---- src/libs/actions/ReportActions.ts | 7 +++-- src/libs/actions/Task.ts | 13 +++++---- 7 files changed, 57 insertions(+), 69 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 32972a81bcb5..b2feed518683 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -20,7 +20,6 @@ import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/Rep import type ReportAction from '@src/types/onyx/ReportAction'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import * as CollectionUtils from './CollectionUtils'; import * as Environment from './Environment/Environment'; import isReportMessageAttachment from './isReportMessageAttachment'; import * as Localize from './Localize'; @@ -59,16 +58,16 @@ Onyx.connect({ }, }); -const allReportActions: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - const reportID = CollectionUtils.extractCollectionItemID(key); - allReportActions[reportID] = actions; + allReportActions = actions; }, }); @@ -195,7 +194,7 @@ function getParentReportAction(report: OnyxEntry | EmptyObject): ReportA if (!report?.parentReportID || !report.parentReportActionID) { return {}; } - return allReportActions?.[report.parentReportID]?.[report.parentReportActionID] ?? {}; + return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID] ?? {}; } /** @@ -593,7 +592,7 @@ function replaceBaseURLInPolicyChangeLogAction(reportAction: ReportAction): Repo } function getLastVisibleAction(reportID: string, actionsToMerge: OnyxCollection = {}): OnyxEntry { - const reportActions = Object.values(fastMerge(allReportActions?.[reportID] ?? {}, actionsToMerge ?? {}, true)); + const reportActions = Object.values(fastMerge(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}, actionsToMerge ?? {}, true)); const visibleReportActions = Object.values(reportActions ?? {}).filter((action): action is ReportAction => shouldReportActionBeVisibleAsLastAction(action)); const sortedReportActions = getSortedReportActions(visibleReportActions, true); if (sortedReportActions.length === 0) { @@ -714,7 +713,7 @@ function getLatestReportActionFromOnyxData(onyxData: OnyxUpdate[] | null): OnyxE * Find the transaction associated with this reportAction, if one exists. */ function getLinkedTransactionID(reportActionOrID: string | OnyxEntry, reportID?: string): string | null { - const reportAction = typeof reportActionOrID === 'string' ? allReportActions?.[reportID ?? '']?.[reportActionOrID] : reportActionOrID; + const reportAction = typeof reportActionOrID === 'string' ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]?.[reportActionOrID] : reportActionOrID; if (!reportAction || reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU) { return null; } @@ -722,7 +721,7 @@ function getLinkedTransactionID(reportActionOrID: string | OnyxEntry { - return allReportActions?.[reportID]?.[reportActionID] ?? null; + return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]?.[reportActionID] ?? null; } function getMostRecentReportActionLastModified(): string { @@ -769,7 +768,7 @@ function getMostRecentReportActionLastModified(): string { */ function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry { return ( - Object.values(allReportActions?.[chatReportID] ?? {}).find( + Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {}).find( (reportAction) => reportAction && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && reportAction.originalMessage.linkedReportID === iouReportID, ) ?? null ); @@ -823,7 +822,7 @@ function isTaskAction(reportAction: OnyxEntry): boolean { * If there are no visible actions left (including system messages), we can hide the report from view entirely */ function doesReportHaveVisibleActions(reportID: string, actionsToMerge: ReportActions = {}): boolean { - const reportActions = Object.values(fastMerge(allReportActions?.[reportID] ?? {}, actionsToMerge, true)); + const reportActions = Object.values(fastMerge(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}, actionsToMerge, true)); const visibleReportActions = Object.values(reportActions ?? {}).filter((action) => shouldReportActionBeVisibleAsLastAction(action)); // Exclude the task system message and the created message @@ -832,7 +831,7 @@ function doesReportHaveVisibleActions(reportID: string, actionsToMerge: ReportAc } function getAllReportActions(reportID: string): ReportActions { - return allReportActions?.[reportID] ?? {}; + return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}; } /** diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 49cf4f11f078..3587d67ee530 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -55,7 +55,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; import * as IOU from './actions/IOU'; import * as store from './actions/ReimbursementAccount/store'; -import * as CollectionUtils from './CollectionUtils'; import * as CurrencyUtils from './CurrencyUtils'; import DateUtils from './DateUtils'; import {hasValidDraftComment} from './DraftCommentUtils'; @@ -537,7 +536,7 @@ Onyx.connect({ }, }); -let reportActionsByReport: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, @@ -545,7 +544,7 @@ Onyx.connect({ if (!actions) { return; } - reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); + allReportActions = actions; }, }); @@ -1319,7 +1318,7 @@ function isMoneyRequestReport(reportOrID: OnyxEntry | EmptyObject | stri * Checks if a report has only one transaction associated with it */ function isOneTransactionReport(reportID: string): boolean { - const reportActions = reportActionsByReport?.[reportID] ?? ([] as ReportAction[]); + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? ([] as ReportAction[]); return ReportActionsUtils.getOneTransactionThreadReportID(reportID, reportActions) !== null; } @@ -1327,7 +1326,7 @@ function isOneTransactionReport(reportID: string): boolean { * Checks if a report is a transaction thread associated with a report that has only one transaction */ function isOneTransactionThread(reportID: string, parentReportID: string): boolean { - const parentReportActions = reportActionsByReport?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`] ?? ([] as ReportAction[]); + const parentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`] ?? ([] as ReportAction[]); const transactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(parentReportID, parentReportActions); return reportID === transactionThreadReportID; } @@ -4553,7 +4552,7 @@ function canAccessReport(report: OnyxEntry, policies: OnyxCollection, currentReportId: string): boolean { const currentReport = getReport(currentReportId); const parentReport = getParentReport(!isEmptyObject(currentReport) ? currentReport : null); - const reportActions = reportActionsByReport?.[report?.reportID ?? ''] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`] ?? {}; const isChildReportHasComment = Object.values(reportActions ?? {})?.some((reportAction) => (reportAction?.childVisibleActionCount ?? 0) > 0); return parentReport?.reportID !== report?.reportID && !isChildReportHasComment; } @@ -5211,7 +5210,7 @@ function canUserPerformWriteAction(report: OnyxEntry) { * Returns ID of the original report from which the given reportAction is first created. */ function getOriginalReportID(reportID: string, reportAction: OnyxEntry): string | undefined { - const reportActions = reportActionsByReport?.[reportID]; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]; const currentReportAction = reportActions?.[reportAction?.reportActionID ?? ''] ?? null; const transactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(reportID, reportActions ?? ([] as ReportAction[])); if (transactionThreadReportID !== null) { @@ -5713,9 +5712,10 @@ function shouldDisableThread(reportAction: OnyxEntry, reportID: st const isIOUAction = ReportActionsUtils.isMoneyRequestAction(reportAction); const isWhisperAction = ReportActionsUtils.isWhisperAction(reportAction) || ReportActionsUtils.isActionableTrackExpense(reportAction); const isArchivedReport = isArchivedRoom(getReport(reportID)); + const isActionDisabled = CONST.REPORT.ACTIONS.THREAD_DISABLED.some((action: string) => action === reportAction?.actionName); return ( - CONST.REPORT.ACTIONS.THREAD_DISABLED.some((action: string) => action === reportAction?.actionName) || + isActionDisabled || isSplitBillAction || (isDeletedAction && !reportAction?.childVisibleActionCount) || (isArchivedReport && !reportAction?.childVisibleActionCount) || @@ -5888,7 +5888,7 @@ function getIndicatedMissingPaymentMethod(userWallet: OnyxEntry, rep * Checks if report chat contains missing payment method */ function hasMissingPaymentMethod(userWallet: OnyxEntry, iouReportID: string): boolean { - const reportActions = reportActionsByReport?.[iouReportID] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`] ?? {}; return Object.values(reportActions).some((action) => getIndicatedMissingPaymentMethod(userWallet, iouReportID, action) !== undefined); } @@ -5907,7 +5907,7 @@ function shouldCreateNewMoneyRequestReport(existingIOUReport: OnyxEntry * Checks if report contains actions with errors */ function hasActionsWithErrors(reportID: string): boolean { - const reportActions = reportActionsByReport?.[reportID ?? ''] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}; return Object.values(reportActions).some((action) => !isEmptyObject(action.errors)); } @@ -5930,7 +5930,7 @@ function getReportActionActorAccountID(reportAction: OnyxEntry, io function createDraftTransactionAndNavigateToParticipantSelector(transactionID: string, reportID: string, actionName: ValueOf, reportActionID: string): void { const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction); - const reportActions = reportActionsByReport?.[reportID] ?? ([] as ReportAction[]); + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? ([] as ReportAction[]); if (!transaction || !reportActions) { return; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 60a177c8fea9..d83a8db3cd39 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -23,35 +23,28 @@ import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; import * as UserUtils from './UserUtils'; -const reportActionsByReport: OnyxCollection = {}; const visibleReportActionItems: ReportActions = {}; - Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (actions) => { - if (!actions) { + callback: (actions, key) => { + if (!actions || !key) { return; } + const reportID = CollectionUtils.extractCollectionItemID(key); + + const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(actions)); + + // The report is only visible if it is the last action not deleted that + // does not match a closed or created state. + const reportActionsForDisplay = actionsArray.filter( + (reportAction, actionKey) => + ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && + !ReportActionsUtils.isWhisperAction(reportAction) && + reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && + reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ); - Object.entries(actions).forEach(([reportKey, reportActions]) => { - const reportID = CollectionUtils.extractCollectionItemID(reportKey as `reportActions_${string}`); - - reportActionsByReport[reportID] = reportActions; - - const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(reportActions ?? {})); - // The report is only visible if it is the last action not deleted that - // does not match a closed or created state. - const reportActionsForDisplay = actionsArray.filter( - (reportAction, actionKey) => - ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && - !ReportActionsUtils.isWhisperAction(reportAction) && - reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && - reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, - ); - - visibleReportActionItems[reportID] = reportActionsForDisplay[reportActionsForDisplay.length - 1]; - }); + visibleReportActionItems[reportID] = reportActionsForDisplay[reportActionsForDisplay.length - 1]; }, }); @@ -91,7 +84,7 @@ function getOrderedReportIDs( const parentReportActionsKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`; const parentReportActions = allReportActions?.[parentReportActionsKey]; - const reportActions = reportActionsByReport?.[report.reportID] ?? {}; + const reportActions = allReportActions?.[report.reportID] ?? {}; const parentReportAction = parentReportActions?.find((action) => action && action?.reportActionID === report.parentReportActionID); const doesReportHaveViolations = !!( betas?.includes(CONST.BETAS.VIOLATIONS) && diff --git a/src/libs/WorkspacesSettingsUtils.ts b/src/libs/WorkspacesSettingsUtils.ts index 54ebe0ef9ab7..5d4238c533be 100644 --- a/src/libs/WorkspacesSettingsUtils.ts +++ b/src/libs/WorkspacesSettingsUtils.ts @@ -6,7 +6,6 @@ import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, PolicyMembers, ReimbursementAccount, Report, ReportActions} from '@src/types/onyx'; import type {Unit} from '@src/types/onyx/Policy'; -import * as CollectionUtils from './CollectionUtils'; import * as CurrencyUtils from './CurrencyUtils'; import type {Phrase, PhraseParameters} from './Localize'; import * as OptionsListUtils from './OptionsListUtils'; @@ -52,7 +51,7 @@ Onyx.connect({ }, }); -let reportActionsByReport: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, @@ -60,7 +59,7 @@ Onyx.connect({ if (!actions) { return; } - reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); + allReportActions = actions; }, }); @@ -69,7 +68,7 @@ Onyx.connect({ * @returns BrickRoad for the policy passed as a param */ const getBrickRoadForPolicy = (report: Report): BrickRoad => { - const reportActions = reportActionsByReport?.[report.reportID] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {}; const reportErrors = OptionsListUtils.getAllReportErrors(report, reportActions); const doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; if (doesReportContainErrors) { @@ -79,7 +78,7 @@ const getBrickRoadForPolicy = (report: Report): BrickRoad => { // To determine if the report requires attention from the current user, we need to load the parent report action let itemParentReportAction = {}; if (report.parentReportID) { - const itemParentReportActions = reportActionsByReport?.[report.parentReportID] ?? {}; + const itemParentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`] ?? {}; itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : {}; } const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)}; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e2de6f995492..3ce1edb36630 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -25,7 +25,6 @@ import type { UpdateMoneyRequestParams, } from '@libs/API/parameters'; import {WRITE_COMMANDS} from '@libs/API/types'; -import * as CollectionUtils from '@libs/CollectionUtils'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import DateUtils from '@libs/DateUtils'; import DistanceRequestUtils from '@libs/DistanceRequestUtils'; @@ -259,7 +258,7 @@ Onyx.connect({ callback: (value) => (allPolicies = value), }); -let reportActionsByReport: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, @@ -267,7 +266,7 @@ Onyx.connect({ if (!actions) { return; } - reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); + allReportActions = actions; }, }); @@ -285,7 +284,7 @@ function getPolicy(policyID: string | undefined): OnyxTypes.Policy | EmptyObject * Find the report preview action from given chat report and iou report */ function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry { - const reportActions = reportActionsByReport?.[chatReportID] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {}; // Find the report preview action from the chat report return ( @@ -5345,7 +5344,7 @@ function canIOUBePaid(iouReport: OnyxEntry | EmptyObject, chat } function hasIOUToApproveOrPay(chatReport: OnyxEntry | EmptyObject, excludedIOUReportID: string): boolean { - const chatReportActions = reportActionsByReport?.[chatReport?.reportID ?? ''] ?? {}; + const chatReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`] ?? {}; return Object.values(chatReportActions).some((action) => { const iouReport = ReportUtils.getReport(action.childReportID ?? ''); diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index 84aba3eca4fd..217dd0b12100 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -1,6 +1,5 @@ import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import * as CollectionUtils from '@libs/CollectionUtils'; import * as ReportActionUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; @@ -59,7 +58,7 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k }); } -let reportActionsByReport: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, @@ -67,7 +66,7 @@ Onyx.connect({ if (!actions) { return; } - reportActionsByReport = Object.fromEntries(Object.entries(actions).map(([key, value]) => [CollectionUtils.extractCollectionItemID(key as `reportActions_${string}`), value])); + allReportActions = actions; }, }); @@ -93,7 +92,7 @@ function clearAllRelatedReportActionErrors(reportID: string, reportAction: Repor } if (reportAction.childReportID && ignore !== 'child') { - const childActions = reportActionsByReport?.[reportAction.childReportID] ?? {}; + const childActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportAction.childReportID}`] ?? {}; Object.values(childActions).forEach((action) => { const childErrorKeys = Object.keys(action.errors ?? {}).filter((err) => errorKeys.includes(err)); clearAllRelatedReportActionErrors(reportAction.childReportID ?? '', action, 'parent', childErrorKeys); diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 9cf7b0b78008..f74b451c29f6 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -5,7 +5,6 @@ import * as Expensicons from '@components/Icon/Expensicons'; import * as API from '@libs/API'; import type {CancelTaskParams, CompleteTaskParams, CreateTaskParams, EditTaskAssigneeParams, EditTaskParams, ReopenTaskParams} from '@libs/API/parameters'; import {WRITE_COMMANDS} from '@libs/API/types'; -import * as CollectionUtils from '@libs/CollectionUtils'; import DateUtils from '@libs/DateUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; @@ -67,16 +66,16 @@ Onyx.connect({ }, }); -const allReportActions: OnyxCollection = {}; +let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (actions, key) => { - if (!key || !actions) { + waitForCollectionCallback: true, + callback: (actions) => { + if (!actions) { return; } - const reportID = CollectionUtils.extractCollectionItemID(key); - allReportActions[reportID] = actions; + allReportActions = actions; }, }); @@ -795,7 +794,7 @@ function getParentReportAction(report: OnyxEntry): ReportActio if (!report?.parentReportID || !report.parentReportActionID) { return {}; } - return allReportActions?.[report.parentReportID]?.[report.parentReportActionID] ?? {}; + return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID] ?? {}; } /** From eb1f0116788e031ccaf4aaa0a07b30c89d524d46 Mon Sep 17 00:00:00 2001 From: Ezra Ellette Date: Tue, 16 Apr 2024 17:08:07 -0500 Subject: [PATCH 4/4] use waitForCollectionCallback no iter --- src/libs/SidebarUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index d83a8db3cd39..f63593f432ca 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -84,7 +84,7 @@ function getOrderedReportIDs( const parentReportActionsKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`; const parentReportActions = allReportActions?.[parentReportActionsKey]; - const reportActions = allReportActions?.[report.reportID] ?? {}; + const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {}; const parentReportAction = parentReportActions?.find((action) => action && action?.reportActionID === report.parentReportActionID); const doesReportHaveViolations = !!( betas?.includes(CONST.BETAS.VIOLATIONS) &&