-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29547 from VickyStash/ts-migration/withCurrentUse…
…rPersonalDetails-hoc [No QA] [TS migration] Migrate 'withCurrentUserPersonalDetails.js' HOC to TypeScript
- Loading branch information
Showing
2 changed files
with
67 additions
and
74 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |