diff --git a/src/components/WideRHPContextProvider/index.tsx b/src/components/WideRHPContextProvider/index.tsx index 98bb7b990459..06c628609051 100644 --- a/src/components/WideRHPContextProvider/index.tsx +++ b/src/components/WideRHPContextProvider/index.tsx @@ -1,4 +1,3 @@ -import useOnyx from '@hooks/useOnyx'; import useRootNavigationState from '@hooks/useRootNavigationState'; import calculateReceiptPaneRHPWidth from '@libs/Navigation/helpers/calculateReceiptPaneRHPWidth'; @@ -6,12 +5,7 @@ import calculateSuperWideRHPWidth from '@libs/Navigation/helpers/calculateSuperW import calculateWideRHPWidth from '@libs/Navigation/helpers/calculateWideRHPWidth'; import type {NavigationRoute} from '@libs/Navigation/types'; -import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; -import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report} from '@src/types/onyx'; - -import type {OnyxCollection} from 'react-native-onyx'; import {findFocusedRoute} from '@react-navigation/native'; import React, {createContext, useCallback, useContext, useEffect, useRef, useState} from 'react'; @@ -49,18 +43,6 @@ const animatedWideRHPWidth = new Animated.Value(wideRHPWidth); const WideRHPStateContext = createContext(defaultWideRHPStateContextValue); const WideRHPActionsContext = createContext(defaultWideRHPActionsContextValue); -const expenseReportSelector = (reports: OnyxCollection) => { - return Object.fromEntries( - Object.entries(reports ?? {}).map(([key, report]) => [ - key, - { - reportID: report?.reportID, - type: report?.type, - }, - ]), - ); -}; - // Function to add a Wide/Super Wide RHP route key to the array including wide/super wide RHP route keys function showWideRHPRoute(route: NavigationRoute, setAllRHPRouteKeys: React.Dispatch>) { if (!route.key) { @@ -108,11 +90,8 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) { const [superWideRHPRouteKeys, setSuperWideRHPRouteKeys] = useState([]); // A reportID maps to at most one hint, making "wide vs super-wide" structurally mutually exclusive. - const [reportRHPWidthHints, setReportRHPWidthHints] = useState>(() => new Map()); - - const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { - selector: expenseReportSelector, - }); + // A ref, not state: hints are only read in effects, and state would hand every useWideRHPActions consumer new actions on each row press. + const reportRHPWidthHintsRef = useRef(new Map()); const isWideRHPClosingRef = useRef(false); const isSuperWideRHPClosingRef = useRef(false); @@ -217,26 +196,13 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) { /** * Sets an optimistic width hint for a reportID before its screen renders, so the right width is - * registered on first paint. Invoices and tasks are excluded from the 'wide' hint. + * registered on first paint. useRHPWidth ignores a 'wide' hint for invoices and tasks. */ const markReportRHPWidth = (reportID: string | undefined, width: RHPWidthHint) => { if (!reportID) { return; } - if (width === 'wide') { - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - if (report?.type === CONST.REPORT.TYPE.INVOICE || report?.type === CONST.REPORT.TYPE.TASK) { - return; - } - } - setReportRHPWidthHints((prev) => { - if (prev.get(reportID) === width) { - return prev; - } - const next = new Map(prev); - next.set(reportID, width); - return next; - }); + reportRHPWidthHintsRef.current.set(reportID, width); }; /** @@ -244,21 +210,13 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) { * current hint; omit to clear unconditionally. Called when a report no longer qualifies for a hint. */ const unmarkReportRHPWidth = (reportID: string, width?: RHPWidthHint) => { - setReportRHPWidthHints((prev) => { - const current = prev.get(reportID); - if (current === undefined) { - return prev; - } - if (width !== undefined && current !== width) { - return prev; - } - const next = new Map(prev); - next.delete(reportID); - return next; - }); + if (width !== undefined && reportRHPWidthHintsRef.current.get(reportID) !== width) { + return; + } + reportRHPWidthHintsRef.current.delete(reportID); }; - const getReportRHPWidthHint = (reportID: string): RHPWidthHint | undefined => reportRHPWidthHints.get(reportID); + const getReportRHPWidthHint = (reportID: string): RHPWidthHint | undefined => reportRHPWidthHintsRef.current.get(reportID); /** * Effect that handles responsive RHP width calculation when window dimensions change. diff --git a/src/components/WideRHPContextProvider/useRHPWidth/index.ts b/src/components/WideRHPContextProvider/useRHPWidth/index.ts index 9eb7cce50fc2..7761dc04e3a1 100644 --- a/src/components/WideRHPContextProvider/useRHPWidth/index.ts +++ b/src/components/WideRHPContextProvider/useRHPWidth/index.ts @@ -1,6 +1,12 @@ +import useOnyx from '@hooks/useOnyx'; + +import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; import {navigationRef} from '@libs/Navigation/Navigation'; +import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; +import ONYXKEYS from '@src/ONYXKEYS'; +import {getReportType} from '@src/selectors/Report'; import {useRoute} from '@react-navigation/native'; import {useEffect} from 'react'; @@ -24,6 +30,8 @@ function useRHPWidth(width: RHPWidth) { const route = useRoute(); const reportID = route.params && 'reportID' in route.params && typeof route.params.reportID === 'string' ? route.params.reportID : ''; const {setRHPWidth, removeRHPRouteKey, getReportRHPWidthHint, unmarkReportRHPWidth} = useWideRHPActions(); + const [reportType] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(reportID)}`, {selector: getReportType}); + const isWideHintExcluded = reportType === CONST.REPORT.TYPE.INVOICE || reportType === CONST.REPORT.TYPE.TASK; const onClose = () => { removeRHPRouteKey(route); @@ -41,10 +49,11 @@ function useRHPWidth(width: RHPWidth) { // Register the width; a higher hint outranks the caller so the screen opens pre-marked before its data loads. useEffect(() => { - const hint = reportID ? getReportRHPWidthHint(reportID) : undefined; + const storedHint = reportID ? getReportRHPWidthHint(reportID) : undefined; + const hint = storedHint === 'wide' && isWideHintExcluded ? undefined : storedHint; const effectiveWidth: RHPWidth = hint && getWidthOrder(hint) > getWidthOrder(width) ? hint : width; setRHPWidth(route, effectiveWidth); - }, [width, reportID, route, setRHPWidth, getReportRHPWidthHint]); + }, [width, reportID, route, setRHPWidth, getReportRHPWidthHint, isWideHintExcluded]); // Clear the hint once the caller's width reaches it; onClose handles the never-reached case. useEffect(() => { diff --git a/src/selectors/Report.ts b/src/selectors/Report.ts index 2d07aa327584..43248caf8b55 100644 --- a/src/selectors/Report.ts +++ b/src/selectors/Report.ts @@ -36,6 +36,10 @@ function getReportChatType(report: OnyxEntry) { return report?.chatType; } +function getReportType(report: OnyxEntry) { + return report?.type; +} + function getReportPolicyID(report: OnyxEntry) { return report?.policyID; } @@ -429,6 +433,7 @@ const reportsByIDsSelector = export { getArchiveReason, getReportChatType, + getReportType, groupChatAvatarReportSelector, getReportOwnerAccountID, getReportParentReportID,