diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 4c1213876466..a16965db004e 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 93e41574fe96..ea9ebbedba43 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,16 +536,15 @@ Onyx.connect({ }, }); -const reportActionsByReport: 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); - reportActionsByReport[reportID] = actions; + allReportActions = actions; }, }); @@ -1320,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; } @@ -1328,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; } @@ -4563,7 +4561,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; } @@ -5221,7 +5219,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) { @@ -5723,9 +5721,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) || @@ -5898,7 +5897,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); } @@ -5917,7 +5916,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)); } @@ -5940,7 +5939,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 3f1b354d70fc..f63593f432ca 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -23,18 +23,14 @@ 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, callback: (actions, key) => { - if (!key || !actions) { + if (!actions || !key) { return; } - const reportID = CollectionUtils.extractCollectionItemID(key); - reportActionsByReport[reportID] = actions; const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(actions)); @@ -47,6 +43,7 @@ Onyx.connect({ reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED && reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, ); + visibleReportActionItems[reportID] = reportActionsForDisplay[reportActionsForDisplay.length - 1]; }, }); @@ -87,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?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${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 db41bb18aeef..8205a9473e1b 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, 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'; @@ -42,16 +41,15 @@ Onyx.connect({ }, }); -const reportActionsByReport: 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); - reportActionsByReport[reportID] = actions; + allReportActions = actions; }, }); @@ -60,7 +58,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) { @@ -70,7 +68,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 896b88988818..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,16 +258,15 @@ Onyx.connect({ callback: (value) => (allPolicies = value), }); -const reportActionsByReport: 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); - reportActionsByReport[reportID] = actions; + allReportActions = actions; }, }); @@ -286,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 ( @@ -5346,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 610c5c67a0fa..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,16 +58,15 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k }); } -const reportActionsByReport: 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); - reportActionsByReport[reportID] = actions; + allReportActions = actions; }, }); @@ -94,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] ?? {}; } /**