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

[No QA] [TS migration] Migrate 'withCurrentUserPersonalDetails.js' HOC to TypeScript #29547

Merged
Show file tree
Hide file tree
Changes from 2 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
74 changes: 0 additions & 74 deletions src/components/withCurrentUserPersonalDetails.js

This file was deleted.

67 changes: 67 additions & 0 deletions src/components/withCurrentUserPersonalDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, {ComponentType, RefAttributes, ForwardedRef, useMemo} from 'react';
import {OnyxEntry, withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import ONYXKEYS from '../ONYXKEYS';
import personalDetailsPropType from '../pages/personalDetailsPropType';
import type {PersonalDetails, Session} from '../types/onyx';

type CurrentUserPersonalDetails = PersonalDetails | Record<string, never>;

type OnyxProps = {
/** Personal details of all the users, including current user */
personalDetails: Record<string, PersonalDetails> | null;
VickyStash marked this conversation as resolved.
Show resolved Hide resolved

/** Session of the current user */
session: OnyxEntry<Session>;
};

type HOCProps = {
currentUserPersonalDetails: CurrentUserPersonalDetails;
};

type ComponentProps = OnyxProps & HOCProps;

// TODO: remove when all components that use it will be migrated to TS
const withCurrentUserPersonalDetailsPropTypes = {
currentUserPersonalDetails: personalDetailsPropType,
};

const withCurrentUserPersonalDetailsDefaultProps: HOCProps = {
currentUserPersonalDetails: {},
};

export default function <TProps extends ComponentProps, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): ComponentType<Omit<Omit<TProps, keyof HOCProps> & RefAttributes<TRef>, keyof OnyxProps>> {
function WithCurrentUserPersonalDetails(props: Omit<TProps, keyof HOCProps>, ref: ForwardedRef<TRef>) {
const accountID = props.session?.accountID ?? 0;
const accountPersonalDetails = props.personalDetails?.[accountID];
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Just one question - can these be null (i.e. do they need the ?s? Seems like they'll always be there, right? Not marked as optional in OnyxProps...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dangrous Since session and personalDetails are OnyxEntry, and OnyxEntry can be null, we need the ?s.

const currentUserPersonalDetails: CurrentUserPersonalDetails = useMemo(
() => (accountPersonalDetails ? {...accountPersonalDetails, accountID} : {}),
[accountPersonalDetails, accountID],
);
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
currentUserPersonalDetails={currentUserPersonalDetails}
/>
);
}

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

const withCurrentUserPersonalDetails = React.forwardRef(WithCurrentUserPersonalDetails);

return withOnyx<Omit<TProps, keyof HOCProps> & RefAttributes<TRef>, OnyxProps>({
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
})(withCurrentUserPersonalDetails);
}

export {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps};
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