From a39db3ca63ebb607f319007f4bb095fbdf07c780 Mon Sep 17 00:00:00 2001 From: VH Date: Sun, 6 Sep 2026 16:01:35 +0700 Subject: [PATCH 1/5] refactor(ReportUtils): pass derivedIsEmptyReport from outside Remove module-level reportAttributesDerivedValue read inside isEmptyReport() by accepting an optional derivedIsEmptyReport parameter, mirroring Phase 2's derivedReportName pattern. - Add reportIsEmptySelector to ReportAttributes selectors - Add useDerivedIsEmptyReport hook - Thread derivedIsEmptyReport through isUnread, reasonForReportToBeInOptionList, and ShouldReportBeInOptionListParams - Update call sites in SidebarUtils, OptionsListUtils, DebugUtils, MarkAllMessageAsRead, and components/hooks - Add tests for isEmptyReport cache hit/miss and new hook Part of #66427 (Phase 4) --- .../MoneyRequestReportActionsList.tsx | 4 +- src/hooks/useMarkAsRead.ts | 6 +- src/hooks/useReportActionsListModel.ts | 3 + src/hooks/useReportAttributes.ts | 17 ++- src/libs/DebugUtils.ts | 3 + .../MarkAllMessagesAsReadHandler.tsx | 9 +- src/libs/OptionsListUtils/index.ts | 2 +- src/libs/ReportUtils.ts | 29 +++-- src/libs/SidebarUtils.ts | 15 ++- src/libs/UnreadIndicatorUpdater/index.ts | 2 +- .../actions/Report/MarkAllMessageAsRead.tsx | 6 +- src/pages/Debug/Report/DebugReportPage.tsx | 4 + .../BaseReportActionContextMenu.tsx | 5 +- .../inbox/report/ReportActionsListContext.tsx | 3 +- src/pages/inbox/sidebar/InboxTabSelector.tsx | 4 +- src/selectors/ReportAttributes.ts | 4 +- tests/actions/ReportTest.ts | 20 ++-- tests/unit/ReportActionsListContextTest.ts | 1 + tests/unit/ReportUtilsTest.ts | 70 +++++++++++- tests/unit/useReportAttributesTest.ts | 102 +++++++++++++++++- 20 files changed, 261 insertions(+), 48 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index cc5cb272eab1..a68a3048c840 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -12,6 +12,7 @@ import useOnyx from '@hooks/useOnyx'; import usePaginatedReportActions from '@hooks/usePaginatedReportActions'; import useParentReportAction from '@hooks/useParentReportAction'; import usePrevious from '@hooks/usePrevious'; +import {useDerivedIsEmptyReport} from '@hooks/useReportAttributes'; import useReportIsArchived from '@hooks/useReportIsArchived'; import useReportScrollManager from '@hooks/useReportScrollManager'; import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection'; @@ -186,6 +187,7 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); const isReportArchived = useReportIsArchived(reportID); + const derivedIsEmptyReport = useDerivedIsEmptyReport(reportID); const canPerformWriteAction = canUserPerformWriteAction(report, isReportArchived); const [visibleReportActionsData] = useOnyx(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS); @@ -385,7 +387,7 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) return; } - if (isUnread(report, transactionThreadReport, isReportArchived) || (lastAction && isCurrentActionUnread(report, lastAction, visibleReportActions))) { + if (isUnread(report, transactionThreadReport, isReportArchived, derivedIsEmptyReport) || (lastAction && isCurrentActionUnread(report, lastAction, visibleReportActions))) { // On desktop, when the notification center is displayed, isVisible will return false. // Currently, there's no programmatic way to dismiss the notification center panel. // To handle this, we use the 'referrer' parameter to check if the current navigation is triggered from a notification. diff --git a/src/hooks/useMarkAsRead.ts b/src/hooks/useMarkAsRead.ts index 58842a847a56..9bfe073ffc70 100644 --- a/src/hooks/useMarkAsRead.ts +++ b/src/hooks/useMarkAsRead.ts @@ -23,6 +23,7 @@ import useAppFocusEvent from './useAppFocusEvent'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useIsAnonymousUser from './useIsAnonymousUser'; import useIsReportActionsLoaded from './useIsReportActionsLoaded'; +import {useDerivedIsEmptyReport} from './useReportAttributes'; import useReportIsArchived from './useReportIsArchived'; // useRef gets reset when the reportID changes (the list reuses the same instance per report), @@ -52,6 +53,7 @@ function useMarkAsRead({reportID, report, transactionThreadReport, sortedVisible const route = useRoute>(); const isFocused = useIsFocused(); const isReportArchived = useReportIsArchived(reportID); + const derivedIsEmptyReport = useDerivedIsEmptyReport(reportID); const isReportActionsLoaded = useIsReportActionsLoaded(reportID); const [isVisible, setIsVisible] = useState(Visibility.isVisible); @@ -73,7 +75,7 @@ function useMarkAsRead({reportID, report, transactionThreadReport, sortedVisible const didMarkReportAsReadInitially = useRef(false); const lastAction = sortedVisibleReportActions.at(0); - const isReportUnreadValue = isUnread(report, transactionThreadReport, isReportArchived) || (!!lastAction && isCurrentActionUnread(report, lastAction)); + const isReportUnreadValue = isUnread(report, transactionThreadReport, isReportArchived, derivedIsEmptyReport) || (!!lastAction && isCurrentActionUnread(report, lastAction)); useEffect(() => { userActiveSince.current = DateUtils.getDBTime(); @@ -111,7 +113,7 @@ function useMarkAsRead({reportID, report, transactionThreadReport, sortedVisible } const isLastActionUnread = !!lastAction && isCurrentActionUnread(report, lastAction, sortedVisibleReportActions); - if (!isUnread(report, transactionThreadReport, isReportArchived) && !isLastActionUnread) { + if (!isUnread(report, transactionThreadReport, isReportArchived, derivedIsEmptyReport) && !isLastActionUnread) { return; } const isFromNotification = route?.params?.referrer === CONST.REFERRER.NOTIFICATION; diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index b3e438fd3adb..726f9094fb1d 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -18,6 +18,7 @@ import useOnyx from './useOnyx'; import useParentReportAction from './useParentReportAction'; import useReportActionsPagination from './useReportActionsPagination'; import useReportActionsVisibility from './useReportActionsVisibility'; +import {useDerivedIsEmptyReport} from './useReportAttributes'; import useReportIsArchived from './useReportIsArchived'; /** @@ -65,6 +66,7 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea const shouldBeAlignedToTop = shouldReportAlignToTop(report, parentReportAction); const isReportArchived = useReportIsArchived(reportID); + const derivedIsEmptyReport = useDerivedIsEmptyReport(reportID); const canPerformWriteAction = !!canUserPerformWriteAction(report, isReportArchived); const isAppLoadPending = useIsAppLoadPending(); @@ -138,6 +140,7 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea isConciergeMainDM, hasCachedReportActions, showConciergeSidePanelWelcome, + derivedIsEmptyReport, }; // The render state slice on `ReportActionsListStateContext`; this is what drives list re-renders. diff --git a/src/hooks/useReportAttributes.ts b/src/hooks/useReportAttributes.ts index 48622b540c5a..0f13893c4f99 100644 --- a/src/hooks/useReportAttributes.ts +++ b/src/hooks/useReportAttributes.ts @@ -3,7 +3,7 @@ import type {ReportAttributesDerivedValue} from '@src/types/onyx'; import type {OnyxEntry} from 'react-native-onyx'; -import {reportNameSelector} from '@selectors/ReportAttributes'; +import {reportIsEmptySelector, reportNameSelector} from '@selectors/ReportAttributes'; import useOnyx from './useOnyx'; @@ -68,5 +68,18 @@ function useDerivedReportNamesByReportIDs(reportIDs: Array) return reportNames; } +/** + * Returns a single report's cached `isEmpty` flag using a selector. + * + * The selector output is a primitive boolean, so its comparison is trivial and the component re-renders only when + * that specific report's emptiness changes — not on every global report attribute change. + */ +function useDerivedIsEmptyReport(reportID: string | undefined) { + const [isEmpty] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, { + selector: (value: OnyxEntry) => reportIsEmptySelector(value, reportID), + }); + return isEmpty; +} + export default useReportAttributes; -export {useReportAttributesByID, useDerivedReportNameByReportID, useDerivedReportNamesByReportIDs}; +export {useReportAttributesByID, useDerivedReportNameByReportID, useDerivedReportNamesByReportIDs, useDerivedIsEmptyReport}; diff --git a/src/libs/DebugUtils.ts b/src/libs/DebugUtils.ts index 96dd26eca237..86144b03f188 100644 --- a/src/libs/DebugUtils.ts +++ b/src/libs/DebugUtils.ts @@ -1486,6 +1486,7 @@ function getReasonForShowingRowInLHN({ currentUserAccountID, conciergeReportID, hasGuidesEmails, + derivedIsEmptyReport, }: { report: OnyxEntry; chatReport: OnyxEntry; @@ -1499,6 +1500,7 @@ function getReasonForShowingRowInLHN({ currentUserAccountID?: number; hasGuidesEmails: boolean; conciergeReportID: string | undefined; + derivedIsEmptyReport?: boolean; }): TranslationPaths | null { if (!report) { return null; @@ -1519,6 +1521,7 @@ function getReasonForShowingRowInLHN({ currentUserLogin, currentUserAccountID, conciergeReportID, + derivedIsEmptyReport, hasGuidesEmails, }); diff --git a/src/libs/Navigation/AppNavigator/KeyboardShortcutsHandler/MarkAllMessagesAsReadHandler.tsx b/src/libs/Navigation/AppNavigator/KeyboardShortcutsHandler/MarkAllMessagesAsReadHandler.tsx index 16f75378ab56..e9b845bf1cd3 100644 --- a/src/libs/Navigation/AppNavigator/KeyboardShortcutsHandler/MarkAllMessagesAsReadHandler.tsx +++ b/src/libs/Navigation/AppNavigator/KeyboardShortcutsHandler/MarkAllMessagesAsReadHandler.tsx @@ -1,4 +1,5 @@ import useOnyx from '@hooks/useOnyx'; +import useReportAttributes from '@hooks/useReportAttributes'; import markAllMessagesAsRead from '@libs/actions/Report/MarkAllMessageAsRead'; import KeyboardShortcut from '@libs/KeyboardShortcut'; @@ -11,19 +12,25 @@ import {useEffect, useRef} from 'react'; function MarkAllMessagesAsReadHandler() { const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {selector: reportNameValuePairsArchivedSelector}); + const reportAttributesDerived = useReportAttributes(); // The keyboard-shortcut callback below is registered once on mount, so keep the latest archived state in a ref // for it to read at fire time instead of closing over a stale value. const reportNameValuePairsRef = useRef(reportNameValuePairs); + const reportAttributesDerivedRef = useRef(reportAttributesDerived); useEffect(() => { reportNameValuePairsRef.current = reportNameValuePairs; }, [reportNameValuePairs]); + useEffect(() => { + reportAttributesDerivedRef.current = reportAttributesDerived; + }, [reportAttributesDerived]); + useEffect(() => { const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.MARK_ALL_MESSAGES_AS_READ; const unsubscribe = KeyboardShortcut.subscribe( shortcutConfig.shortcutKey, - () => markAllMessagesAsRead(reportNameValuePairsRef.current), + () => markAllMessagesAsRead(reportNameValuePairsRef.current, reportAttributesDerivedRef.current), shortcutConfig.descriptionKey, shortcutConfig.modifiers, true, diff --git a/src/libs/OptionsListUtils/index.ts b/src/libs/OptionsListUtils/index.ts index 9f42fee2d6d5..313b8ea452b9 100644 --- a/src/libs/OptionsListUtils/index.ts +++ b/src/libs/OptionsListUtils/index.ts @@ -1930,7 +1930,7 @@ function prepareReportOptionsForDisplay( : undefined; const oneTransactionThreadReport = oneTransactionThreadReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`] : undefined; - isOptionUnread = isUnread(report, oneTransactionThreadReport, option.private_isArchived) && !!report.lastActorAccountID; + isOptionUnread = isUnread(report, oneTransactionThreadReport, option.private_isArchived, reportAttributesDerived?.[report.reportID]?.isEmpty) && !!report.lastActorAccountID; } let lastIOUCreationDate; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 6efa65d8eff7..4efef19ae517 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -9611,20 +9611,15 @@ function buildOptimisticMoneyRequestEntities({ /** * Check if the report is empty, meaning it has no visible messages (i.e. only a "created" report action). - * Added caching mechanism via derived values. + * + * @param derivedIsEmptyReport Always prefer passing this cached flag for this report from ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, i.e. + * `reportAttributes?.[reportID]?.isEmpty`. */ -function isEmptyReport(report: OnyxEntry, isReportArchived: boolean | undefined): boolean { +function isEmptyReport(report: OnyxEntry, isReportArchived: boolean | undefined, derivedIsEmptyReport: boolean | undefined): boolean { if (!report) { return true; } - - // Get the `isEmpty` state from cached report attributes - const attributes = reportAttributesDerivedValue?.[report.reportID]; - if (attributes) { - return attributes.isEmpty; - } - - return generateIsEmptyReport(report, isReportArchived); + return derivedIsEmptyReport ?? generateIsEmptyReport(report, isReportArchived); } /** @@ -9724,12 +9719,12 @@ function generateIsEmptyReport(report: OnyxEntry, isReportArchived: bool } // We need oneTransactionThreadReport to get the correct last visible action created -function isUnread(report: OnyxEntry, oneTransactionThreadReport: OnyxEntry, isReportArchived: boolean | undefined): boolean { +function isUnread(report: OnyxEntry, oneTransactionThreadReport: OnyxEntry, isReportArchived: boolean | undefined, derivedIsEmptyReport: boolean | undefined): boolean { if (!report) { return false; } - if (isEmptyReport(report, isReportArchived)) { + if (isEmptyReport(report, isReportArchived, derivedIsEmptyReport)) { return false; } @@ -10270,6 +10265,8 @@ type ShouldReportBeInOptionListParams = { conciergeReportID: string | undefined; /** Pre-computed value from reportAttributes derived value. When provided, skips the expensive requiresAttentionFromCurrentUser recomputation. */ requiresAttention?: boolean; + /** Pre-computed isEmpty flag from reportAttributes derived value. When provided, skips the module-level reportAttributesDerivedValue read inside isEmptyReport. */ + derivedIsEmptyReport?: boolean; hasGuidesEmails: boolean; }; @@ -10290,6 +10287,7 @@ function reasonForReportToBeInOptionList({ isReportArchived, conciergeReportID, requiresAttention, + derivedIsEmptyReport, hasGuidesEmails, }: ShouldReportBeInOptionListParams): ValueOf | null { const isInDefaultMode = !isInFocusMode; @@ -10338,7 +10336,7 @@ function reasonForReportToBeInOptionList({ // We used to use the system DM for A/B testing onboarding tasks, but now only create them in the Concierge chat. We // still need to allow existing users who have tasks in the system DM to see them, but otherwise we don't need to // show that chat - if (report?.participants?.[CONST.ACCOUNT_ID.NOTIFICATIONS] && isEmptyReport(report, isReportArchived)) { + if (report?.participants?.[CONST.ACCOUNT_ID.NOTIFICATIONS] && isEmptyReport(report, isReportArchived, derivedIsEmptyReport)) { return null; } @@ -10375,7 +10373,7 @@ function reasonForReportToBeInOptionList({ return CONST.REPORT_IN_LHN_REASONS.HAS_GBR; } - const isEmptyChat = isEmptyReport(report, isReportArchived); + const isEmptyChat = isEmptyReport(report, isReportArchived, derivedIsEmptyReport); const canHideReport = shouldHideReport(report, currentReportId, isReportArchived); // Drafts already return early above, so no draft check needed here @@ -10416,7 +10414,7 @@ function reasonForReportToBeInOptionList({ if (isInFocusMode) { const oneTransactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, currentReportActions); const oneTransactionThreadReport = deprecatedAllReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; - return isUnread(report, oneTransactionThreadReport, isReportArchived) && getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE + return isUnread(report, oneTransactionThreadReport, isReportArchived, derivedIsEmptyReport) && getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE ? CONST.REPORT_IN_LHN_REASONS.IS_UNREAD : null; } @@ -14631,6 +14629,7 @@ export { isDefaultRoom, isDeprecatedGroupDM, generateIsEmptyReport, + isEmptyReport, isRootGroupChat, isExpenseReport, isExpenseRequest, diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 6cf4f48ad082..03a378784cf6 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -230,6 +230,7 @@ function shouldDisplayReportInLHN({ includeSelfDM: true, isReportArchived, requiresAttention, + derivedIsEmptyReport: reportAttributes?.[report?.reportID]?.isEmpty, currentUserLogin, currentUserAccountID, conciergeReportID, @@ -302,7 +303,7 @@ function getReportsToDisplayInLHN({ if (shouldDisplay) { const requiresAttention = reportAttributes?.[report?.reportID]?.requiresAttention ?? false; - const isUnreadReport = getIsUnreadReportForInboxTab(report, isReportArchived); + const isUnreadReport = getIsUnreadReportForInboxTab(report, isReportArchived, reportAttributes?.[report?.reportID]?.isEmpty); reportsToDisplay[reportID] = requiresAttention || hasErrorsOtherThanFailedReceipt || isUnreadReport ? {...report, requiresAttention, hasErrorsOtherThanFailedReceipt, isUnreadReport} : report; } @@ -391,7 +392,7 @@ function updateReportsToDisplayInLHN({ if (shouldDisplay) { const requiresAttention = reportAttributes?.[report?.reportID]?.requiresAttention ?? false; - const isUnreadReport = getIsUnreadReportForInboxTab(report, isReportArchived); + const isUnreadReport = getIsUnreadReportForInboxTab(report, isReportArchived, reportAttributes?.[report?.reportID]?.isEmpty); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const hasFlags = requiresAttention || hasErrorsOtherThanFailedReceipt || isUnreadReport; const existingEntry = displayedReports[reportID]; @@ -820,7 +821,7 @@ function getOptionData({ result.statusNum = report.statusNum; // When the only message of a report is deleted lastVisibleActionCreated is not reset leading to wrongly // setting it Unread so we add additional condition here to avoid empty chat LHN from being bold. - result.isUnread = isUnread(report, oneTransactionThreadReport, isReportArchived) && !!report.lastActorAccountID; + result.isUnread = isUnread(report, oneTransactionThreadReport, isReportArchived, reportAttributes?.isEmpty) && !!report.lastActorAccountID; result.isUnreadWithMention = isUnreadWithMention(report); result.isPinned = report.isPinned; result.iouReportID = report.iouReportID; @@ -964,10 +965,14 @@ function getOptionData({ * Whether a report should appear in the "Unread" Inbox tab: it has unread messages and is not muted. * Computed once while building the LHN report set (which is cached/incremental) so the tab filter only reads a flag. */ -function getIsUnreadReportForInboxTab(report: Report, isReportArchived: boolean): boolean { +function getIsUnreadReportForInboxTab(report: Report, isReportArchived: boolean, derivedIsEmptyReport?: boolean): boolean { // The `lastActorAccountID` guard matches getOptionData: it keeps chats whose only visible message was // deleted out of the Unread tab even though isUnread() can still be true (lastVisibleActionCreated isn't reset). - return isUnread(report, undefined, isReportArchived) && !!report.lastActorAccountID && getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; + return ( + isUnread(report, undefined, isReportArchived, derivedIsEmptyReport) && + !!report.lastActorAccountID && + getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE + ); } /** Whether a report belongs in the "To-do" Inbox tab: it has an outstanding GBR (requiresAttention) or RBR (errors). */ diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index ff892c7c2617..e3dd55f5bb31 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -91,7 +91,7 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur const oneTransactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`], isOffline); const oneTransactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; - if (!ReportUtils.isUnread(report, oneTransactionThreadReport, isReportArchived)) { + if (!ReportUtils.isUnread(report, oneTransactionThreadReport, isReportArchived, undefined)) { return false; } diff --git a/src/libs/actions/Report/MarkAllMessageAsRead.tsx b/src/libs/actions/Report/MarkAllMessageAsRead.tsx index d85d58bbe5dd..e91d769276f5 100644 --- a/src/libs/actions/Report/MarkAllMessageAsRead.tsx +++ b/src/libs/actions/Report/MarkAllMessageAsRead.tsx @@ -7,7 +7,7 @@ import {getOneTransactionThreadReportID} from '@libs/ReportActionsUtils'; import {isArchivedReport, isUnread} from '@libs/ReportUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportActions} from '@src/types/onyx'; +import type {Report, ReportActions, ReportAttributesDerivedValue} from '@src/types/onyx'; import type {ReportNameValuePairsArchivedState} from '@selectors/ReportNameValuePairs'; import type {OnyxCollection} from 'react-native-onyx'; @@ -31,7 +31,7 @@ Onyx.connectWithoutView({ // The archived state is passed in by the caller (read via useOnyx with reportNameValuePairsArchivedSelector) rather // than subscribed to here, so this action stays a plain function and callers only re-render when the archived flags // actually change. -function markAllMessagesAsRead(reportNameValuePairs: OnyxCollection) { +function markAllMessagesAsRead(reportNameValuePairs: OnyxCollection, reportAttributesDerived?: ReportAttributesDerivedValue['reports']) { if (isAnonymousUser()) { return; } @@ -55,7 +55,7 @@ function markAllMessagesAsRead(reportNameValuePairs: OnyxCollection(() => { if (!report) { @@ -128,6 +130,7 @@ function DebugReportPage({ currentUserAccountID, conciergeReportID, hasGuidesEmails, + derivedIsEmptyReport, }); return [ @@ -190,6 +193,7 @@ function DebugReportPage({ translate, conciergeReportID, hasGuidesEmails, + derivedIsEmptyReport, ]); const icons = useMemoizedLazyExpensifyIcons(['Eye']); diff --git a/src/pages/inbox/report/ContextMenu/BaseReportActionContextMenu.tsx b/src/pages/inbox/report/ContextMenu/BaseReportActionContextMenu.tsx index a9abc0e3af29..087f0bb7709e 100755 --- a/src/pages/inbox/report/ContextMenu/BaseReportActionContextMenu.tsx +++ b/src/pages/inbox/report/ContextMenu/BaseReportActionContextMenu.tsx @@ -17,7 +17,7 @@ import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import usePaginatedReportActions from '@hooks/usePaginatedReportActions'; -import useReportAttributes, {useDerivedReportNameByReportID} from '@hooks/useReportAttributes'; +import useReportAttributes, {useDerivedIsEmptyReport, useDerivedReportNameByReportID} from '@hooks/useReportAttributes'; import useReportIsArchived from '@hooks/useReportIsArchived'; import useReportOrReportDraft from '@hooks/useReportOrReportDraft'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; @@ -273,7 +273,8 @@ function BaseReportActionContextMenu({ const isArchivedRoom = isArchivedNonExpenseReport(originalReport, isOriginalReportArchived); const isChronosReport = chatIncludesChronosWithID(originalReportID); const isPinnedChat = !!report?.isPinned; - const isUnreadChat = isUnread(report, lhnOneTransactionThreadReport, isOriginalReportArchived); + const derivedIsEmptyReport = useDerivedIsEmptyReport(reportID); + const isUnreadChat = isUnread(report, lhnOneTransactionThreadReport, isOriginalReportArchived, derivedIsEmptyReport); const shouldEnableArrowNavigation = !isMini && (isVisible || shouldKeepOpen); const isHarvestReport = isHarvestCreatedExpenseReport(reportNameValuePairs?.origin, reportNameValuePairs?.originalID); const memberChangeLogReportActionMessage = isMemberChangeAction(reportAction) ? getOriginalMessage(reportAction) : undefined; diff --git a/src/pages/inbox/report/ReportActionsListContext.tsx b/src/pages/inbox/report/ReportActionsListContext.tsx index 0e5e876212ae..4910aa0793e7 100644 --- a/src/pages/inbox/report/ReportActionsListContext.tsx +++ b/src/pages/inbox/report/ReportActionsListContext.tsx @@ -63,9 +63,10 @@ function computeReportActionsSkeletonState(readinessSignals: ReportActionsReadin isConciergeMainDM, hasCachedReportActions, showConciergeSidePanelWelcome, + derivedIsEmptyReport, } = readinessSignals; - const isReportUnread = isUnread(report, transactionThreadReport, isReportArchived); + const isReportUnread = isUnread(report, transactionThreadReport, isReportArchived, derivedIsEmptyReport); // Before the first successful load, a missing loading-state entry means ReportFetchHandler has not // enqueued OpenReport yet. Once a terminal failure writes false, the stored state releases the gate. diff --git a/src/pages/inbox/sidebar/InboxTabSelector.tsx b/src/pages/inbox/sidebar/InboxTabSelector.tsx index b88245f17f9b..7fe375b7bd2f 100644 --- a/src/pages/inbox/sidebar/InboxTabSelector.tsx +++ b/src/pages/inbox/sidebar/InboxTabSelector.tsx @@ -10,6 +10,7 @@ import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; import usePopoverPosition from '@hooks/usePopoverPosition'; +import useReportAttributes from '@hooks/useReportAttributes'; import {useSidebarOrderedReportsActions, useSidebarOrderedReportsState} from '@hooks/useSidebarOrderedReports'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -35,6 +36,7 @@ function InboxTabSelector() { const {activeTab, inboxTabCounts} = useSidebarOrderedReportsState(); const {setActiveTab} = useSidebarOrderedReportsActions(); const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {selector: reportNameValuePairsArchivedSelector}); + const reportAttributesDerived = useReportAttributes(); const icons = useMemoizedLazyExpensifyIcons(['Checkmark']); const {showConfirmModal} = useConfirmModal(); @@ -67,7 +69,7 @@ function InboxTabSelector() { if (action !== ModalActions.CONFIRM) { return; } - markAllMessagesAsRead(reportNameValuePairs); + markAllMessagesAsRead(reportNameValuePairs, reportAttributesDerived); }); }; diff --git a/src/selectors/ReportAttributes.ts b/src/selectors/ReportAttributes.ts index b56086ca52e0..3c6fb8789673 100644 --- a/src/selectors/ReportAttributes.ts +++ b/src/selectors/ReportAttributes.ts @@ -17,5 +17,7 @@ const reportByIDsSelector = (reportIDs: string[]) => (attributes: OnyxEntry, reportID: string | undefined) => (reportID ? attributes?.reports?.[reportID]?.reportName : undefined); -export {reportNameSelector}; +const reportIsEmptySelector = (attributes: OnyxEntry, reportID: string | undefined) => (reportID ? attributes?.reports?.[reportID]?.isEmpty : undefined); + +export {reportNameSelector, reportIsEmptySelector}; export default reportByIDsSelector; diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index a4fd788509bd..d5af762212ce 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -650,7 +650,7 @@ describe('actions/Report', () => { }) .then(() => { // Then the report will be unread - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(true); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(true); // And show a green dot for unread mentions in the LHN expect(ReportUtils.isUnreadWithMention(report)).toBe(true); @@ -671,7 +671,7 @@ describe('actions/Report', () => { }) .then(() => { // The report will be read - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(false); expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime()); // And no longer show the green dot for unread mentions in the LHN @@ -683,7 +683,7 @@ describe('actions/Report', () => { }) .then(() => { // Then the report will be unread and show the green dot for unread mentions in LHN - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(true); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(true); expect(ReportUtils.isUnreadWithMention(report)).toBe(true); expect(report?.lastReadTime).toBe(DateUtils.subtractMillisecondsFromDateTime(reportActionCreatedDate, 1)); @@ -704,7 +704,7 @@ describe('actions/Report', () => { }) .then(() => { // The report will be read, the green dot for unread mentions will go away, and the lastReadTime updated - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(false); expect(ReportUtils.isUnreadWithMention(report)).toBe(false); expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime()); expect(report?.lastMessageText).toBe('Current User Comment 1'); @@ -725,7 +725,7 @@ describe('actions/Report', () => { }) .then(() => { // The report will be read and the lastReadTime updated - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(false); expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime()); expect(report?.lastMessageText).toBe('Current User Comment 2'); @@ -745,7 +745,7 @@ describe('actions/Report', () => { }) .then(() => { // The report will be read and the lastReadTime updated - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(false); expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime()); expect(report?.lastMessageText).toBe('Current User Comment 3'); @@ -827,7 +827,7 @@ describe('actions/Report', () => { .then(() => { // Then no change will occur expect(report?.lastReadTime).toBe(reportActionCreatedDate); - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(false); // When the user manually marks a message as "unread" Report.markCommentAsUnread(REPORT_ID, reportActions, reportActions[400], USER_1_ACCOUNT_ID, false); @@ -835,7 +835,7 @@ describe('actions/Report', () => { }) .then(() => { // Then we should expect the report to be to be unread - expect(ReportUtils.isUnread(report, undefined, undefined)).toBe(true); + expect(ReportUtils.isUnread(report, undefined, undefined, undefined)).toBe(true); expect(report?.lastReadTime).toBe(DateUtils.subtractMillisecondsFromDateTime(reportActions[400].created, 1)); rerender(report); @@ -845,7 +845,7 @@ describe('actions/Report', () => { }) .then(() => getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}` as const)) .then((updatedReport) => { - expect(ReportUtils.isUnread(updatedReport, undefined, undefined)).toBe(false); + expect(ReportUtils.isUnread(updatedReport, undefined, undefined, undefined)).toBe(false); expect(updatedReport?.lastMessageText).toBe('Current User Comment 2'); }); waitForBatchedUpdates(); // flushing onyx.set as it will be batched @@ -3330,7 +3330,7 @@ describe('actions/Report', () => { key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, callback: (reportVal) => { Onyx.disconnect(connection); - resolve(ReportUtils.isUnread(reportVal, undefined, undefined)); + resolve(ReportUtils.isUnread(reportVal, undefined, undefined, undefined)); }, }); }); diff --git a/tests/unit/ReportActionsListContextTest.ts b/tests/unit/ReportActionsListContextTest.ts index 6bfa0ddbbd1f..d23ca6dc7830 100644 --- a/tests/unit/ReportActionsListContextTest.ts +++ b/tests/unit/ReportActionsListContextTest.ts @@ -53,6 +53,7 @@ function createReadinessSignals(overrides: Partial { lastVisibleActionCreated: '2024-03-01 12:00:00.000', }; - expect(isUnread(report, transactionThreadReport, false)).toBe(false); + expect(isUnread(report, transactionThreadReport, false, undefined)).toBe(false); + }); + + it('returns false when derivedIsEmptyReport is true (empty report is never unread)', () => { + const report = { + ...LHNTestUtils.getFakeReport(), + reportID: '1', + lastReadTime: '2024-03-01 12:00:00.000', + lastVisibleActionCreated: '2024-03-01 12:00:01.000', + lastMessageText: 'Hello', + }; + + expect(isUnread(report, undefined, false, true)).toBe(false); + }); + + it('does not short-circuit when derivedIsEmptyReport is false', () => { + const report = { + ...LHNTestUtils.getFakeReport(), + reportID: '1', + lastReadTime: '2024-03-01 12:00:00.000', + lastVisibleActionCreated: '2024-03-01 12:00:01.000', + lastMessageText: 'Hello', + }; + + // derivedIsEmptyReport=false means report is not empty, so isUnread proceeds to check lastReadTime + expect(isUnread(report, undefined, false, false)).toBe(true); + }); + + it('falls through to generateIsEmptyReport when derivedIsEmptyReport is undefined', () => { + const report = { + ...LHNTestUtils.getFakeReport(), + reportID: '1', + lastReadTime: '2024-03-01 12:00:00.000', + lastVisibleActionCreated: '2024-03-01 12:00:01.000', + lastMessageText: 'Hello', + }; + + // report has lastMessageText so generateIsEmptyReport returns false, isUnread proceeds + expect(isUnread(report, undefined, false, undefined)).toBe(true); + expect(isUnread(report, undefined, false, undefined)).toBe(true); + }); + }); + + describe('isEmptyReport', () => { + const report = { + ...LHNTestUtils.getFakeReport(), + reportID: '1', + lastMessageText: 'Hello', + }; + + it('returns the passed value when derivedIsEmptyReport is true', () => { + expect(isEmptyReport(report, false, true)).toBe(true); + }); + + it('returns the passed value when derivedIsEmptyReport is false', () => { + // Verifies ?? (not ||) — false is a valid cache hit, must NOT fall through + expect(isEmptyReport(report, false, false)).toBe(false); + }); + + it('falls through to generateIsEmptyReport when derivedIsEmptyReport is undefined', () => { + // report has lastMessageText so generateIsEmptyReport returns false + expect(isEmptyReport(report, false, undefined)).toBe(false); + expect(isEmptyReport(report, false, undefined)).toBe(false); + }); + + it('returns true when report is undefined', () => { + expect(isEmptyReport(undefined, false, false)).toBe(true); + expect(isEmptyReport(undefined, false, true)).toBe(true); }); }); diff --git a/tests/unit/useReportAttributesTest.ts b/tests/unit/useReportAttributesTest.ts index 07e8f86d6c3d..c9e4fbc6b98e 100644 --- a/tests/unit/useReportAttributesTest.ts +++ b/tests/unit/useReportAttributesTest.ts @@ -1,12 +1,13 @@ import {renderHook} from '@testing-library/react-native'; -import useReportAttributes, {useDerivedReportNameByReportID, useDerivedReportNamesByReportIDs} from '@hooks/useReportAttributes'; +import useReportAttributes, {useDerivedIsEmptyReport, useDerivedReportNameByReportID, useDerivedReportNamesByReportIDs} from '@hooks/useReportAttributes'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {ReportAttributesDerivedValue} from '@src/types/onyx'; import type {ReportAttributes} from '@src/types/onyx/DerivedValues'; +import {reportIsEmptySelector} from '@selectors/ReportAttributes'; import Onyx from 'react-native-onyx'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; @@ -268,3 +269,102 @@ describe('useDerivedReportNamesByReportIDs', () => { expect(result.current?.[REPORT_ID_2]).toBe('Report 2'); }); }); + +describe('useDerivedIsEmptyReport', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(async () => { + await Onyx.clear(); + }); + + it('should return undefined when the derived value is not set', async () => { + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useDerivedIsEmptyReport(REPORT_ID_1)); + + expect(result.current).toBeUndefined(); + }); + + it('should return false for a non-empty report', async () => { + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue(MOCK_REPORTS)); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useDerivedIsEmptyReport(REPORT_ID_1)); + + expect(result.current).toBe(false); + }); + + it('should return true for an empty report', async () => { + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue(MOCK_REPORTS)); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useDerivedIsEmptyReport(REPORT_ID_2)); + + expect(result.current).toBe(true); + }); + + it('should return undefined when reportID is undefined', async () => { + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue(MOCK_REPORTS)); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useDerivedIsEmptyReport(undefined)); + + expect(result.current).toBeUndefined(); + }); + + it('should return undefined when the reportID does not exist', async () => { + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue(MOCK_REPORTS)); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useDerivedIsEmptyReport('nonExistentReportID')); + + expect(result.current).toBeUndefined(); + }); + + it('should update when the isEmpty flag changes', async () => { + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue(MOCK_REPORTS)); + + await waitForBatchedUpdates(); + + const {result, rerender} = renderHook(() => useDerivedIsEmptyReport(REPORT_ID_1)); + + expect(result.current).toBe(false); + + await Onyx.set(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, createDerivedValue({[REPORT_ID_1]: createMockReport({isEmpty: true})})); + + await waitForBatchedUpdates(); + rerender(undefined); + + expect(result.current).toBe(true); + }); +}); + +describe('reportIsEmptySelector', () => { + const derivedValue = createDerivedValue(MOCK_REPORTS); + + it('should return false for a non-empty report', () => { + expect(reportIsEmptySelector(derivedValue, REPORT_ID_1)).toBe(false); + }); + + it('should return true for an empty report', () => { + expect(reportIsEmptySelector(derivedValue, REPORT_ID_2)).toBe(true); + }); + + it('should return undefined when reportID is undefined', () => { + expect(reportIsEmptySelector(derivedValue, undefined)).toBeUndefined(); + }); + + it('should return undefined when reportID does not exist', () => { + expect(reportIsEmptySelector(derivedValue, 'nonExistentReportID')).toBeUndefined(); + }); + + it('should return undefined when attributes is undefined', () => { + expect(reportIsEmptySelector(undefined, REPORT_ID_1)).toBeUndefined(); + }); +}); From 3c74c72bd07d3cc20e1e98e8ca4f97c73c5f859e Mon Sep 17 00:00:00 2001 From: VH Date: Thu, 10 Sep 2026 12:57:06 +0700 Subject: [PATCH 2/5] refactor: pass derivedIsEmptyReport through isValidReport to shouldReportBeInOptionList --- src/libs/DebugUtils.ts | 2 +- src/libs/OptionsListUtils/index.ts | 3 ++ src/libs/ReportUtils.ts | 2 +- src/libs/SidebarUtils.ts | 2 +- src/libs/UnreadIndicatorUpdater/index.ts | 1 + .../actions/Report/MarkAllMessageAsRead.tsx | 4 +- tests/actions/ReportTest.ts | 2 +- tests/perf-test/ReportUtils.perf-test.ts | 1 + tests/unit/DebugUtilsTest.ts | 12 +++++ tests/unit/ReportUtilsTest.ts | 50 +++++++++++++++++++ 10 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/libs/DebugUtils.ts b/src/libs/DebugUtils.ts index 5a11702a8cad..664e0443cc1f 100644 --- a/src/libs/DebugUtils.ts +++ b/src/libs/DebugUtils.ts @@ -1519,7 +1519,7 @@ function getReasonForShowingRowInLHN({ currentUserAccountID?: number; hasGuidesEmails: boolean; conciergeReportID: string | undefined; - derivedIsEmptyReport?: boolean; + derivedIsEmptyReport: boolean | undefined; }): TranslationPaths | null { if (!report) { return null; diff --git a/src/libs/OptionsListUtils/index.ts b/src/libs/OptionsListUtils/index.ts index 36ad27b0671b..d8d58b9203c9 100644 --- a/src/libs/OptionsListUtils/index.ts +++ b/src/libs/OptionsListUtils/index.ts @@ -1731,6 +1731,7 @@ function isValidReport( draftComment: string | undefined, chatReport: OnyxEntry, hasGuidesEmails: boolean, + derivedIsEmptyReport: boolean | undefined, ): boolean { const { betas = [], @@ -1776,6 +1777,7 @@ function isValidReport( currentUserAccountID, conciergeReportID, hasGuidesEmails, + derivedIsEmptyReport, }); if (!shouldBeInOptionList) { @@ -2181,6 +2183,7 @@ function getValidOptions( chatReport, // TODO: Pass guideAccountIDs once callers are fully migrated — PR 33 (https://github.com/Expensify/App/issues/66413); hasExpensifyGuidesEmails falls back to allPersonalDetails isDefaultRoom(report.item) ? hasExpensifyGuidesEmails(Object.keys(report.item?.participants ?? {}).map(Number), undefined) : false, + report.reportID ? reportAttributesDerived?.[report.reportID]?.isEmpty : undefined, ); }; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index f764b39adaeb..d6797ea3d2f5 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -10282,7 +10282,7 @@ type ShouldReportBeInOptionListParams = { /** Pre-computed value from reportAttributes derived value. When provided, skips the expensive requiresAttentionFromCurrentUser recomputation. */ requiresAttention?: boolean; /** Pre-computed isEmpty flag from reportAttributes derived value. When provided, skips the module-level reportAttributesDerivedValue read inside isEmptyReport. */ - derivedIsEmptyReport?: boolean; + derivedIsEmptyReport: boolean | undefined; hasGuidesEmails: boolean; }; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 79057660b4bb..e1a83ae7f819 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -968,7 +968,7 @@ function getOptionData({ * Whether a report should appear in the "Unread" Inbox tab: it has unread messages and is not muted. * Computed once while building the LHN report set (which is cached/incremental) so the tab filter only reads a flag. */ -function getIsUnreadReportForInboxTab(report: Report, isReportArchived: boolean, derivedIsEmptyReport?: boolean): boolean { +function getIsUnreadReportForInboxTab(report: Report, isReportArchived: boolean, derivedIsEmptyReport: boolean | undefined): boolean { // The `lastActorAccountID` guard matches getOptionData: it keeps chats whose only visible message was // deleted out of the Unread tab even though isUnread() can still be true (lastVisibleActionCreated isn't reset). return ( diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index e3dd55f5bb31..4adbc8357205 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -111,6 +111,7 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur // TODO: Pass guideAccountIDs once callers are fully migrated — PR 33 (https://github.com/Expensify/App/issues/66413); hasExpensifyGuidesEmails falls back to allPersonalDetails hasGuidesEmails: ReportUtils.isDefaultRoom(report) ? ReportUtils.hasExpensifyGuidesEmails(Object.keys(report?.participants ?? {}).map(Number), undefined) : false, conciergeReportID, + derivedIsEmptyReport: undefined, }); }); } diff --git a/src/libs/actions/Report/MarkAllMessageAsRead.tsx b/src/libs/actions/Report/MarkAllMessageAsRead.tsx index 48a654451ab6..ad1c2a9f2c78 100644 --- a/src/libs/actions/Report/MarkAllMessageAsRead.tsx +++ b/src/libs/actions/Report/MarkAllMessageAsRead.tsx @@ -37,8 +37,8 @@ Onyx.connectWithoutView({ */ function markAllMessagesAsRead( reportNameValuePairs: OnyxCollection, - reportIDs?: string[], - reportAttributesDerived?: ReportAttributesDerivedValue['reports'], + reportIDs: string[] | undefined, + reportAttributesDerived: ReportAttributesDerivedValue['reports'] | undefined, ) { if (isAnonymousUser()) { return; diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index e03904dee9d9..ab953943ce58 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -3446,7 +3446,7 @@ describe('actions/Report', () => { await Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportCollections); // When mark all reports as read - markAllMessagesAsRead(undefined); + markAllMessagesAsRead(undefined, undefined, undefined); await waitForBatchedUpdates(); diff --git a/tests/perf-test/ReportUtils.perf-test.ts b/tests/perf-test/ReportUtils.perf-test.ts index 4b273103bd88..f93a85b94bd7 100644 --- a/tests/perf-test/ReportUtils.perf-test.ts +++ b/tests/perf-test/ReportUtils.perf-test.ts @@ -199,6 +199,7 @@ describe('ReportUtils', () => { isReportArchived: false, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ); }); diff --git a/tests/unit/DebugUtilsTest.ts b/tests/unit/DebugUtilsTest.ts index 2ff13173c7fe..c853d822e696 100644 --- a/tests/unit/DebugUtilsTest.ts +++ b/tests/unit/DebugUtilsTest.ts @@ -796,6 +796,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBeNull(); }); @@ -809,6 +810,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasDraftComment'); }); @@ -825,6 +827,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasGBR'); }); @@ -840,6 +843,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.pinnedByUser'); }); @@ -859,6 +863,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasAddWorkspaceRoomErrors'); }); @@ -886,6 +891,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.isUnread'); }); @@ -906,6 +912,7 @@ describe('DebugUtils', () => { draftComment: '', hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.isArchived'); }); @@ -921,6 +928,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.isSelfDM'); }); @@ -933,6 +941,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.isFocused'); }); @@ -994,6 +1003,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR'); }); @@ -1055,6 +1065,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR'); }); @@ -1068,6 +1079,7 @@ describe('DebugUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR'); }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 808d68c79de3..857ba78c30ff 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -7885,6 +7885,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -7911,6 +7912,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -7942,6 +7944,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8007,6 +8010,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8032,6 +8036,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8057,6 +8062,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8082,6 +8088,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8120,6 +8127,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8153,6 +8161,7 @@ describe('ReportUtils', () => { draftComment: '', hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8186,6 +8195,7 @@ describe('ReportUtils', () => { draftComment: '', hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8213,6 +8223,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8242,6 +8253,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8264,6 +8276,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8289,6 +8302,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8343,6 +8357,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8365,6 +8380,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8395,6 +8411,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID, + derivedIsEmptyReport: undefined, }), ).toBe(CONST.REPORT_IN_LHN_REASONS.DEFAULT); }); @@ -8423,6 +8440,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, conciergeReportID, hasGuidesEmails: false, + derivedIsEmptyReport: undefined, }), ).toBe(CONST.REPORT_IN_LHN_REASONS.DEFAULT); }); @@ -8450,6 +8468,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, conciergeReportID: 'some-other-report-id', hasGuidesEmails: false, + derivedIsEmptyReport: undefined, }), ).toBeNull(); }); @@ -8477,6 +8496,7 @@ describe('ReportUtils', () => { draftComment: '', isReportArchived: undefined, hasGuidesEmails: false, + derivedIsEmptyReport: undefined, }; // When the param identifies this report as Concierge, the empty chat is kept in the option list... @@ -8505,6 +8525,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8553,6 +8574,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8575,6 +8597,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8611,6 +8634,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8644,6 +8668,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBe(CONST.REPORT_IN_LHN_REASONS.IS_UNREAD); }); @@ -8675,6 +8700,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8699,6 +8725,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_ADD_WORKSPACE_ROOM_ERRORS); }); @@ -8745,6 +8772,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8798,6 +8826,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBe(CONST.REPORT_IN_LHN_REASONS.PINNED_BY_USER); }); @@ -8822,6 +8851,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeNull(); }); @@ -8845,6 +8875,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeTruthy(); }); @@ -8868,6 +8899,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8891,6 +8923,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8914,6 +8947,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -8933,6 +8967,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }), ).toBeFalsy(); }); @@ -9001,6 +9036,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).not.toBe(CONST.REPORT_IN_LHN_REASONS.HAS_IOU_VIOLATIONS); @@ -16404,6 +16440,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -16465,6 +16502,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.DEFAULT); @@ -16659,6 +16697,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonForOptionList).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -16705,6 +16744,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonForOptionList).toBe(null); @@ -17828,6 +17868,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, currentUserAccountID: adminAccountID, }); @@ -18012,6 +18053,7 @@ describe('ReportUtils', () => { isReportArchived: undefined, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.DEFAULT); @@ -18208,6 +18250,7 @@ describe('ReportUtils', () => { isReportArchived: archiveState.current, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonBeforeDelete).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -18232,6 +18275,7 @@ describe('ReportUtils', () => { isReportArchived: archiveState.current, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonAfterDelete).toBe(CONST.REPORT_IN_LHN_REASONS.IS_ARCHIVED); @@ -18334,6 +18378,7 @@ describe('ReportUtils', () => { isReportArchived: isReportArchivedBefore.current, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonBeforeRemoval).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -18376,6 +18421,7 @@ describe('ReportUtils', () => { isReportArchived: isReportArchivedAfter.current, hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reasonAfterRemoval).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -19556,6 +19602,7 @@ describe('ReportUtils', () => { draftComment: '', hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -19611,6 +19658,7 @@ describe('ReportUtils', () => { draftComment: '', hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.DEFAULT); @@ -21828,6 +21876,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); @@ -21900,6 +21949,7 @@ describe('ReportUtils', () => { hasGuidesEmails: false, conciergeReportID: undefined, + derivedIsEmptyReport: undefined, }); expect(reason).toBe(CONST.REPORT_IN_LHN_REASONS.HAS_GBR); From 1981a189a84da5e58f50d6160a8bdba0729ed683 Mon Sep 17 00:00:00 2001 From: VH Date: Fri, 11 Sep 2026 13:51:43 +0700 Subject: [PATCH 3/5] refactor: pass derivedIsEmptyReport through UnreadIndicatorUpdater - Connect to DERIVED.REPORT_ATTRIBUTES Onyx key - Pass derivedIsEmptyReport to isUnread and shouldReportBeInOptionList --- src/libs/UnreadIndicatorUpdater/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index 4adbc8357205..ba90c7d8b120 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -8,7 +8,7 @@ import Navigation, {navigationRef} from '@navigation/Navigation'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportActions, ReportNameValuePairs, Session} from '@src/types/onyx'; +import type {Report, ReportActions, ReportAttributesDerivedValue, ReportNameValuePairs, Session} from '@src/types/onyx'; import type {OnyxCollection} from 'react-native-onyx'; @@ -65,6 +65,14 @@ Onyx.connectWithoutView({ }, }); +let reportAttributesDerived: ReportAttributesDerivedValue['reports'] | undefined; +Onyx.connectWithoutView({ + key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, + callback: (value) => { + reportAttributesDerived = value?.reports; + }, +}); + function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, currentReportID: string | undefined, draftComment: string | undefined) { // Read the in-memory offline state directly since this is an imperative one-shot computation (reactivity is not needed here). const isOffline = getIsOffline(); @@ -91,7 +99,9 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur const oneTransactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.reportID}`], isOffline); const oneTransactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`]; - if (!ReportUtils.isUnread(report, oneTransactionThreadReport, isReportArchived, undefined)) { + const derivedIsEmptyReport = report?.reportID ? reportAttributesDerived?.[report.reportID]?.isEmpty : undefined; + + if (!ReportUtils.isUnread(report, oneTransactionThreadReport, isReportArchived, derivedIsEmptyReport)) { return false; } @@ -111,7 +121,7 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur // TODO: Pass guideAccountIDs once callers are fully migrated — PR 33 (https://github.com/Expensify/App/issues/66413); hasExpensifyGuidesEmails falls back to allPersonalDetails hasGuidesEmails: ReportUtils.isDefaultRoom(report) ? ReportUtils.hasExpensifyGuidesEmails(Object.keys(report?.participants ?? {}).map(Number), undefined) : false, conciergeReportID, - derivedIsEmptyReport: undefined, + derivedIsEmptyReport, }); }); } From a24baed05311b27bdccc1b05271b47aec2416b8a Mon Sep 17 00:00:00 2001 From: VH Date: Fri, 11 Sep 2026 22:04:23 +0700 Subject: [PATCH 4/5] refactor: add justification comment for connectWithoutView in UnreadIndicatorUpdater --- src/libs/UnreadIndicatorUpdater/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index ba90c7d8b120..d90e8d692759 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -66,6 +66,7 @@ Onyx.connectWithoutView({ }); let reportAttributesDerived: ReportAttributesDerivedValue['reports'] | undefined; +// This subscription is used to update the unread indicators count which is not linked to UI and it does not update any UI state. Onyx.connectWithoutView({ key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, callback: (value) => { From 9cd9b1186acaddb1aba2971d33005424b7c88460 Mon Sep 17 00:00:00 2001 From: VH Date: Fri, 18 Sep 2026 12:11:53 +0700 Subject: [PATCH 5/5] fix: remove unused reportAttributesDerivedValue variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Variable was connected to Onyx but never read — isEmptyReport receives derivedIsEmptyReport as a parameter instead. --- src/libs/ReportUtils.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index be2b69415c0d..780f8cb17e15 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1219,14 +1219,6 @@ Onyx.connect({ }, }); -let reportAttributesDerivedValue: ReportAttributesDerivedValue['reports']; -Onyx.connect({ - key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, - callback: (value) => { - reportAttributesDerivedValue = value?.reports ?? {}; - }, -}); - let cachedSelfDMReportID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.SELF_DM_REPORT_ID,