Skip to content

Commit

Permalink
Merge pull request #40233 from EzraEllette/iss-39091-use-waitForColle…
Browse files Browse the repository at this point in the history
…ctionCallback

Issue 39091 - use waitForCollectionCallback
  • Loading branch information
tgolen authored Apr 17, 2024
2 parents 553e54f + eb1f011 commit 1d3886d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 67 deletions.
25 changes: 12 additions & 13 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,16 +58,16 @@ Onyx.connect({
},
});

const allReportActions: OnyxCollection<ReportActions> = {};
let allReportActions: OnyxCollection<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;
allReportActions = actions;
},
});

Expand Down Expand Up @@ -195,7 +194,7 @@ function getParentReportAction(report: OnyxEntry<Report> | EmptyObject): ReportA
if (!report?.parentReportID || !report.parentReportActionID) {
return {};
}
return allReportActions?.[report.parentReportID]?.[report.parentReportActionID] ?? {};
return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID] ?? {};
}

/**
Expand Down Expand Up @@ -593,7 +592,7 @@ function replaceBaseURLInPolicyChangeLogAction(reportAction: ReportAction): Repo
}

function getLastVisibleAction(reportID: string, actionsToMerge: OnyxCollection<ReportAction> = {}): OnyxEntry<ReportAction> {
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) {
Expand Down Expand Up @@ -714,15 +713,15 @@ function getLatestReportActionFromOnyxData(onyxData: OnyxUpdate[] | null): OnyxE
* Find the transaction associated with this reportAction, if one exists.
*/
function getLinkedTransactionID(reportActionOrID: string | OnyxEntry<ReportAction>, 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;
}
return reportAction.originalMessage?.IOUTransactionID ?? null;
}

function getReportAction(reportID: string, reportActionID: string): OnyxEntry<ReportAction> {
return allReportActions?.[reportID]?.[reportActionID] ?? null;
return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]?.[reportActionID] ?? null;
}

function getMostRecentReportActionLastModified(): string {
Expand Down Expand Up @@ -769,7 +768,7 @@ function getMostRecentReportActionLastModified(): string {
*/
function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry<ReportAction> {
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
);
Expand Down Expand Up @@ -823,7 +822,7 @@ function isTaskAction(reportAction: OnyxEntry<ReportAction>): 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
Expand All @@ -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}`] ?? {};
}

/**
Expand Down
29 changes: 14 additions & 15 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -537,16 +536,15 @@ Onyx.connect({
},
});

const reportActionsByReport: OnyxCollection<ReportActions> = {};
let allReportActions: OnyxCollection<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;
allReportActions = actions;
},
});

Expand Down Expand Up @@ -1320,15 +1318,15 @@ function isMoneyRequestReport(reportOrID: OnyxEntry<Report> | 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;
}

/**
* 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;
}
Expand Down Expand Up @@ -4563,7 +4561,7 @@ function canAccessReport(report: OnyxEntry<Report>, policies: OnyxCollection<Pol
function shouldHideReport(report: OnyxEntry<Report>, 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;
}
Expand Down Expand Up @@ -5221,7 +5219,7 @@ function canUserPerformWriteAction(report: OnyxEntry<Report>) {
* Returns ID of the original report from which the given reportAction is first created.
*/
function getOriginalReportID(reportID: string, reportAction: OnyxEntry<ReportAction>): 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) {
Expand Down Expand Up @@ -5723,9 +5721,10 @@ function shouldDisableThread(reportAction: OnyxEntry<ReportAction>, 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) ||
Expand Down Expand Up @@ -5898,7 +5897,7 @@ function getIndicatedMissingPaymentMethod(userWallet: OnyxEntry<UserWallet>, rep
* Checks if report chat contains missing payment method
*/
function hasMissingPaymentMethod(userWallet: OnyxEntry<UserWallet>, 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);
}

Expand All @@ -5917,7 +5916,7 @@ function shouldCreateNewMoneyRequestReport(existingIOUReport: OnyxEntry<Report>
* 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));
}

Expand All @@ -5940,7 +5939,7 @@ function getReportActionActorAccountID(reportAction: OnyxEntry<ReportAction>, io

function createDraftTransactionAndNavigateToParticipantSelector(transactionID: string, reportID: string, actionName: ValueOf<typeof CONST.IOU.ACTION>, 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;
Expand Down
9 changes: 3 additions & 6 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ import * as ReportUtils from './ReportUtils';
import * as TaskUtils from './TaskUtils';
import * as UserUtils from './UserUtils';

const reportActionsByReport: OnyxCollection<ReportActions> = {};
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));

Expand All @@ -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];
},
});
Expand Down Expand Up @@ -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) &&
Expand Down
16 changes: 7 additions & 9 deletions src/libs/WorkspacesSettingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,16 +41,15 @@ Onyx.connect({
},
});

const reportActionsByReport: OnyxCollection<ReportActions> = {};
let allReportActions: OnyxCollection<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;
allReportActions = actions;
},
});

Expand All @@ -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) {
Expand All @@ -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)};
Expand Down
16 changes: 7 additions & 9 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -259,16 +258,15 @@ Onyx.connect({
callback: (value) => (allPolicies = value),
});

const reportActionsByReport: OnyxCollection<OnyxTypes.ReportActions> = {};
let allReportActions: OnyxCollection<OnyxTypes.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;
allReportActions = actions;
},
});

Expand All @@ -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<ReportAction> {
const reportActions = reportActionsByReport?.[chatReportID] ?? {};
const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {};

// Find the report preview action from the chat report
return (
Expand Down Expand Up @@ -5346,7 +5344,7 @@ function canIOUBePaid(iouReport: OnyxEntry<OnyxTypes.Report> | EmptyObject, chat
}

function hasIOUToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report> | 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 ?? '');
Expand Down
14 changes: 6 additions & 8 deletions src/libs/actions/ReportActions.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -59,16 +58,15 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k
});
}

const reportActionsByReport: OnyxCollection<ReportActions> = {};
let allReportActions: OnyxCollection<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;
allReportActions = actions;
},
});

Expand All @@ -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);
Expand Down
Loading

0 comments on commit 1d3886d

Please sign in to comment.