Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/components/PriorityModeController.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {useNavigation} from '@react-navigation/native';
import {accountIDSelector} from '@selectors/Session';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import useOnyx from '@hooks/useOnyx';
import {useSidebarOrderedReportsState} from '@hooks/useSidebarOrderedReports';
import {updateChatPriorityMode} from '@libs/actions/User';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import navigationRef from '@libs/Navigation/navigationRef';
import {isReportParticipant, isValidReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
Expand All @@ -29,25 +28,14 @@ const isInFocusModeSelector = (priorityMode: OnyxEntry<ValueOf<typeof CONST.PRIO
*
*/
export default function PriorityModeController() {
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector});
const {orderedReportIDs} = useSidebarOrderedReportsState();
const lhnReportCount = orderedReportIDs.length;
const [isLoadingReportData] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA);
const [isInFocusMode, isInFocusModeMetadata] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: isInFocusModeSelector});
const [hasTriedFocusMode, hasTriedFocusModeMetadata] = useOnyx(ONYXKEYS.NVP_TRY_FOCUS_MODE);
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const currentRouteName = useCurrentRouteName();
const [shouldShowModal, setShouldShowModal] = useState(false);
const closeModal = useCallback(() => setShouldShowModal(false), []);
const validReportCount = useMemo(() => {
let count = 0;
for (const report of Object.values(allReports ?? {})) {
if (!isValidReport(report) || !isReportParticipant(accountID ?? CONST.DEFAULT_NUMBER_ID, report)) {
continue;
}

count++;
}
return count;
}, [accountID, allReports]);

// We set this when we have finally auto-switched the user of #focus mode to prevent duplication.
const hasSwitched = useRef(false);
Expand All @@ -57,7 +45,6 @@ export default function PriorityModeController() {
// Wait for Onyx state to fully load
if (
isLoadingReportData !== false ||
!accountID ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot Why is this no longer needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !accountID guard was previously needed because this component used accountID to compute validReportCount — specifically in isReportParticipant(accountID ?? CONST.DEFAULT_NUMBER_ID, report). Without a valid accountID, the count would be wrong.

Now that we use orderedReportIDs.length from useSidebarOrderedReportsState(), accountID is no longer used anywhere in this component. The sidebar context already handles user-specific filtering internally (it only populates with reports for the logged-in user), so there's no need to independently guard on accountID here.

isLoadingOnyxValue(isInFocusModeMetadata, hasTriedFocusModeMetadata) ||
typeof isInFocusMode !== 'boolean' ||
typeof hasTriedFocusMode !== 'boolean'
Expand All @@ -70,25 +57,25 @@ export default function PriorityModeController() {
return;
}

if (validReportCount < CONST.REPORT.MAX_COUNT_BEFORE_FOCUS_UPDATE) {
Log.info('[PriorityModeController] Not switching user to focus mode as they do not have enough reports', false, {validReportCount});
if (lhnReportCount < CONST.REPORT.MAX_COUNT_BEFORE_FOCUS_UPDATE) {
Log.info('[PriorityModeController] Not switching user to focus mode as they do not have enough reports', false, {lhnReportCount});
return;
}

// We wait for the user to navigate back to the home screen before triggering this switch
const isNarrowLayout = getIsNarrowLayout();
if ((isNarrowLayout && currentRouteName !== SCREENS.INBOX) || (!isNarrowLayout && currentRouteName !== SCREENS.REPORT)) {
Log.info("[PriorityModeController] Not switching user to focus mode as they aren't on the home screen", false, {validReportCount, currentRouteName});
Log.info("[PriorityModeController] Not switching user to focus mode as they aren't on the home screen", false, {lhnReportCount, currentRouteName});
return;
}

Log.info('[PriorityModeController] Switching user to focus mode', false, {validReportCount, hasTriedFocusMode, isInFocusMode, currentRouteName});
Log.info('[PriorityModeController] Switching user to focus mode', false, {lhnReportCount, hasTriedFocusMode, isInFocusMode, currentRouteName});
updateChatPriorityMode(CONST.PRIORITY_MODE.GSD, true);
requestAnimationFrame(() => {
setShouldShowModal(true);
});
hasSwitched.current = true;
}, [accountID, currentRouteName, hasTriedFocusMode, hasTriedFocusModeMetadata, isInFocusMode, isInFocusModeMetadata, isLoadingReportData, validReportCount]);
}, [currentRouteName, hasTriedFocusMode, hasTriedFocusModeMetadata, isInFocusMode, isInFocusModeMetadata, isLoadingReportData, lhnReportCount]);

useEffect(() => {
if (!shouldShowModal) {
Expand Down
Loading