diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 2f18f7c3733c..9c91f75273c1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4486,6 +4486,36 @@ 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(accountID: number, report: OnyxEntry): boolean { + if (!accountID) { + return false; + } + + // If we have a DM AND the accountID we are checking is the current user THEN we won't find them as a participant and must assume they are a participant + if (isDM(report) && accountID === currentUserAccountID) { + return true; + } + + const possibleAccountIDs = report?.participantAccountIDs ?? []; + if (report?.ownerAccountID) { + possibleAccountIDs.push(report?.ownerAccountID); + } + if (report?.managerID) { + possibleAccountIDs.push(report?.managerID); + } + return possibleAccountIDs.includes(accountID); +} + function shouldUseFullTitleToDisplay(report: OnyxEntry): boolean { return isMoneyRequestReport(report) || isPolicyExpenseChat(report) || isChatRoom(report) || isChatThread(report) || isTaskReport(report); } @@ -4809,6 +4839,8 @@ export { shouldDisableThread, doesReportBelongToWorkspace, getChildReportNotificationPreference, + isReportParticipant, + isValidReport, isReportFieldOfTypeTitle, }; diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 1d38d09e08a1..7ae174ac8606 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -3,6 +3,7 @@ import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as CollectionUtils from '@libs/CollectionUtils'; import Log from '@libs/Log'; +import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; @@ -120,7 +121,21 @@ function tryFocusModeUpdate() { return; } - const reportCount = Object.keys(allReports ?? {}).length; + const validReports = []; + Object.keys(allReports ?? {}).forEach((key) => { + const report = allReports?.[key]; + if (!report) { + return; + } + + 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;