Skip to content

Commit 24c0237

Browse files
authored
Merge pull request #54702 from etCoderDysto/remove-withCurrentReportID-usage
fix: Remove withCurrentReportID HOC
2 parents 6871aa2 + 3a3b5d5 commit 24c0237

File tree

7 files changed

+69
-101
lines changed

7 files changed

+69
-101
lines changed

src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import {FullScreenContextProvider} from './components/VideoPlayerContexts/FullSc
2929
import {PlaybackContextProvider} from './components/VideoPlayerContexts/PlaybackContext';
3030
import {VideoPopoverMenuContextProvider} from './components/VideoPlayerContexts/VideoPopoverMenuContext';
3131
import {VolumeContextProvider} from './components/VideoPlayerContexts/VolumeContext';
32-
import {CurrentReportIDContextProvider} from './components/withCurrentReportID';
3332
import {EnvironmentProvider} from './components/withEnvironment';
3433
import {KeyboardStateProvider} from './components/withKeyboardState';
3534
import CONFIG from './CONFIG';
3635
import Expensify from './Expensify';
36+
import {CurrentReportIDContextProvider} from './hooks/useCurrentReportID';
3737
import useDefaultDragAndDrop from './hooks/useDefaultDragAndDrop';
3838
import {ReportIDsContextProvider} from './hooks/useReportIDs';
3939
import OnyxUpdateManager from './libs/actions/OnyxUpdateManager';

src/components/withCurrentReportID.tsx

-89
This file was deleted.

src/hooks/useCurrentReportID.tsx

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
1-
import {useContext} from 'react';
2-
import type {CurrentReportIDContextValue} from '@components/withCurrentReportID';
3-
import {CurrentReportIDContext} from '@components/withCurrentReportID';
1+
import type {NavigationState} from '@react-navigation/native';
2+
import React, {createContext, useCallback, useContext, useMemo, useState} from 'react';
3+
import Navigation from '@libs/Navigation/Navigation';
4+
5+
type CurrentReportIDContextValue = {
6+
updateCurrentReportID: (state: NavigationState) => void;
7+
currentReportID: string | undefined;
8+
};
9+
10+
type CurrentReportIDContextProviderProps = {
11+
/** Actual content wrapped by this component */
12+
children: React.ReactNode;
13+
};
14+
15+
const CurrentReportIDContext = createContext<CurrentReportIDContextValue | null>(null);
16+
17+
function CurrentReportIDContextProvider(props: CurrentReportIDContextProviderProps) {
18+
const [currentReportID, setCurrentReportID] = useState<string | undefined>('');
19+
20+
/**
21+
* This function is used to update the currentReportID
22+
* @param state root navigation state
23+
*/
24+
const updateCurrentReportID = useCallback(
25+
(state: NavigationState) => {
26+
const reportID = Navigation.getTopmostReportId(state);
27+
28+
/*
29+
* Make sure we don't make the reportID undefined when switching between the chat list and settings tab.
30+
* This helps prevent unnecessary re-renders.
31+
*/
32+
const params = state?.routes?.[state.index]?.params;
33+
if (params && 'screen' in params && typeof params.screen === 'string' && params.screen.indexOf('Settings_') !== -1) {
34+
return;
35+
}
36+
setCurrentReportID(reportID);
37+
},
38+
[setCurrentReportID],
39+
);
40+
41+
/**
42+
* The context this component exposes to child components
43+
* @returns currentReportID to share between central pane and LHN
44+
*/
45+
const contextValue = useMemo(
46+
(): CurrentReportIDContextValue => ({
47+
updateCurrentReportID,
48+
currentReportID,
49+
}),
50+
[updateCurrentReportID, currentReportID],
51+
);
52+
53+
return <CurrentReportIDContext.Provider value={contextValue}>{props.children}</CurrentReportIDContext.Provider>;
54+
}
55+
56+
CurrentReportIDContextProvider.displayName = 'CurrentReportIDContextProvider';
457

558
export default function useCurrentReportID(): CurrentReportIDContextValue | null {
659
return useContext(CurrentReportIDContext);
760
}
61+
62+
export {CurrentReportIDContextProvider};
63+
export type {CurrentReportIDContextValue};

src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function BottomTabBar({selectedTab}: BottomTabBarProps) {
7070
const styles = useThemeStyles();
7171
const {translate} = useLocalize();
7272
const {activeWorkspaceID} = useActiveWorkspace();
73-
const {currentReportID} = useCurrentReportID() ?? {currentReportID: null};
73+
const {currentReportID = null} = useCurrentReportID() ?? {};
7474
const [user] = useOnyx(ONYXKEYS.USER);
7575
const [betas] = useOnyx(ONYXKEYS.BETAS);
7676
const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE);

src/pages/home/ReportScreen.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
1616
import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView';
1717
import ScreenWrapper from '@components/ScreenWrapper';
1818
import TaskHeaderActionButton from '@components/TaskHeaderActionButton';
19-
import type {CurrentReportIDContextValue} from '@components/withCurrentReportID';
20-
import withCurrentReportID from '@components/withCurrentReportID';
2119
import useActiveWorkspace from '@hooks/useActiveWorkspace';
2220
import useAppFocusEvent from '@hooks/useAppFocusEvent';
21+
import type {CurrentReportIDContextValue} from '@hooks/useCurrentReportID';
22+
import useCurrentReportID from '@hooks/useCurrentReportID';
2323
import useDeepCompareRef from '@hooks/useDeepCompareRef';
2424
import useLocalize from '@hooks/useLocalize';
2525
import useNetwork from '@hooks/useNetwork';
@@ -97,7 +97,7 @@ function getParentReportAction(parentReportActions: OnyxEntry<OnyxTypes.ReportAc
9797
return parentReportActions[parentReportActionID ?? '0'];
9898
}
9999

100-
function ReportScreen({route, currentReportID = '', navigation}: ReportScreenProps) {
100+
function ReportScreen({route, navigation}: ReportScreenProps) {
101101
const styles = useThemeStyles();
102102
const {translate} = useLocalize();
103103
const reportIDFromRoute = getReportID(route);
@@ -112,6 +112,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
112112
const {isOffline} = useNetwork();
113113
const {shouldUseNarrowLayout, isInNarrowPaneModal} = useResponsiveLayout();
114114
const {activeWorkspaceID} = useActiveWorkspace();
115+
const currentReportIDValue = useCurrentReportID();
115116

116117
const [modal] = useOnyx(ONYXKEYS.MODAL);
117118
const [isComposerFullSize] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_IS_COMPOSER_FULL_SIZE}${reportIDFromRoute}`, {initialValue: false});
@@ -277,7 +278,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
277278
const lastReportAction = [...combinedReportActions, parentReportAction].find((action) => ReportUtils.canEditReportAction(action) && !ReportActionsUtils.isMoneyRequestAction(action));
278279
const isSingleTransactionView = ReportUtils.isMoneyRequest(report) || ReportUtils.isTrackExpenseReport(report);
279280
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '-1'}`];
280-
const isTopMostReportId = currentReportID === reportIDFromRoute;
281+
const isTopMostReportId = currentReportIDValue?.currentReportID === reportIDFromRoute;
281282
const didSubscribeToReportLeavingEvents = useRef(false);
282283
const [showSoftInputOnFocus, setShowSoftInputOnFocus] = useState(false);
283284

@@ -870,4 +871,4 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
870871
}
871872

872873
ReportScreen.displayName = 'ReportScreen';
873-
export default withCurrentReportID(memo(ReportScreen, (prevProps, nextProps) => prevProps.currentReportID === nextProps.currentReportID && lodashIsEqual(prevProps.route, nextProps.route)));
874+
export default memo(ReportScreen, (prevProps, nextProps) => lodashIsEqual(prevProps.route, nextProps.route));

tests/ui/WorkspaceCategoriesTest.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Onyx from 'react-native-onyx';
66
import ComposeProviders from '@components/ComposeProviders';
77
import {LocaleContextProvider} from '@components/LocaleContextProvider';
88
import OnyxProvider from '@components/OnyxProvider';
9-
import {CurrentReportIDContextProvider} from '@components/withCurrentReportID';
9+
import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID';
1010
import * as useResponsiveLayoutModule from '@hooks/useResponsiveLayout';
1111
import type ResponsiveLayoutResult from '@hooks/useResponsiveLayout/types';
1212
import * as Localize from '@libs/Localize';

tests/utils/LHNTestUtils.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import React from 'react';
66
import ComposeProviders from '@components/ComposeProviders';
77
import {LocaleContextProvider} from '@components/LocaleContextProvider';
88
import OnyxProvider from '@components/OnyxProvider';
9-
import {CurrentReportIDContextProvider} from '@components/withCurrentReportID';
109
import {EnvironmentProvider} from '@components/withEnvironment';
10+
import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID';
1111
import {ReportIDsContextProvider} from '@hooks/useReportIDs';
1212
import DateUtils from '@libs/DateUtils';
1313
import * as ReportUtils from '@libs/ReportUtils';

0 commit comments

Comments
 (0)