Skip to content

Commit

Permalink
Merge pull request #29547 from VickyStash/ts-migration/withCurrentUse…
Browse files Browse the repository at this point in the history
…rPersonalDetails-hoc

[No QA] [TS migration] Migrate 'withCurrentUserPersonalDetails.js' HOC to TypeScript
  • Loading branch information
dangrous authored Oct 20, 2023
2 parents ad8b9ff + 8ab35de commit 212e405
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 74 deletions.
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: OnyxEntry<Record<string, PersonalDetails>>;

/** 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];
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>({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
})(withCurrentUserPersonalDetails);
}

export {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps};

0 comments on commit 212e405

Please sign in to comment.