-
Notifications
You must be signed in to change notification settings - Fork 4k
[No QA] Avatar component decomposition #95050
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
roryabraham
merged 22 commits into
Expensify:main
from
software-mansion-labs:chore/decomposition-of-avatar-component
Jul 26, 2026
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e55b38f
Avatar component decomposition poc
jmusial 5630d21
fix prettier
jmusial ad4a884
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial eb13be1
resolve differences after merge; add AvatarInitials
jmusial d0ac78a
add tests
jmusial acd871a
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial f902574
rename avatar primitives, move LettersAvatar out of common body
jmusial 889100d
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial af83ed3
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial 4a8f8f1
update folder structure
jmusial 88b1ae1
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial 601b3c6
add jsdoc to props; simplify hooks
jmusial 34fa6ee
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial 77f2fd7
simplify UserAvatar & WorkspaceAvatar types
jmusial b53c297
fix codex review
jmusial f47c476
Merge branch 'main' into chore/decomposition-of-avatar-component
jmusial 95815ca
liquidate AvatarBody
jmusial 21b19e5
Remove avatar hooks
jmusial 1070234
make account ID numeric
jmusial bfa5231
clenup initials avatar
jmusial 6817132
rename id prop
jmusial 7701a51
fix props name
jmusial 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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import useDefaultAvatars from '@hooks/useDefaultAvatars'; | ||
| import useStyleUtils from '@hooks/useStyleUtils'; | ||
| import useTheme from '@hooks/useTheme'; | ||
|
|
||
| import type {AvatarSource} from '@libs/UserAvatarUtils'; | ||
| import {getAvatar, optimizeAvatarSource, parseLetterAvatarURL} from '@libs/UserAvatarUtils'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import type {AvatarCommonProps} from './types'; | ||
|
|
||
| import useAvatarLoadError from './hooks/useAvatarLoadError'; | ||
| import AvatarContainer from './primitives/AvatarContainer'; | ||
| import AvatarIcon from './primitives/AvatarIcon'; | ||
| import AvatarImage from './primitives/AvatarImage'; | ||
| import AvatarLetter from './primitives/AvatarLetter'; | ||
|
|
||
| type UserAvatarProps = AvatarCommonProps & { | ||
| /** A fallback avatar icon to display when there is an error on loading avatar from remote URL. */ | ||
| fallbackIcon?: AvatarSource; | ||
|
|
||
| /** Used to locate fallback icon in end-to-end tests. */ | ||
| fallbackIconTestID?: string; | ||
|
|
||
| /** Owning account ID. Picks the default avatar when `source` is a default or absent. */ | ||
| accountID: number; | ||
| }; | ||
|
|
||
| /** Renders a user avatar, falling back to a default icon when no source is available. */ | ||
| function UserAvatar({ | ||
| source, | ||
| imageStyles, | ||
| iconAdditionalStyles, | ||
| containerStyles, | ||
| size = CONST.AVATAR_SIZE.DEFAULT, | ||
| fill, | ||
| fallbackIcon, | ||
| fallbackIconTestID = '', | ||
| accountID, | ||
| testID, | ||
| }: UserAvatarProps) { | ||
| const defaultAvatars = useDefaultAvatars(); | ||
| const theme = useTheme(); | ||
| const StyleUtils = useStyleUtils(); | ||
| const {hasImageError, onImageError} = useAvatarLoadError(source); | ||
|
|
||
| const resolvedSource = getAvatar({avatarSource: source, accountID, defaultAvatars}); | ||
|
|
||
| // Generated letter-avatar URLs are not fetched — their color and initials are parsed out and drawn locally. | ||
| const letterAvatar = parseLetterAvatarURL(resolvedSource); | ||
| if (letterAvatar) { | ||
| return ( | ||
| <AvatarContainer | ||
| containerStyles={containerStyles} | ||
| testID={testID} | ||
| > | ||
| <AvatarLetter | ||
| initials={letterAvatar.initials} | ||
| urlColors={letterAvatar.colors} | ||
| accountID={accountID} | ||
| size={size} | ||
| type={CONST.ICON_TYPE_AVATAR} | ||
| containerStyles={imageStyles} | ||
| containerAdditionalStyles={iconAdditionalStyles} | ||
| /> | ||
| </AvatarContainer> | ||
| ); | ||
| } | ||
|
|
||
| const optimizedSource = optimizeAvatarSource(resolvedSource); | ||
| const useFallBackAvatar = hasImageError || !resolvedSource || resolvedSource === defaultAvatars.FallbackAvatar; | ||
| const fallbackAvatar = (fallbackIcon ?? defaultAvatars.FallbackAvatar) || defaultAvatars.FallbackAvatar; | ||
| const fallbackAvatarTestID = fallbackIconTestID || 'SvgFallbackAvatar Icon'; | ||
| const avatarSource = useFallBackAvatar ? fallbackAvatar : (optimizedSource ?? fallbackAvatar); | ||
|
|
||
| if (typeof avatarSource === 'string') { | ||
| return ( | ||
| <AvatarContainer | ||
| containerStyles={containerStyles} | ||
| testID={testID} | ||
| > | ||
| <AvatarImage | ||
| avatarSource={avatarSource} | ||
| size={size} | ||
| type={CONST.ICON_TYPE_AVATAR} | ||
| imageStyles={imageStyles} | ||
| imageContainerAdditionalStyles={iconAdditionalStyles} | ||
| onImageError={onImageError} | ||
| /> | ||
| </AvatarContainer> | ||
| ); | ||
| } | ||
|
|
||
| let iconColors = null; | ||
| if (useFallBackAvatar && avatarSource === defaultAvatars.FallbackAvatar) { | ||
| iconColors = StyleUtils.getBackgroundColorAndFill(theme.buttonHoveredBG, theme.icon); | ||
| } | ||
|
|
||
| return ( | ||
| <AvatarContainer | ||
| containerStyles={containerStyles} | ||
| testID={testID} | ||
| > | ||
| <AvatarIcon | ||
| avatarSource={avatarSource} | ||
| size={size} | ||
| type={CONST.ICON_TYPE_AVATAR} | ||
| iconContainerStyles={imageStyles} | ||
| iconAdditionalStyles={iconAdditionalStyles} | ||
| fallbackAvatarTestID={fallbackAvatarTestID} | ||
| iconColors={iconColors} | ||
| fill={iconColors?.fill ?? (hasImageError ? theme.offline : fill)} | ||
| /> | ||
| </AvatarContainer> | ||
| ); | ||
| } | ||
|
|
||
| export default UserAvatar; | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Nit: the parent
code is duplicated couple of times. If possible we should think of avoiding it.
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.
In theory we could, but with each
Avatarprimitive having slightly different logic I think this is more readable (avoiding conditionals in the code or pushing it into primitives)