From 6ef0b18e168a6ab219a36bc340c1d6f05853a0ff Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 13 Jan 2025 14:55:21 +0100 Subject: [PATCH 1/3] avoid using getOrderedReportIDs when calculating RBR state --- .../BottomTabBar.tsx | 23 +++--------- .../DebugTabView.tsx | 18 ++++------ src/libs/WorkspacesSettingsUtils.ts | 36 +++---------------- 3 files changed, 17 insertions(+), 60 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar.tsx b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar.tsx index 80165073fda8..788469ed2f45 100644 --- a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar.tsx +++ b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar.tsx @@ -10,8 +10,8 @@ import Text from '@components/Text'; import EducationalTooltip from '@components/Tooltip/EducationalTooltip'; import useActiveWorkspace from '@hooks/useActiveWorkspace'; import useBottomTabIsFocused from '@hooks/useBottomTabIsFocused'; -import useCurrentReportID from '@hooks/useCurrentReportID'; import useLocalize from '@hooks/useLocalize'; +import {useReportIDs} from '@hooks/useReportIDs'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; import getPlatform from '@libs/getPlatform'; @@ -70,17 +70,10 @@ function BottomTabBar({selectedTab}: BottomTabBarProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); const {activeWorkspaceID} = useActiveWorkspace(); - const {currentReportID = null} = useCurrentReportID() ?? {}; + const {orderedReportIDs} = useReportIDs(); const [user] = useOnyx(ONYXKEYS.USER); - const [betas] = useOnyx(ONYXKEYS.BETAS); - const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE); - const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); - const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS); - const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); - const [chatTabBrickRoad, setChatTabBrickRoad] = useState(() => - getChatTabBrickRoad(activeWorkspaceID, currentReportID, reports, betas, policies, priorityMode, transactionViolations), - ); + const [chatTabBrickRoad, setChatTabBrickRoad] = useState(undefined); const isFocused = useBottomTabIsFocused(); const platform = getPlatform(); const isWebOrDesktop = platform === CONST.PLATFORM.WEB || platform === CONST.PLATFORM.DESKTOP; @@ -89,10 +82,10 @@ function BottomTabBar({selectedTab}: BottomTabBarProps) { selectedTab !== SCREENS.HOME && isFocused, ); useEffect(() => { - setChatTabBrickRoad(getChatTabBrickRoad(activeWorkspaceID, currentReportID, reports, betas, policies, priorityMode, transactionViolations)); + setChatTabBrickRoad(getChatTabBrickRoad(activeWorkspaceID, orderedReportIDs)); // We need to get a new brick road state when report actions are updated, otherwise we'll be showing an outdated brick road. // That's why reportActions is added as a dependency here - }, [activeWorkspaceID, transactionViolations, reports, reportActions, betas, policies, priorityMode, currentReportID]); + }, [activeWorkspaceID, orderedReportIDs, reportActions]); const navigateToChats = useCallback(() => { if (selectedTab === SCREENS.HOME) { @@ -138,12 +131,6 @@ function BottomTabBar({selectedTab}: BottomTabBarProps) { selectedTab={selectedTab} chatTabBrickRoad={chatTabBrickRoad} activeWorkspaceID={activeWorkspaceID} - reports={reports} - currentReportID={currentReportID} - betas={betas} - policies={policies} - transactionViolations={transactionViolations} - priorityMode={priorityMode} /> )} diff --git a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/DebugTabView.tsx b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/DebugTabView.tsx index 354529941e0c..35ece3c28a3d 100644 --- a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/DebugTabView.tsx +++ b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/DebugTabView.tsx @@ -1,6 +1,6 @@ import React, {useCallback, useMemo} from 'react'; import {View} from 'react-native'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import {useOnyx} from 'react-native-onyx'; import Button from '@components/Button'; import Icon from '@components/Icon'; @@ -9,6 +9,7 @@ import Text from '@components/Text'; import type {IndicatorStatus} from '@hooks/useIndicatorStatus'; import useIndicatorStatus from '@hooks/useIndicatorStatus'; import useLocalize from '@hooks/useLocalize'; +import {useReportIDs} from '@hooks/useReportIDs'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -21,18 +22,12 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; -import type {Beta, Policy, PriorityMode, ReimbursementAccount, Report, TransactionViolations} from '@src/types/onyx'; +import type {ReimbursementAccount} from '@src/types/onyx'; type DebugTabViewProps = { selectedTab?: string; chatTabBrickRoad: BrickRoad; activeWorkspaceID?: string; - currentReportID: string | null; - reports: OnyxCollection; - betas: OnyxEntry; - policies: OnyxCollection; - transactionViolations: OnyxCollection; - priorityMode: OnyxEntry; }; function getSettingsMessage(status: IndicatorStatus | undefined): TranslationPaths | undefined { @@ -97,13 +92,14 @@ function getSettingsRoute(status: IndicatorStatus | undefined, reimbursementAcco } } -function DebugTabView({selectedTab = '', chatTabBrickRoad, activeWorkspaceID, currentReportID, reports, betas, policies, transactionViolations, priorityMode}: DebugTabViewProps) { +function DebugTabView({selectedTab = '', chatTabBrickRoad, activeWorkspaceID}: DebugTabViewProps) { const StyleUtils = useStyleUtils(); const theme = useTheme(); const styles = useThemeStyles(); const {translate} = useLocalize(); const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT); const {status, indicatorColor, policyIDWithErrors} = useIndicatorStatus(); + const {orderedReportIDs} = useReportIDs(); const message = useMemo((): TranslationPaths | undefined => { if (selectedTab === SCREENS.HOME) { @@ -137,7 +133,7 @@ function DebugTabView({selectedTab = '', chatTabBrickRoad, activeWorkspaceID, cu const navigateTo = useCallback(() => { if (selectedTab === SCREENS.HOME && !!chatTabBrickRoad) { - const report = getChatTabBrickRoadReport(activeWorkspaceID, currentReportID, reports, betas, policies, priorityMode, transactionViolations); + const report = getChatTabBrickRoadReport(activeWorkspaceID, orderedReportIDs); if (report) { Navigation.navigate(ROUTES.DEBUG_REPORT.getRoute(report.reportID)); @@ -150,7 +146,7 @@ function DebugTabView({selectedTab = '', chatTabBrickRoad, activeWorkspaceID, cu Navigation.navigate(route); } } - }, [selectedTab, chatTabBrickRoad, activeWorkspaceID, currentReportID, reports, betas, policies, priorityMode, transactionViolations, status, reimbursementAccount, policyIDWithErrors]); + }, [selectedTab, chatTabBrickRoad, activeWorkspaceID, orderedReportIDs, status, reimbursementAccount, policyIDWithErrors]); if (!([SCREENS.HOME, SCREENS.SETTINGS.ROOT] as string[]).includes(selectedTab) || !indicator) { return null; diff --git a/src/libs/WorkspacesSettingsUtils.ts b/src/libs/WorkspacesSettingsUtils.ts index 60dff1c97247..8f3dc15612f7 100644 --- a/src/libs/WorkspacesSettingsUtils.ts +++ b/src/libs/WorkspacesSettingsUtils.ts @@ -2,18 +2,16 @@ import Onyx from 'react-native-onyx'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import type {LocaleContextProps} from '@components/LocaleContextProvider'; -import type {PolicySelector} from '@hooks/useReportIDs'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Beta, Policy, PriorityMode, ReimbursementAccount, Report, ReportAction, ReportActions, TransactionViolation, TransactionViolations} from '@src/types/onyx'; +import type {Policy, ReimbursementAccount, Report, ReportAction, ReportActions, TransactionViolations} from '@src/types/onyx'; import type {PolicyConnectionSyncProgress, Unit} from '@src/types/onyx/Policy'; import {isConnectionInProgress} from './actions/connections'; import * as CurrencyUtils from './CurrencyUtils'; import {isPolicyAdmin, shouldShowCustomUnitsError, shouldShowEmployeeListError, shouldShowPolicyError, shouldShowSyncError, shouldShowTaxRateError} from './PolicyUtils'; import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils'; -import SidebarUtils from './SidebarUtils'; type CheckingMethod = () => boolean; @@ -138,23 +136,8 @@ function hasWorkspaceSettingsRBR(policy: Policy) { ); } -function getChatTabBrickRoadReport( - policyID: string | undefined, - currentReportId: string | null, - reports: OnyxCollection, - betas: OnyxEntry, - policies: OnyxCollection, - priorityMode: OnyxEntry, - transactionViolations: OnyxCollection, - policyMemberAccountIDs: number[] = [], -): OnyxEntry { - const reportIDs = SidebarUtils.getOrderedReportIDs(currentReportId, reports, betas, policies, priorityMode, transactionViolations, policyID, policyMemberAccountIDs); - if (!reportIDs.length) { - return undefined; - } - - const allReports = reportIDs.map((reportID) => reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]); - +function getChatTabBrickRoadReport(policyID: string | undefined, orderedReportIDs: string[] = []): OnyxEntry { + const allReports = orderedReportIDs.map((reportID) => reportsCollection?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]); // If policyID is undefined, then all reports are checked whether they contain any brick road const policyReports = policyID ? Object.values(allReports).filter((report) => report?.policyID === policyID) : Object.values(allReports); @@ -180,17 +163,8 @@ function getChatTabBrickRoadReport( return undefined; } -function getChatTabBrickRoad( - policyID: string | undefined, - currentReportId: string | null, - reports: OnyxCollection, - betas: OnyxEntry, - policies: OnyxCollection, - priorityMode: OnyxEntry, - transactionViolations: OnyxCollection, - policyMemberAccountIDs: number[] = [], -): BrickRoad | undefined { - const report = getChatTabBrickRoadReport(policyID, currentReportId, reports, betas, policies, priorityMode, transactionViolations, policyMemberAccountIDs); +function getChatTabBrickRoad(policyID: string | undefined, orderedReportIDs: string[]): BrickRoad | undefined { + const report = getChatTabBrickRoadReport(policyID, orderedReportIDs); return report ? getBrickRoadForPolicy(report) : undefined; } From 20adc9c6a5b3a50ea456ce260844da834081ba0b Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Wed, 15 Jan 2025 11:55:22 +0100 Subject: [PATCH 2/3] fix changed --- src/libs/WorkspacesSettingsUtils.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libs/WorkspacesSettingsUtils.ts b/src/libs/WorkspacesSettingsUtils.ts index 8f3dc15612f7..8c154f55b6b5 100644 --- a/src/libs/WorkspacesSettingsUtils.ts +++ b/src/libs/WorkspacesSettingsUtils.ts @@ -8,10 +8,10 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, ReimbursementAccount, Report, ReportAction, ReportActions, TransactionViolations} from '@src/types/onyx'; import type {PolicyConnectionSyncProgress, Unit} from '@src/types/onyx/Policy'; import {isConnectionInProgress} from './actions/connections'; -import * as CurrencyUtils from './CurrencyUtils'; +import {convertToDisplayString} from './CurrencyUtils'; import {isPolicyAdmin, shouldShowCustomUnitsError, shouldShowEmployeeListError, shouldShowPolicyError, shouldShowSyncError, shouldShowTaxRateError} from './PolicyUtils'; -import * as ReportActionsUtils from './ReportActionsUtils'; -import * as ReportUtils from './ReportUtils'; +import {getOneTransactionThreadReportID} from './ReportActionsUtils'; +import {getAllReportErrors, hasReportViolations, isReportOwner, isUnread, isUnreadWithMention, requiresAttentionFromCurrentUser, shouldDisplayViolationsRBRInLHN} from './ReportUtils'; type CheckingMethod = () => boolean; @@ -67,13 +67,13 @@ Onyx.connect({ */ const getBrickRoadForPolicy = (report: Report, altReportActions?: OnyxCollection): BrickRoad => { const reportActions = (altReportActions ?? allReportActions)?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {}; - const reportErrors = ReportUtils.getAllReportErrors(report, reportActions); - const oneTransactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, reportActions); + const reportErrors = getAllReportErrors(report, reportActions); + const oneTransactionThreadReportID = getOneTransactionThreadReportID(report.reportID, reportActions); let doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; if (!doesReportContainErrors) { - const shouldDisplayViolations = ReportUtils.shouldDisplayViolationsRBRInLHN(report, allTransactionViolations); - const shouldDisplayReportViolations = ReportUtils.isReportOwner(report) && ReportUtils.hasReportViolations(report.reportID); + const shouldDisplayViolations = shouldDisplayViolationsRBRInLHN(report, allTransactionViolations); + const shouldDisplayReportViolations = isReportOwner(report) && hasReportViolations(report.reportID); const hasViolations = shouldDisplayViolations || shouldDisplayReportViolations; if (hasViolations) { doesReportContainErrors = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR; @@ -83,7 +83,7 @@ const getBrickRoadForPolicy = (report: Report, altReportActions?: OnyxCollection if (oneTransactionThreadReportID && !doesReportContainErrors) { const oneTransactionThreadReport = reportsCollection?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; - if (ReportUtils.shouldDisplayViolationsRBRInLHN(oneTransactionThreadReport, allTransactionViolations)) { + if (shouldDisplayViolationsRBRInLHN(oneTransactionThreadReport, allTransactionViolations)) { doesReportContainErrors = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR; } } @@ -98,8 +98,8 @@ const getBrickRoadForPolicy = (report: Report, altReportActions?: OnyxCollection const itemParentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`] ?? {}; itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : undefined; } - const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)}; - const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(reportOption, itemParentReportAction); + const reportOption = {...report, isUnread: isUnread(report), isUnreadWithMention: isUnreadWithMention(report)}; + const shouldShowGreenDotIndicator = requiresAttentionFromCurrentUser(reportOption, itemParentReportAction); return shouldShowGreenDotIndicator ? CONST.BRICK_ROAD_INDICATOR_STATUS.INFO : undefined; }; @@ -224,7 +224,7 @@ function getWorkspacesUnreadStatuses(reports: OnyxCollection): Record Date: Thu, 23 Jan 2025 13:58:12 +0100 Subject: [PATCH 3/3] add early return --- src/libs/WorkspacesSettingsUtils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/WorkspacesSettingsUtils.ts b/src/libs/WorkspacesSettingsUtils.ts index 8c154f55b6b5..8f571ead56cd 100644 --- a/src/libs/WorkspacesSettingsUtils.ts +++ b/src/libs/WorkspacesSettingsUtils.ts @@ -137,6 +137,10 @@ function hasWorkspaceSettingsRBR(policy: Policy) { } function getChatTabBrickRoadReport(policyID: string | undefined, orderedReportIDs: string[] = []): OnyxEntry { + if (!orderedReportIDs.length) { + return undefined; + } + const allReports = orderedReportIDs.map((reportID) => reportsCollection?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]); // If policyID is undefined, then all reports are checked whether they contain any brick road const policyReports = policyID ? Object.values(allReports).filter((report) => report?.policyID === policyID) : Object.values(allReports);