Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 9 additions & 51 deletions src/components/WideRHPContextProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import useOnyx from '@hooks/useOnyx';
import useRootNavigationState from '@hooks/useRootNavigationState';

import calculateReceiptPaneRHPWidth from '@libs/Navigation/helpers/calculateReceiptPaneRHPWidth';
import calculateSuperWideRHPWidth from '@libs/Navigation/helpers/calculateSuperWideRHPWidth';
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';
Expand Down Expand Up @@ -49,18 +43,6 @@ const animatedWideRHPWidth = new Animated.Value(wideRHPWidth);
const WideRHPStateContext = createContext<WideRHPStateContextType>(defaultWideRHPStateContextValue);
const WideRHPActionsContext = createContext<WideRHPActionsContextType>(defaultWideRHPActionsContextValue);

const expenseReportSelector = (reports: OnyxCollection<Report>) => {
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<React.SetStateAction<string[]>>) {
if (!route.key) {
Expand Down Expand Up @@ -108,11 +90,8 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) {
const [superWideRHPRouteKeys, setSuperWideRHPRouteKeys] = useState<string[]>([]);

// A reportID maps to at most one hint, making "wide vs super-wide" structurally mutually exclusive.
const [reportRHPWidthHints, setReportRHPWidthHints] = useState<Map<string, RHPWidthHint>>(() => 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<string, RHPWidthHint>());

const isWideRHPClosingRef = useRef(false);
const isSuperWideRHPClosingRef = useRef(false);
Expand Down Expand Up @@ -217,48 +196,27 @@ 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);
};

/**
* Clears the optimistic width hint for a reportID. Pass `width` to clear only when it matches the
* 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.
Expand Down
13 changes: 11 additions & 2 deletions src/components/WideRHPContextProvider/useRHPWidth/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -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(() => {
Expand Down
5 changes: 5 additions & 0 deletions src/selectors/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function getReportChatType(report: OnyxEntry<Report>) {
return report?.chatType;
}

function getReportType(report: OnyxEntry<Report>) {
return report?.type;
}

function getReportPolicyID(report: OnyxEntry<Report>) {
return report?.policyID;
}
Expand Down Expand Up @@ -429,6 +433,7 @@ const reportsByIDsSelector =
export {
getArchiveReason,
getReportChatType,
getReportType,
groupChatAvatarReportSelector,
getReportOwnerAccountID,
getReportParentReportID,
Expand Down
Loading