Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'useCurrentReportID.js' hook to TypeScript #29264

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
},
{
selector: ['parameter', 'method'],
format: ['camelCase'],
format: ['camelCase', 'PascalCase'],
},
],
'@typescript-eslint/ban-types': [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React, {createContext, forwardRef, useCallback, useState, useMemo} from 'react';
import React, {createContext, forwardRef, useCallback, useState, useMemo, RefAttributes, ComponentType} from 'react';
import PropTypes from 'prop-types';
import {NavigationState} from '@react-navigation/native';

import getComponentDisplayName from '../libs/getComponentDisplayName';
import Navigation from '../libs/Navigation/Navigation';

const CurrentReportIDContext = createContext(null);
type CurrentReportIDContextValue = {
updateCurrentReportID: (state: NavigationState) => void;
currentReportID: string;
};
type CurrentReportIDContextProviderProps = {
children: React.ReactNode;
};
Copy link
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);

kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
const withCurrentReportIDPropTypes = {
/** Function to update the state */
Expand All @@ -18,26 +26,26 @@ const withCurrentReportIDDefaultProps = {
currentReportID: '',
};

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

/**
* This function is used to update the currentReportID
* @param {Object} state root navigation state
* @param state root navigation state
*/
const updateCurrentReportID = useCallback(
(state) => {
setCurrentReportID(Navigation.getTopmostReportId(state));
(state: NavigationState) => {
setCurrentReportID(Navigation.getTopmostReportId(state) ?? '');
},
Copy link
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
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 {Object} currentReportID to share between central pane and LHN
* @returns currentReportID to share between central pane and LHN
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @returns currentReportID to share between central pane and LHN
* @returns currentReportID to share between central pane and LHN

const contextValue = useMemo(
() => ({
(): CurrentReportIDContextValue => ({
updateCurrentReportID,
currentReportID,
}),
Expand All @@ -53,8 +61,8 @@ CurrentReportIDContextProvider.propTypes = {
children: PropTypes.node.isRequired,
};

export default function withCurrentReportID(WrappedComponent) {
const WithCurrentReportID = forwardRef((props, ref) => (
export default function withCurrentReportID<TComponentProps extends CurrentReportIDContextValue>(WrappedComponent: ComponentType<TComponentProps>) {
const WithCurrentReportID: ComponentType<TComponentProps & RefAttributes<ComponentType<TComponentProps>>> = forwardRef((props, ref) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Maybe rename to TComponentProps to TProps for better visibility?
  2. HOCs need to be more generic in terms of ref, that's why we need to add another type parameter TRef. Without a type parameter, typescript complains with this test code:
const X = forwardRef((props: CurrentReportIDContextValue, ref: React.Ref<HTMLInputElement>) => {
    console.log(props.currentReportID);
    console.log(props.updateCurrentReportID);
    return <input ref={ref} />;
});

function Test() {
    const Wrapped = withCurrentReportID(X);

    const testRef = useRef<HTMLInputElement>(null);

    const x = <Wrapped ref={testRef} />;
}
  1. According to the guidelines (discussion on slack). We should use forwardRef a bit different, extract the component and define it in a separate line. Wrap it in forwardRef on the last line of withCurrentReportID HOC.

Final code looks like this:

export default function withCurrentReportID<TProps extends CurrentReportIDContextValue, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) {
    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);
}

<CurrentReportIDContext.Consumer>
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
{(currentReportIDUtils) => (
<WrappedComponent
Expand All @@ -74,3 +82,4 @@ export default function withCurrentReportID(WrappedComponent) {
}

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);
}
2 changes: 1 addition & 1 deletion src/libs/getComponentDisplayName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentType} from 'react';

/** Returns the display name of a component */
export default function getComponentDisplayName(component: ComponentType): string {
export default function getComponentDisplayName<TProps>(component: ComponentType<TProps>): string {
return component.displayName ?? component.name ?? 'Component';
}
Loading