From 45a3c12b3ab8bc50ab666162ea1d81ede2a84663 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 20 Jun 2024 11:17:26 +0700 Subject: [PATCH] create a separate method --- src/libs/ReportUtils.ts | 33 +++++++++++++++---- src/libs/actions/ReportActions.ts | 7 +--- .../BaseReportActionContextMenu.tsx | 4 +-- src/pages/home/report/ReportActionItem.tsx | 6 ++-- .../step/IOURequestStepParticipants.tsx | 2 +- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 28f36afb03c6..c0c3f5a6dfa0 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -886,11 +886,16 @@ function isPolicyExpenseChat(report: OnyxInputOrEntry | Participant | Em return getChatType(report) === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT || (report?.isPolicyExpenseChat ?? false); } -function isInvoiceRoom(reportOrID: OnyxEntry | EmptyObject | string): boolean { - const report = typeof reportOrID === 'string' ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] : reportOrID; +function isInvoiceRoom(report: OnyxEntry | EmptyObject): boolean { return getChatType(report) === CONST.REPORT.CHAT_TYPE.INVOICE; } +function isInvoiceRoomWithID(reportID?: string): boolean { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID || -1}`]; + return isInvoiceRoom(report); +} + /** * Checks if a report is a completed task report. */ @@ -1232,8 +1237,7 @@ function isClosedExpenseReportWithNoExpenses(report: OnyxEntry): boolean /** * Whether the provided report is an archived room */ -function isArchivedRoom(reportOrID: OnyxInputOrEntry | EmptyObject | string, reportNameValuePairs?: OnyxInputOrEntry | EmptyObject): boolean { - const report = typeof reportOrID === 'string' ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] : reportOrID; +function isArchivedRoom(report: OnyxInputOrEntry | EmptyObject, reportNameValuePairs?: OnyxInputOrEntry | EmptyObject): boolean { if (reportNameValuePairs) { return reportNameValuePairs.isArchived; } @@ -1241,6 +1245,15 @@ function isArchivedRoom(reportOrID: OnyxInputOrEntry | EmptyObject | str return report?.statusNum === CONST.REPORT.STATUS_NUM.CLOSED && report?.stateNum === CONST.REPORT.STATE_NUM.APPROVED; } +/** + * Whether the report with the provided reportID is an archived room + */ +function isArchivedRoomWithID(reportID?: string) { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID || -1}`]; + return isArchivedRoom(report); +} + /** * Whether the provided report is a closed report */ @@ -5526,12 +5539,17 @@ function getAllPolicyReports(policyID: string): Array> { /** * Returns true if Chronos is one of the chat participants (1:1) */ -function chatIncludesChronos(reportOrID: OnyxInputOrEntry | EmptyObject | string): boolean { - const report = typeof reportOrID === 'string' ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] : reportOrID; +function chatIncludesChronos(report: OnyxInputOrEntry | EmptyObject): boolean { const participantAccountIDs = Object.keys(report?.participants ?? {}).map(Number); return participantAccountIDs.includes(CONST.ACCOUNT_ID.CHRONOS); } +function chatIncludesChronosWithID(reportID?: string): boolean { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID || -1}`]; + return chatIncludesChronos(report); +} + /** * Can only flag if: * @@ -7025,6 +7043,7 @@ export { canShowReportRecipientLocalTime, canUserPerformWriteAction, chatIncludesChronos, + chatIncludesChronosWithID, chatIncludesConcierge, createDraftTransactionAndNavigateToParticipantSelector, doesReportBelongToWorkspace, @@ -7130,6 +7149,7 @@ export { isAllowedToSubmitDraftExpenseReport, isAnnounceRoom, isArchivedRoom, + isArchivedRoomWithID, isClosedReport, isCanceledTaskReport, isChatReport, @@ -7201,6 +7221,7 @@ export { isValidReportIDFromPath, isWaitingForAssigneeToCompleteTask, isInvoiceRoom, + isInvoiceRoomWithID, isInvoiceReport, isOpenInvoiceReport, canWriteInReport, diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index 124a1dc5f9fd..4435978075e0 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -14,12 +14,7 @@ let allReportActions: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, - callback: (actions) => { - if (!actions) { - return; - } - allReportActions = actions; - }, + callback: (value) => (allReportActions = value), }); let allReports: OnyxCollection; diff --git a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx index 24ea61e3ffc1..f1e541d2fa91 100755 --- a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx +++ b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx @@ -218,8 +218,8 @@ function BaseReportActionContextMenu({ checkIfContextMenuActive?.(); setShouldKeepOpen(false); }, - ReportUtils.isArchivedRoom(originalReportID), - ReportUtils.chatIncludesChronos(originalReportID), + ReportUtils.isArchivedRoomWithID(originalReportID), + ReportUtils.chatIncludesChronosWithID(originalReportID), undefined, undefined, filteredContextMenuActions, diff --git a/src/pages/home/report/ReportActionItem.tsx b/src/pages/home/report/ReportActionItem.tsx index 7399db1df15f..b9dda0b5abd9 100644 --- a/src/pages/home/report/ReportActionItem.tsx +++ b/src/pages/home/report/ReportActionItem.tsx @@ -342,8 +342,8 @@ function ReportActionItem({ draftMessage ?? '', () => setIsContextMenuActive(true), toggleContextMenuFromActiveReportAction, - ReportUtils.isArchivedRoom(originalReportID), - ReportUtils.chatIncludesChronos(originalReportID), + ReportUtils.isArchivedRoomWithID(originalReportID), + ReportUtils.chatIncludesChronosWithID(originalReportID), false, false, [], @@ -868,7 +868,7 @@ function ReportActionItem({ disabledActions={!ReportUtils.canWriteInReport(report) ? RestrictedReadOnlyContextMenuActions : []} isVisible={hovered && draftMessage === undefined && !hasErrors} draftMessage={draftMessage} - isChronosReport={ReportUtils.chatIncludesChronos(originalReportID)} + isChronosReport={ReportUtils.chatIncludesChronosWithID(originalReportID)} checkIfContextMenuActive={toggleContextMenuFromActiveReportAction} setIsEmojiPickerActive={setIsEmojiPickerActive} /> diff --git a/src/pages/iou/request/step/IOURequestStepParticipants.tsx b/src/pages/iou/request/step/IOURequestStepParticipants.tsx index cd2d9982b49d..7fbc8d260f8a 100644 --- a/src/pages/iou/request/step/IOURequestStepParticipants.tsx +++ b/src/pages/iou/request/step/IOURequestStepParticipants.tsx @@ -94,7 +94,7 @@ function IOURequestStepParticipants({ const firstParticipantReportID = val[0]?.reportID ?? ''; const rateID = DistanceRequestUtils.getCustomUnitRateID(firstParticipantReportID); - const isInvoice = iouType === CONST.IOU.TYPE.INVOICE && ReportUtils.isInvoiceRoom(firstParticipantReportID); + const isInvoice = iouType === CONST.IOU.TYPE.INVOICE && ReportUtils.isInvoiceRoomWithID(firstParticipantReportID); numberOfParticipants.current = val.length; IOU.setMoneyRequestParticipants(transactionID, val);