Repository navigation
[TS migration] Migrate 'AvatarWithDisplayName.js' component to TypeScript #32114
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
Merged
techievivek
merged 6 commits into
Expensify:main
from
VickyStash:ts-migration/avatarWithDisplayName-component
Dec 5, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0ae29b
[TS migration] Migrate 'AvatarWithDisplayName.js' component
VickyStash c6c4cb8
Minor code improvement
VickyStash 7a5d6b0
Re-run performance test
VickyStash a624c3b
Merge branch 'main' into ts-migration/avatarWithDisplayName-component
VickyStash ea69b5c
TS updates after merging main
VickyStash 08a860d
Make params optional
VickyStash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,83 +1,76 @@ | ||
| import lodashGet from 'lodash/get'; | ||
| import PropTypes from 'prop-types'; | ||
| import React, {useCallback, useEffect, useRef} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import {withOnyx} from 'react-native-onyx'; | ||
| import _ from 'underscore'; | ||
| import {OnyxCollection, OnyxEntry, withOnyx} from 'react-native-onyx'; | ||
| import {ValueOf} from 'type-fest'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import * as OptionsListUtils from '@libs/OptionsListUtils'; | ||
| import * as ReportUtils from '@libs/ReportUtils'; | ||
| import reportActionPropTypes from '@pages/home/report/reportActionPropTypes'; | ||
| import reportPropTypes from '@pages/reportPropTypes'; | ||
| import * as StyleUtils from '@styles/StyleUtils'; | ||
| import useTheme from '@styles/themes/useTheme'; | ||
| import useThemeStyles from '@styles/useThemeStyles'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import {PersonalDetails, Policy, Report, ReportActions} from '@src/types/onyx'; | ||
| import DisplayNames from './DisplayNames'; | ||
| import MultipleAvatars from './MultipleAvatars'; | ||
| import ParentNavigationSubtitle from './ParentNavigationSubtitle'; | ||
| import participantPropTypes from './participantPropTypes'; | ||
| import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback'; | ||
| import SubscriptAvatar from './SubscriptAvatar'; | ||
| import Text from './Text'; | ||
|
|
||
| const propTypes = { | ||
| type AvatarWithDisplayNamePropsWithOnyx = { | ||
| /** All of the actions of the report */ | ||
| parentReportActions: OnyxEntry<ReportActions>; | ||
| }; | ||
|
|
||
| type AvatarWithDisplayNameProps = AvatarWithDisplayNamePropsWithOnyx & { | ||
| /** The report currently being looked at */ | ||
| report: reportPropTypes, | ||
| report: OnyxEntry<Report>; | ||
|
|
||
| /** The policy which the user has access to and which the report is tied to */ | ||
| policy: PropTypes.shape({ | ||
| /** Name of the policy */ | ||
| name: PropTypes.string, | ||
| }), | ||
| policy?: OnyxEntry<Policy>; | ||
|
|
||
| /** The size of the avatar */ | ||
| size: PropTypes.oneOf(_.values(CONST.AVATAR_SIZE)), | ||
| size?: ValueOf<typeof CONST.AVATAR_SIZE>; | ||
|
|
||
| /** Personal details of all the users */ | ||
| personalDetails: PropTypes.objectOf(participantPropTypes), | ||
| personalDetails: OnyxCollection<PersonalDetails>; | ||
|
|
||
| /** Whether if it's an unauthenticated user */ | ||
| isAnonymous: PropTypes.bool, | ||
| isAnonymous?: boolean; | ||
|
|
||
| shouldEnableDetailPageNavigation: PropTypes.bool, | ||
|
|
||
| /* Onyx Props */ | ||
| /** All of the actions of the report */ | ||
| parentReportActions: PropTypes.objectOf(PropTypes.shape(reportActionPropTypes)), | ||
| /** Whether we should enable detail page navigation */ | ||
| shouldEnableDetailPageNavigation?: boolean; | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| personalDetails: {}, | ||
| policy: {}, | ||
| report: {}, | ||
| parentReportActions: {}, | ||
| isAnonymous: false, | ||
| size: CONST.AVATAR_SIZE.DEFAULT, | ||
| shouldEnableDetailPageNavigation: false, | ||
| }; | ||
|
|
||
| function AvatarWithDisplayName({report, policy, size, isAnonymous, parentReportActions, personalDetails, shouldEnableDetailPageNavigation}) { | ||
| function AvatarWithDisplayName({ | ||
| personalDetails, | ||
| policy, | ||
| report, | ||
| parentReportActions, | ||
| isAnonymous = false, | ||
| size = CONST.AVATAR_SIZE.DEFAULT, | ||
| shouldEnableDetailPageNavigation = false, | ||
| }: AvatarWithDisplayNameProps) { | ||
| const theme = useTheme(); | ||
| const styles = useThemeStyles(); | ||
| const title = ReportUtils.getReportName(report); | ||
| const subtitle = ReportUtils.getChatRoomSubtitle(report); | ||
| const parentNavigationSubtitleData = ReportUtils.getParentNavigationSubtitle(report); | ||
| const isMoneyRequestOrReport = ReportUtils.isMoneyRequestReport(report) || ReportUtils.isMoneyRequest(report); | ||
| const icons = ReportUtils.getIcons(report, personalDetails, policy); | ||
| const ownerPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs([report.ownerAccountID], personalDetails); | ||
| const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(_.values(ownerPersonalDetails), false); | ||
| const icons = ReportUtils.getIcons(report, personalDetails, null, '', -1, policy); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was implemented incorrectly, but make sure to test it, so that it doesn't make any regressions 😄 |
||
| const ownerPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs(report?.ownerAccountID ? [report.ownerAccountID] : [], personalDetails); | ||
| const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(Object.values(ownerPersonalDetails), false); | ||
| const shouldShowSubscriptAvatar = ReportUtils.shouldReportShowSubscript(report); | ||
| const isExpenseRequest = ReportUtils.isExpenseRequest(report); | ||
| const defaultSubscriptSize = isExpenseRequest ? CONST.AVATAR_SIZE.SMALL_NORMAL : size; | ||
| const avatarBorderColor = isAnonymous ? theme.highlightBG : theme.componentBG; | ||
|
|
||
| const actorAccountID = useRef(null); | ||
| const actorAccountID = useRef<number | null>(null); | ||
| useEffect(() => { | ||
| const parentReportAction = lodashGet(parentReportActions, [report.parentReportActionID], {}); | ||
| actorAccountID.current = lodashGet(parentReportAction, 'actorAccountID', -1); | ||
| const parentReportAction = parentReportActions?.[report?.parentReportActionID ?? '']; | ||
| actorAccountID.current = parentReportAction?.actorAccountID ?? -1; | ||
| }, [parentReportActions, report]); | ||
|
|
||
| const showActorDetails = useCallback(() => { | ||
|
|
@@ -86,31 +79,33 @@ function AvatarWithDisplayName({report, policy, size, isAnonymous, parentReportA | |
| return ReportUtils.navigateToDetailsPage(report); | ||
| } | ||
|
|
||
| if (ReportUtils.isExpenseReport(report)) { | ||
| if (ReportUtils.isExpenseReport(report) && report?.ownerAccountID) { | ||
| Navigation.navigate(ROUTES.PROFILE.getRoute(report.ownerAccountID)); | ||
| return; | ||
| } | ||
|
|
||
| if (ReportUtils.isIOUReport(report)) { | ||
| if (ReportUtils.isIOUReport(report) && report?.reportID) { | ||
| Navigation.navigate(ROUTES.REPORT_PARTICIPANTS.getRoute(report.reportID)); | ||
| return; | ||
| } | ||
|
|
||
| if (ReportUtils.isChatThread(report)) { | ||
| // In an ideal situation account ID won't be 0 | ||
| if (actorAccountID.current > 0) { | ||
| if (actorAccountID.current && actorAccountID.current > 0) { | ||
| Navigation.navigate(ROUTES.PROFILE.getRoute(actorAccountID.current)); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Report detail route is added as fallback but based on the current implementation this route won't be executed | ||
| Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(report.reportID)); | ||
| if (report?.reportID) { | ||
| // Report detail route is added as fallback but based on the current implementation this route won't be executed | ||
| Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(report.reportID)); | ||
| } | ||
| }, [report, shouldEnableDetailPageNavigation]); | ||
|
|
||
| const headerView = ( | ||
| <View style={[styles.appContentHeaderTitle, styles.flex1]}> | ||
| {Boolean(report && title) && ( | ||
| {report && !!title && ( | ||
| <View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter, styles.justifyContentBetween]}> | ||
| <PressableWithoutFeedback | ||
| onPress={showActorDetails} | ||
|
|
@@ -141,13 +136,13 @@ function AvatarWithDisplayName({report, policy, size, isAnonymous, parentReportA | |
| textStyles={[isAnonymous ? styles.headerAnonymousFooter : styles.headerText, styles.pre]} | ||
| shouldUseFullTitle={isMoneyRequestOrReport || isAnonymous} | ||
| /> | ||
| {!_.isEmpty(parentNavigationSubtitleData) && ( | ||
| {Object.keys(parentNavigationSubtitleData).length > 0 && ( | ||
| <ParentNavigationSubtitle | ||
| parentNavigationSubtitleData={parentNavigationSubtitleData} | ||
| parentReportID={report.parentReportID} | ||
| parentReportID={report?.parentReportID} | ||
| /> | ||
| )} | ||
| {!_.isEmpty(subtitle) && ( | ||
| {!!subtitle && ( | ||
|
VickyStash marked this conversation as resolved.
|
||
| <Text | ||
| style={[styles.sidebarLinkText, styles.optionAlternateText, styles.textLabelSupporting, styles.pre]} | ||
| numberOfLines={1} | ||
|
|
@@ -176,11 +171,10 @@ function AvatarWithDisplayName({report, policy, size, isAnonymous, parentReportA | |
| </PressableWithoutFeedback> | ||
| ); | ||
| } | ||
| AvatarWithDisplayName.propTypes = propTypes; | ||
|
|
||
| AvatarWithDisplayName.displayName = 'AvatarWithDisplayName'; | ||
| AvatarWithDisplayName.defaultProps = defaultProps; | ||
|
|
||
| export default withOnyx({ | ||
| export default withOnyx<AvatarWithDisplayNameProps, AvatarWithDisplayNamePropsWithOnyx>({ | ||
| parentReportActions: { | ||
| key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, | ||
| canEvict: false, | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB: Some props like
sizeare made optional, any specific reason for the change?