From 7da324b736eac985cef9268c23f39616e0b46873 Mon Sep 17 00:00:00 2001 From: truph01 Date: Thu, 4 Jul 2024 16:25:42 +0700 Subject: [PATCH 1/7] Fix: Add red dot to one transaction reports --- .../LHNOptionsList/OptionRowLHNData.tsx | 1 + src/libs/SidebarUtils.ts | 44 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/components/LHNOptionsList/OptionRowLHNData.tsx b/src/components/LHNOptionsList/OptionRowLHNData.tsx index 8d61058ed5be8..7377e81387595 100644 --- a/src/components/LHNOptionsList/OptionRowLHNData.tsx +++ b/src/components/LHNOptionsList/OptionRowLHNData.tsx @@ -47,6 +47,7 @@ function OptionRowLHNData({ policy, parentReportAction, hasViolations: !!shouldDisplayViolations, + transactionViolations, }); if (deepEqual(item, optionItemRef.current)) { return optionItemRef.current; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index b7d365a103ae5..d91758092f326 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -2,6 +2,7 @@ import {Str} from 'expensify-common'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {ChatReportSelector, PolicySelector, ReportActionsSelector} from '@hooks/useReportIDs'; +import Permissions from '@libs/Permissions'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {PersonalDetails, PersonalDetailsList, ReportActions, TransactionViolation} from '@src/types/onyx'; @@ -22,6 +23,21 @@ import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; +let allReports: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + allReports = reports; + }, +}); + +let allBetas: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.BETAS, + callback: (value) => (allBetas = value), +}); + const visibleReportActionItems: ReportActions = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -92,8 +108,17 @@ function getOrderedReportIDs( const isHidden = report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; const isFocused = report.reportID === currentReportId; const allReportErrors = OptionsListUtils.getAllReportErrors(report, reportActions) ?? {}; + const transactionReportActions = ReportActionsUtils.getAllReportActions(report.reportID); + const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, transactionReportActions, undefined); + let doesTransactionThreadReportHasViolations = false; + if (oneTransactionThreadReportID) { + const transactionReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; + doesTransactionThreadReportHasViolations = OptionsListUtils.shouldShowViolations(transactionReport, betas ?? [], transactionViolations); + } const hasErrorsOtherThanFailedReceipt = - doesReportHaveViolations || Object.values(allReportErrors).some((error) => error?.[0] !== Localize.translateLocal('iou.error.genericSmartscanFailureMessage')); + doesTransactionThreadReportHasViolations || + doesReportHaveViolations || + Object.values(allReportErrors).some((error) => error?.[0] !== Localize.translateLocal('iou.error.genericSmartscanFailureMessage')); if (ReportUtils.isOneTransactionThread(report.reportID, report.parentReportID ?? '0')) { return; } @@ -211,6 +236,7 @@ function getOptionData({ policy, parentReportAction, hasViolations, + transactionViolations, }: { report: OnyxEntry; reportActions: OnyxEntry; @@ -219,6 +245,7 @@ function getOptionData({ policy: OnyxEntry | undefined; parentReportAction: OnyxEntry | undefined; hasViolations: boolean; + transactionViolations: OnyxCollection; }): ReportUtils.OptionData | undefined { // When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for // this method to be called after the Onyx data has been cleared out. In that case, it's fine to do @@ -278,6 +305,21 @@ function getOptionData({ result.shouldShowSubscript = ReportUtils.shouldReportShowSubscript(report); result.pendingAction = report.pendingFields?.addWorkspaceRoom ?? report.pendingFields?.createChat; result.brickRoadIndicator = hasErrors || hasViolations ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; + const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, ReportActionsUtils.getAllReportActions(report.reportID)); + if (oneTransactionThreadReportID) { + const oneTransactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; + + if ( + Permissions.canUseViolations(allBetas) && + ReportUtils.shouldDisplayTransactionThreadViolations( + oneTransactionThreadReport, + transactionViolations, + ReportActionsUtils.getAllReportActions(report.reportID)[oneTransactionThreadReport?.parentReportActionID ?? '-1'], + ) + ) { + result.brickRoadIndicator = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR; + } + } result.ownerAccountID = report.ownerAccountID; result.managerID = report.managerID; result.reportID = report.reportID; From 39f8fedc2135017460591dcc9ca25f216ac99914 Mon Sep 17 00:00:00 2001 From: truph01 Date: Mon, 8 Jul 2024 16:13:26 +0700 Subject: [PATCH 2/7] Fix: Lint --- src/libs/SidebarUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 62e83b221add6..619221f027816 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -113,7 +113,7 @@ function getOrderedReportIDs( let doesTransactionThreadReportHasViolations = false; if (oneTransactionThreadReportID) { const transactionReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; - doesTransactionThreadReportHasViolations = OptionsListUtils.shouldShowViolations(transactionReport, betas ?? [], transactionViolations); + doesTransactionThreadReportHasViolations = !!transactionReport && OptionsListUtils.shouldShowViolations(transactionReport, betas ?? [], transactionViolations); } const hasErrorsOtherThanFailedReceipt = doesTransactionThreadReportHasViolations || From 1bef82d7d07ef66f7c5a85fb1a8b3500ad4d598c Mon Sep 17 00:00:00 2001 From: truph01 Date: Mon, 8 Jul 2024 16:22:39 +0700 Subject: [PATCH 3/7] Fix: Typescript --- src/libs/SidebarUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 619221f027816..2b1cf95b514b0 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -245,7 +245,7 @@ function getOptionData({ policy: OnyxEntry | undefined; parentReportAction: OnyxEntry | undefined; hasViolations: boolean; - transactionViolations: OnyxCollection; + transactionViolations?: OnyxCollection; }): ReportUtils.OptionData | undefined { // When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for // this method to be called after the Onyx data has been cleared out. In that case, it's fine to do From 805f6ae3156f29af102415a0d5960a6076fc8563 Mon Sep 17 00:00:00 2001 From: truph01 Date: Mon, 8 Jul 2024 16:28:51 +0700 Subject: [PATCH 4/7] Fix: Remove allReports --- src/libs/SidebarUtils.ts | 14 +++----------- src/libs/actions/Task.ts | 1 + 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 2b1cf95b514b0..770f81a6cb498 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -12,6 +12,7 @@ import type PriorityMode from '@src/types/onyx/PriorityMode'; import type Report from '@src/types/onyx/Report'; import type ReportAction from '@src/types/onyx/ReportAction'; import type DeepValueOf from '@src/types/utils/DeepValueOf'; +import {getReport} from './actions/Task'; import * as CollectionUtils from './CollectionUtils'; import {hasValidDraftComment} from './DraftCommentUtils'; import localeCompare from './LocaleCompare'; @@ -23,15 +24,6 @@ import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; -let allReports: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (reports) => { - allReports = reports; - }, -}); - let allBetas: OnyxEntry; Onyx.connect({ key: ONYXKEYS.BETAS, @@ -112,7 +104,7 @@ function getOrderedReportIDs( const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, transactionReportActions, undefined); let doesTransactionThreadReportHasViolations = false; if (oneTransactionThreadReportID) { - const transactionReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; + const transactionReport = getReport(oneTransactionThreadReportID); doesTransactionThreadReportHasViolations = !!transactionReport && OptionsListUtils.shouldShowViolations(transactionReport, betas ?? [], transactionViolations); } const hasErrorsOtherThanFailedReceipt = @@ -307,7 +299,7 @@ function getOptionData({ result.brickRoadIndicator = hasErrors || hasViolations ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, ReportActionsUtils.getAllReportActions(report.reportID)); if (oneTransactionThreadReportID) { - const oneTransactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; + const oneTransactionThreadReport = getReport(oneTransactionThreadReportID); if ( Permissions.canUseViolations(allBetas) && diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 013ae698ed3f9..117012f566e87 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -1169,6 +1169,7 @@ export { clearTaskErrors, canModifyTask, setNewOptimisticAssignee, + getReport, }; export type {PolicyValue, Assignee, ShareDestination}; From d8e6c25eaff41d8ab8d5d5b0d9d6fa86c008066e Mon Sep 17 00:00:00 2001 From: truph01 Date: Mon, 8 Jul 2024 17:13:26 +0700 Subject: [PATCH 5/7] Fix: Typecheck --- src/libs/SidebarUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 2a60283ee78d2..8592df46137de 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -18,6 +18,7 @@ import localeCompare from './LocaleCompare'; import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as OptionsListUtils from './OptionsListUtils'; +import Permissions from './Permissions'; import * as PolicyUtils from './PolicyUtils'; import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils'; From 9c8ef36c53bb03378ac0bcb37e5c2cbbd551500c Mon Sep 17 00:00:00 2001 From: truph01 Date: Thu, 11 Jul 2024 04:20:18 +0700 Subject: [PATCH 6/7] Fix: Create getReport function in ReportUtils --- src/libs/ReportUtils.ts | 9 +++++++++ src/libs/SidebarUtils.ts | 5 ++--- src/libs/actions/Task.ts | 10 +--------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0a333b2f1f39a..66bf90330839b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -22,6 +22,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; +import type * as OnyxTypes from '@src/types/onyx'; import type { Beta, OnyxInputOrEntry, @@ -607,6 +608,13 @@ function isDraftReport(reportID: string | undefined): boolean { return !!draftReport; } +/** + * Returns the report + */ +function getReport(reportID: string): OnyxEntry { + return ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; +} + /** * Returns the parentReport if the given report is a thread */ @@ -7366,6 +7374,7 @@ export { findPolicyExpenseChatByPolicyID, hasOnlyNonReimbursableTransactions, getMostRecentlyVisitedReport, + getReport, }; export type { diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 6d53df3c7c7f8..207c8adf8e6ca 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -11,7 +11,6 @@ import type PriorityMode from '@src/types/onyx/PriorityMode'; import type Report from '@src/types/onyx/Report'; import type ReportAction from '@src/types/onyx/ReportAction'; import type DeepValueOf from '@src/types/utils/DeepValueOf'; -import {getReport} from './actions/Task'; import * as CollectionUtils from './CollectionUtils'; import {hasValidDraftComment} from './DraftCommentUtils'; import localeCompare from './LocaleCompare'; @@ -104,7 +103,7 @@ function getOrderedReportIDs( const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, transactionReportActions, undefined); let doesTransactionThreadReportHasViolations = false; if (oneTransactionThreadReportID) { - const transactionReport = getReport(oneTransactionThreadReportID); + const transactionReport = ReportUtils.getReport(oneTransactionThreadReportID); doesTransactionThreadReportHasViolations = !!transactionReport && OptionsListUtils.shouldShowViolations(transactionReport, betas ?? [], transactionViolations); } const hasErrorsOtherThanFailedReceipt = @@ -300,7 +299,7 @@ function getOptionData({ result.brickRoadIndicator = hasErrors || hasViolations ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, ReportActionsUtils.getAllReportActions(report.reportID)); if (oneTransactionThreadReportID) { - const oneTransactionThreadReport = getReport(oneTransactionThreadReportID); + const oneTransactionThreadReport = ReportUtils.getReport(oneTransactionThreadReportID); if ( Permissions.canUseViolations(allBetas) && diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 83cd5decff1c0..964f5be0129bc 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -126,7 +126,7 @@ function createTaskAndNavigate( const currentTime = DateUtils.getDBTimeWithSkew(); const lastCommentText = ReportUtils.formatReportLastMessageText(ReportActionsUtils.getReportActionText(optimisticAddCommentReport.reportAction)); - const parentReport = getReport(parentReportID); + const parentReport = ReportUtils.getReport(parentReportID); const optimisticParentReport = { lastVisibleActionCreated: optimisticAddCommentReport.reportAction.created, lastMessageText: lastCommentText, @@ -906,13 +906,6 @@ function getParentReport(report: OnyxEntry): OnyxEntry { - return ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; -} - /** * Cancels a task by setting the report state to SUBMITTED and status to CLOSED */ @@ -1163,7 +1156,6 @@ export { clearTaskErrors, canModifyTask, setNewOptimisticAssignee, - getReport, }; export type {PolicyValue, Assignee, ShareDestination}; From 9392dc99793008311888d3430facfdcc5ac85b46 Mon Sep 17 00:00:00 2001 From: truph01 Date: Thu, 11 Jul 2024 04:31:37 +0700 Subject: [PATCH 7/7] Fix: Lint --- src/libs/ReportUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 66bf90330839b..c908bf8f0f1b3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -22,7 +22,6 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; -import type * as OnyxTypes from '@src/types/onyx'; import type { Beta, OnyxInputOrEntry, @@ -611,7 +610,7 @@ function isDraftReport(reportID: string | undefined): boolean { /** * Returns the report */ -function getReport(reportID: string): OnyxEntry { +function getReport(reportID: string): OnyxEntry { return ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; }