diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 544ced533e82..aadeaa831261 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -37,7 +37,6 @@ import type {EmptyObject} from '@src/types/utils/EmptyObject'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import times from '@src/utils/times'; import Timing from './actions/Timing'; -import * as CollectionUtils from './CollectionUtils'; import * as ErrorUtils from './ErrorUtils'; import filterArrayByMatch from './filterArrayByMatch'; import localeCompare from './LocaleCompare'; @@ -264,32 +263,46 @@ Onyx.connect({ const lastReportActions: ReportActions = {}; const allSortedReportActions: Record = {}; -const allReportActions: Record = {}; +let allReportActions: OnyxCollection; const 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); - allReportActions[reportID] = actions; - let sortedReportActions = ReportActionUtils.getSortedReportActions(Object.values(actions), true); - allSortedReportActions[reportID] = sortedReportActions; - - const transactionThreadReportID = ReportActionUtils.getOneTransactionThreadReportID(reportID, allReportActions[reportID], true); - if (transactionThreadReportID) { - sortedReportActions = ReportActionUtils.getCombinedReportActions(allSortedReportActions[reportID], allSortedReportActions[transactionThreadReportID]); - } - lastReportActions[reportID] = sortedReportActions[0]; + allReportActions = actions ?? {}; + + // Iterate over the report actions to build the sorted and lastVisible report actions objects + Object.entries(allReportActions).forEach((reportActions) => { + const reportID = reportActions[0].split('_')[1]; + const reportActionsArray = Object.values(reportActions[1] ?? {}); + let sortedReportActions = ReportActionUtils.getSortedReportActions(reportActionsArray, true); + allSortedReportActions[reportID] = sortedReportActions; + + // If the report is a one-transaction report and has , we need to return the combined reportActions so that the LHN can display modifications + // to the transaction thread or the report itself + const transactionThreadReportID = ReportActionUtils.getOneTransactionThreadReportID(reportID, actions[reportActions[0]], true); + if (transactionThreadReportID) { + const transactionThreadReportActionsArray = Object.values(actions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`] ?? {}); + sortedReportActions = ReportActionUtils.getCombinedReportActions(reportActionsArray, transactionThreadReportActionsArray); + } - // The report is only visible if it is the last action not deleted that - // does not match a closed or created state. - const reportActionsForDisplay = sortedReportActions.filter( - (reportAction) => ReportActionUtils.shouldReportActionBeVisibleAsLastAction(reportAction) && reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED, - ); - visibleReportActionItems[reportID] = reportActionsForDisplay[0]; + lastReportActions[reportID] = sortedReportActions[0]; + + // The report is only visible if it is the last action not deleted that + // does not match a closed or created state. + const reportActionsForDisplay = sortedReportActions.filter( + (reportAction, actionKey) => + ReportActionUtils.shouldReportActionBeVisible(reportAction, actionKey) && + !ReportActionUtils.isWhisperAction(reportAction) && + reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && + reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ); + visibleReportActionItems[reportID] = reportActionsForDisplay[0]; + }); }, }); @@ -1746,7 +1759,8 @@ function getOptions( const {parentReportID, parentReportActionID} = report ?? {}; const canGetParentReport = parentReportID && parentReportActionID && allReportActions; - const parentReportAction = canGetParentReport ? allReportActions[parentReportID]?.[parentReportActionID] ?? null : null; + const parentReportActions = allReportActions ? allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`] ?? {} : {}; + const parentReportAction = canGetParentReport ? parentReportActions[parentReportActionID] ?? null : null; const doesReportHaveViolations = (betas?.includes(CONST.BETAS.VIOLATIONS) && ReportUtils.doesTransactionThreadHaveViolations(report, transactionViolations, parentReportAction)) ?? false;