diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e4b82aa36c7a..317ace1d7d4e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4317,6 +4317,29 @@ function isGroupChat(report: OnyxEntry): boolean { ); } +/** + * Assume any report without a reportID is unusable. + */ +function isValidReport(report?: OnyxEntry): boolean { + return Boolean(!report?.reportID); +} + +/** + * Check to see if we are a participant of this report. + */ +function isReportParticipant(report?: OnyxEntry, accountID: number): boolean { + if (!accountID) { + return false; + } + + // We are not the owner or the manager or a participant + if (report?.ownerAccountID !== accountID && report?.managerID !== accountID && !(report?.participantAccountIDs ?? []).includes(accountID)) { + return false; + } + + return true; +} + function shouldUseFullTitleToDisplay(report: OnyxEntry): boolean { return isMoneyRequestReport(report) || isPolicyExpenseChat(report) || isChatRoom(report) || isChatThread(report) || isTaskReport(report); } @@ -4613,6 +4636,8 @@ export { shouldDisplayThreadReplies, shouldDisableThread, getChildReportNotificationPreference, + isReportParticipant, + isValidReport, }; export type {ExpenseOriginalMessage, OptionData, OptimisticChatReport, OptimisticCreatedReportAction}; diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 1d38d09e08a1..1e86c8dd8f1a 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -6,6 +6,7 @@ import Log from '@libs/Log'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; +import * as ReportUtils from '@libs/ReportUtils'; /** * This actions file is used to automatically switch a user into #focus mode when they exceed a certain number of reports. We do this primarily for performance reasons. @@ -120,7 +121,17 @@ function tryFocusModeUpdate() { return; } - const reportCount = Object.keys(allReports ?? {}).length; + const validReports = []; + Object.keys(allReports ?? {}).forEach((key) => { + const report = allReports?.[key]; + if (ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { + return; + } + + validReports.push(report); + }); + + const reportCount = validReports.length; if (reportCount < CONST.REPORT.MAX_COUNT_BEFORE_FOCUS_UPDATE) { Log.info('Not switching user to optimized focus mode as they do not have enough reports', false, {reportCount}); return;