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
75 changes: 0 additions & 75 deletions src/components/withCurrentReportID.js

This file was deleted.

87 changes: 87 additions & 0 deletions src/components/withCurrentReportID.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {NavigationState} from '@react-navigation/native';
import PropTypes from 'prop-types';
import React, {ComponentType, createContext, ForwardedRef, forwardRef, RefAttributes, useCallback, useMemo, useState} from 'react';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import Navigation from '@libs/Navigation/Navigation';

type CurrentReportIDContextValue = {
updateCurrentReportID: (state: NavigationState) => void;
currentReportID: string;
};
type CurrentReportIDContextProviderProps = {
/** Actual content wrapped by this component */
children: React.ReactNode;
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.

NAB: Copy the comment from propTypes:

 /** Actual content wrapped by this component */

};

const CurrentReportIDContext = createContext<CurrentReportIDContextValue | null>(null);
Comment thread
kubabutkiewicz marked this conversation as resolved.

// TODO: Remove when depended components are migrated to TypeScript.
const withCurrentReportIDPropTypes = {
/** Function to update the state */
updateCurrentReportID: PropTypes.func.isRequired,

/** The top most report id */
currentReportID: PropTypes.string,
};

const withCurrentReportIDDefaultProps = {
currentReportID: '',
};

function CurrentReportIDContextProvider(props: CurrentReportIDContextProviderProps) {
const [currentReportID, setCurrentReportID] = useState('');

/**
* This function is used to update the currentReportID
* @param state root navigation state
*/
const updateCurrentReportID = useCallback(
(state: NavigationState) => {
setCurrentReportID(Navigation.getTopmostReportId(state) ?? '');
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.

why is this change needed?
I think it's original logic to return undefined or null as is

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.

@situchan So

function CurrentReportIDContextProvider(props) {
const [currentReportID, setCurrentReportID] = useState('');

The default state is empty string so useState is infering from this that currentReportID will be always string, and
Navigation.getTopmostReportId is returning string | undefined we need to use ?? to put empty string in case
Navigation.getTopmostReportId would return undefined

},
[setCurrentReportID],
);

/**
* The context this component exposes to child components
* @returns currentReportID to share between central pane and LHN
*/
const contextValue = useMemo(
(): CurrentReportIDContextValue => ({
updateCurrentReportID,
currentReportID,
}),
[updateCurrentReportID, currentReportID],
);

return <CurrentReportIDContext.Provider value={contextValue}>{props.children}</CurrentReportIDContext.Provider>;
}

CurrentReportIDContextProvider.displayName = 'CurrentReportIDContextProvider';

export default function withCurrentReportID<TProps extends CurrentReportIDContextValue, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): (props: Omit<TProps, keyof CurrentReportIDContextValue> & React.RefAttributes<TRef>) => React.ReactElement | null {
function WithCurrentReportID(props: Omit<TProps, keyof CurrentReportIDContextValue>, ref: ForwardedRef<TRef>) {
return (
<CurrentReportIDContext.Consumer>
{(currentReportIDUtils) => (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...currentReportIDUtils}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
/>
)}
</CurrentReportIDContext.Consumer>
);
}

WithCurrentReportID.displayName = `withCurrentReportID(${getComponentDisplayName(WrappedComponent)})`;

return forwardRef(WithCurrentReportID);
Comment on lines +81 to +83
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.

I expected this would cause console error since displayName is set to component wrapped with forwardRef.
But actually not happening. Not sure why.

}

export {withCurrentReportIDPropTypes, withCurrentReportIDDefaultProps, CurrentReportIDContextProvider, CurrentReportIDContext};
export type {CurrentReportIDContextValue};
6 changes: 0 additions & 6 deletions src/hooks/useCurrentReportID.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/hooks/useCurrentReportID.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useContext} from 'react';
import {CurrentReportIDContext, CurrentReportIDContextValue} from '@components/withCurrentReportID';

export default function useCurrentReportID(): CurrentReportIDContextValue | null {
return useContext(CurrentReportIDContext);
}