From e74d7248d068e4edb551525fb52bdaeb3cc38fd2 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Mon, 9 Oct 2023 09:42:13 +0200 Subject: [PATCH] fix: fixed import --- src/libs/ReportActionsUtils.ts | 85 +++++++++++++++++----------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index e3b053c94252..57646abc9341 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -4,7 +4,8 @@ import Onyx, {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import {ValueOf} from 'type-fest'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; -import * as OnyxTypes from '../types/onyx'; +import ReportAction, {ReportActions} from '../types/onyx/ReportAction'; +import Report from '../types/onyx/Report'; import {ActionName} from '../types/onyx/OriginalMessage'; import * as CollectionUtils from './CollectionUtils'; import Log from './Log'; @@ -16,7 +17,7 @@ type LastVisibleMessage = { lastMessageHtml?: string; }; -const allReports: OnyxCollection = {}; +const allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, callback: (report, key) => { @@ -29,7 +30,7 @@ Onyx.connect({ }, }); -const allReportActions: OnyxCollection = {}; +const allReportActions: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, callback: (actions, key) => { @@ -48,44 +49,44 @@ Onyx.connect({ callback: (val) => (isNetworkOffline = val?.isOffline ?? false), }); -function isCreatedAction(reportAction: OnyxEntry): boolean { +function isCreatedAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED; } -function isDeletedAction(reportAction: OnyxEntry): boolean { +function isDeletedAction(reportAction: OnyxEntry): boolean { // A deleted comment has either an empty array or an object with html field with empty string as value const message = reportAction?.message ?? []; return message.length === 0 || message[0]?.html === ''; } -function isDeletedParentAction(reportAction: OnyxEntry): boolean { +function isDeletedParentAction(reportAction: OnyxEntry): boolean { return (reportAction?.message?.[0]?.isDeletedParentAction ?? false) && (reportAction?.childVisibleActionCount ?? 0) > 0; } -function isPendingRemove(reportAction: OnyxEntry): boolean { +function isPendingRemove(reportAction: OnyxEntry): boolean { return reportAction?.message?.[0]?.moderationDecision?.decision === CONST.MODERATION.MODERATOR_DECISION_PENDING_REMOVE; } -function isMoneyRequestAction(reportAction: OnyxEntry): boolean { +function isMoneyRequestAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU; } -function isReportPreviewAction(reportAction: OnyxEntry): boolean { +function isReportPreviewAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW; } -function isModifiedExpenseAction(reportAction: OnyxEntry): boolean { +function isModifiedExpenseAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE; } -function isWhisperAction(reportAction: OnyxEntry): boolean { +function isWhisperAction(reportAction: OnyxEntry): boolean { return (reportAction?.whisperedToAccountIDs ?? []).length > 0; } /** * Returns whether the comment is a thread parent message/the first message in a thread */ -function isThreadParentMessage(reportAction: OnyxEntry, reportID: string): boolean { +function isThreadParentMessage(reportAction: OnyxEntry, reportID: string): boolean { const {childType, childVisibleActionCount = 0, childReportID} = reportAction ?? {}; return childType === CONST.REPORT.TYPE.CHAT && (childVisibleActionCount > 0 || String(childReportID) === reportID); } @@ -95,7 +96,7 @@ function isThreadParentMessage(reportAction: OnyxEntry, * * @deprecated Use Onyx.connect() or withOnyx() instead */ -function getParentReportAction(report: OnyxEntry, allReportActionsParam?: OnyxCollection): OnyxTypes.ReportAction | Record { +function getParentReportAction(report: OnyxEntry, allReportActionsParam?: OnyxCollection): ReportAction | Record { if (!report?.parentReportID || !report.parentReportActionID) { return {}; } @@ -105,7 +106,7 @@ function getParentReportAction(report: OnyxEntry, allReportAct /** * Determines if the given report action is sent money report action by checking for 'pay' type and presence of IOUDetails object. */ -function isSentMoneyReportAction(reportAction: OnyxEntry): boolean { +function isSentMoneyReportAction(reportAction: OnyxEntry): boolean { return ( reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU && reportAction?.originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.PAY && !!reportAction?.originalMessage?.IOUDetails ); @@ -115,7 +116,7 @@ function isSentMoneyReportAction(reportAction: OnyxEntry * Returns whether the thread is a transaction thread, which is any thread with IOU parent * report action from requesting money (type - create) or from sending money (type - pay with IOUDetails field) */ -function isTransactionThread(parentReportAction: OnyxEntry): boolean { +function isTransactionThread(parentReportAction: OnyxEntry): boolean { return ( parentReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU && (parentReportAction.originalMessage.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE || @@ -128,7 +129,7 @@ function isTransactionThread(parentReportAction: OnyxEntry> = [CONST.IOU.REPORT_ACTION_TYPE.CREATE, CONST.IOU.REPORT_ACTION_TYPE.SPLIT]; const iouRequestActions = reportActions?.filter((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU && iouRequestTypes.includes(action.originalMessage.type)) ?? []; @@ -175,7 +176,7 @@ function getMostRecentIOURequestActionID(reportActions: OnyxTypes.ReportAction[] /** * Returns array of links inside a given report action */ -function extractLinksFromMessageHtml(reportAction: OnyxEntry): string[] { +function extractLinksFromMessageHtml(reportAction: OnyxEntry): string[] { const htmlContent = reportAction?.message?.[0]?.html; // Regex to get link in href prop inside of component @@ -193,7 +194,7 @@ function extractLinksFromMessageHtml(reportAction: OnyxEntry { +function findPreviousAction(reportActions: ReportAction[] | null, actionIndex: number): OnyxEntry { if (!reportActions) { return null; } @@ -215,7 +216,7 @@ function findPreviousAction(reportActions: OnyxTypes.ReportAction[] | null, acti * * @param actionIndex - index of the comment item in state to check */ -function isConsecutiveActionMadeByPreviousActor(reportActions: OnyxTypes.ReportAction[] | null, actionIndex: number): boolean { +function isConsecutiveActionMadeByPreviousActor(reportActions: ReportAction[] | null, actionIndex: number): boolean { const previousAction = findPreviousAction(reportActions, actionIndex); const currentAction = reportActions?.[actionIndex]; @@ -251,7 +252,7 @@ function isConsecutiveActionMadeByPreviousActor(reportActions: OnyxTypes.ReportA /** * Checks if a reportAction is deprecated. */ -function isReportActionDeprecated(reportAction: OnyxEntry, key: string): boolean { +function isReportActionDeprecated(reportAction: OnyxEntry, key: string): boolean { if (!reportAction) { return true; } @@ -270,7 +271,7 @@ function isReportActionDeprecated(reportAction: OnyxEntry, key: string): boolean { +function shouldReportActionBeVisible(reportAction: OnyxEntry, key: string): boolean { if (!reportAction) { return false; } @@ -310,7 +311,7 @@ function shouldReportActionBeVisible(reportAction: OnyxEntry): boolean { +function shouldReportActionBeVisibleAsLastAction(reportAction: OnyxEntry): boolean { if (!reportAction) { return false; } @@ -327,8 +328,8 @@ function shouldReportActionBeVisibleAsLastAction(reportAction: OnyxEntry { - const updatedActionsToMerge: OnyxTypes.ReportActions = {}; +function getLastVisibleAction(reportID: string, actionsToMerge: ReportActions = {}): OnyxEntry { + const updatedActionsToMerge: ReportActions = {}; if (actionsToMerge && Object.keys(actionsToMerge).length !== 0) { Object.keys(actionsToMerge).forEach( (actionToMergeID) => (updatedActionsToMerge[actionToMergeID] = {...allReportActions?.[reportID]?.[actionToMergeID], ...actionsToMerge[actionToMergeID]}), @@ -348,7 +349,7 @@ function getLastVisibleAction(reportID: string, actionsToMerge: OnyxTypes.Report return maxAction ?? null; } -function getLastVisibleMessage(reportID: string, actionsToMerge: OnyxTypes.ReportActions = {}): LastVisibleMessage { +function getLastVisibleMessage(reportID: string, actionsToMerge: ReportActions = {}): LastVisibleMessage { const lastVisibleAction = getLastVisibleAction(reportID, actionsToMerge); const message = lastVisibleAction?.message?.[0]; @@ -375,7 +376,7 @@ function getLastVisibleMessage(reportID: string, actionsToMerge: OnyxTypes.Repor /** * A helper method to filter out report actions keyed by sequenceNumbers. */ -function filterOutDeprecatedReportActions(reportActions: OnyxTypes.ReportActions | null): OnyxTypes.ReportAction[] { +function filterOutDeprecatedReportActions(reportActions: ReportActions | null): ReportAction[] { return Object.entries(reportActions ?? {}) .filter(([key, reportAction]) => !isReportActionDeprecated(reportAction, key)) .map((entry) => entry[1]); @@ -387,7 +388,7 @@ function filterOutDeprecatedReportActions(reportActions: OnyxTypes.ReportActions * to ensure they will always be displayed in the same order (in case multiple actions have the same timestamp). * This is all handled with getSortedReportActions() which is used by several other methods to keep the code DRY. */ -function getSortedReportActionsForDisplay(reportActions: OnyxTypes.ReportActions | null): OnyxTypes.ReportAction[] { +function getSortedReportActionsForDisplay(reportActions: ReportActions | null): ReportAction[] { const filteredReportActions = Object.entries(reportActions ?? {}) .filter(([key, reportAction]) => shouldReportActionBeVisible(reportAction, key)) .map((entry) => entry[1]); @@ -400,7 +401,7 @@ function getSortedReportActionsForDisplay(reportActions: OnyxTypes.ReportActions * Additionally, archived #admins and #announce do not have the closed report action so we will return null if none is found. * */ -function getLastClosedReportAction(reportActions: OnyxTypes.ReportActions | null): OnyxEntry { +function getLastClosedReportAction(reportActions: ReportActions | null): OnyxEntry { // If closed report action is not present, return early if (!Object.values(reportActions ?? {}).some((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED)) { return null; @@ -414,14 +415,14 @@ function getLastClosedReportAction(reportActions: OnyxTypes.ReportActions | null /** * @returns The latest report action in the `onyxData` or `null` if one couldn't be found */ -function getLatestReportActionFromOnyxData(onyxData: OnyxUpdate[] | null): OnyxEntry { +function getLatestReportActionFromOnyxData(onyxData: OnyxUpdate[] | null): OnyxEntry { const reportActionUpdate = onyxData?.find((onyxUpdate) => onyxUpdate.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS)); if (!reportActionUpdate) { return null; } - const reportActions = Object.values((reportActionUpdate.value as OnyxTypes.ReportActions) ?? {}); + const reportActions = Object.values((reportActionUpdate.value as ReportActions) ?? {}); const sortedReportActions = getSortedReportActions(reportActions); return sortedReportActions.at(-1) ?? null; } @@ -437,7 +438,7 @@ function getLinkedTransactionID(reportID: string, reportActionID: string): strin return reportAction.originalMessage.IOUTransactionID ?? null; } -function getReportAction(reportID: string, reportActionID: string): OnyxEntry { +function getReportAction(reportID: string, reportActionID: string): OnyxEntry { return allReportActions?.[reportID]?.[reportActionID] ?? null; } @@ -483,7 +484,7 @@ function getMostRecentReportActionLastModified(): string { /** * @returns The report preview action or `null` if one couldn't be found */ -function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry { +function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry { return ( Object.values(allReportActions?.[chatReportID] ?? {}).find( (reportAction) => reportAction && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && reportAction.originalMessage.linkedReportID === iouReportID, @@ -494,33 +495,33 @@ function getReportPreviewAction(chatReportID: string, iouReportID: string): Onyx /** * Get the iouReportID for a given report action. */ -function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry): string { +function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry): string { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW ? reportAction.originalMessage.linkedReportID : ''; } -function isCreatedTaskReportAction(reportAction: OnyxEntry): boolean { +function isCreatedTaskReportAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && !!reportAction.originalMessage?.taskReportID; } /** * A helper method to identify if the message is deleted or not. */ -function isMessageDeleted(reportAction: OnyxEntry): boolean { +function isMessageDeleted(reportAction: OnyxEntry): boolean { return reportAction?.message?.[0]?.isDeletedParentAction ?? false; } /** * Returns the number of money requests associated with a report preview */ -function getNumberOfMoneyRequests(reportPreviewAction: OnyxEntry): number { +function getNumberOfMoneyRequests(reportPreviewAction: OnyxEntry): number { return reportPreviewAction?.childMoneyRequestCount ?? 0; } -function isSplitBillAction(reportAction: OnyxEntry): boolean { +function isSplitBillAction(reportAction: OnyxEntry): boolean { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU && reportAction.originalMessage.type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT; } -function isTaskAction(reportAction: OnyxEntry): boolean { +function isTaskAction(reportAction: OnyxEntry): boolean { const reportActionName = reportAction?.actionName; return ( reportActionName === CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED || @@ -529,7 +530,7 @@ function isTaskAction(reportAction: OnyxEntry): boolean ); } -function getAllReportActions(reportID: string): OnyxTypes.ReportActions { +function getAllReportActions(reportID: string): ReportActions { return allReportActions?.[reportID] ?? {}; } @@ -538,7 +539,7 @@ function getAllReportActions(reportID: string): OnyxTypes.ReportActions { * * @param reportAction report action */ -function isReportActionAttachment(reportAction: OnyxEntry): boolean { +function isReportActionAttachment(reportAction: OnyxEntry): boolean { const message = reportAction?.message?.[0]; if (reportAction && Object.hasOwn(reportAction, 'isAttachment')) { @@ -555,7 +556,7 @@ function isReportActionAttachment(reportAction: OnyxEntry): boolean { +function isNotifiableReportAction(reportAction: OnyxEntry): boolean { if (!reportAction) { return false; }