Repository navigation
Allow initial avatars #72685
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
grgia
merged 25 commits into
Expensify:main
from
software-mansion-labs:feat/allow-initial-avatars
Oct 21, 2025
Merged
Allow initial avatars #72685
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a5248fe
poc
jmusial bc03ce3
fix lint
jmusial 9a4a225
Merge branch 'main' into feat/allow-initial-avatars
jmusial 1db86c7
cleanup AvatarCatalog; Add tests for useLetterAvatars
jmusial eff232b
fix lint
jmusial ada1a9b
update Avatar Selector tests
jmusial 81d98f3
rename ColoredLetterAvatar
jmusial 6354223
Refactor Avatar Page
jmusial 63f951f
remove unused types
jmusial 8a752d8
update translation
jmusial 8f75984
make jest happy
jmusial 3e97b47
update avatar generation logic
jmusial 94b6cb0
small cleanup
jmusial 80d024c
Merge branch 'main' into feat/allow-initial-avatars
jmusial ae5f566
Add label, fix crash on special characters
jmusial a17a2b4
prettier fix
jmusial 8695a2e
fix prettier v2
jmusial 86c7207
update spacing
jmusial c23f38d
Clean up avatar preview flow
jmusial 95987be
Update translations
jmusial dc94b97
fix test
jmusial 589bfcf
fix lint
jmusial 0bd58e5
Merge branch 'main' into feat/allow-initial-avatars
jmusial 45bbb85
fix tsc
jmusial 8e7f89c
prettier fix
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 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
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,44 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import type {SvgProps} from 'react-native-svg'; | ||
| import useStyleUtils from '@hooks/useStyleUtils'; | ||
| import type {AvatarSizeName} from '@styles/utils'; | ||
| import CONST from '@src/CONST'; | ||
| import ImageSVG from './ImageSVG'; | ||
|
|
||
| type ColoredLetterAvatarProps = { | ||
| /** The SVG component to render */ | ||
| component: React.FC<SvgProps>; | ||
| /** Background color for the circular background */ | ||
| backgroundColor: string; | ||
| /** Fill color for the SVG icon */ | ||
| fillColor: string; | ||
| /** Size of the avatar */ | ||
| size?: AvatarSizeName; | ||
| }; | ||
|
|
||
| /** | ||
| * ColoredLetterAvatar renders an SVG component with a colored circular background. | ||
| * Used for letter avatars and other colored icon avatars. | ||
| */ | ||
| function ColoredLetterAvatar({component, backgroundColor, fillColor, size = CONST.AVATAR_SIZE.MEDIUM}: ColoredLetterAvatarProps) { | ||
| const StyleUtils = useStyleUtils(); | ||
| const avatarSize = StyleUtils.getAvatarSize(size); | ||
| return ( | ||
| <View | ||
| style={{width: avatarSize, height: avatarSize, backgroundColor}} | ||
| dataSet={{id: 'colored-avatar'}} | ||
| > | ||
| <ImageSVG | ||
| src={component} | ||
| width={avatarSize} | ||
| height={avatarSize} | ||
| fill={fillColor} | ||
| /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| ColoredLetterAvatar.displayName = 'ColoredLetterAvatar'; | ||
|
|
||
| export default ColoredLetterAvatar; |
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,82 @@ | ||
| import {useCallback} from 'react'; | ||
| import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types'; | ||
| import * as Expensicons from '@components/Icon/Expensicons'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type {FileObject} from '@src/types/utils/Attachment'; | ||
| import useLocalize from './useLocalize'; | ||
|
|
||
| type OpenPicker = (options: {onPicked: (files: FileObject[]) => void}) => void; | ||
|
|
||
| type UseAvatarMenuParams = { | ||
| /** Whether the user is using a default avatar */ | ||
| isUsingDefaultAvatar: boolean; | ||
| /** Whether the user has chosen a new avatar in the form but hasn't uploaded it yet */ | ||
| isAvatarSelected: boolean; | ||
| /** Account ID for navigation */ | ||
| accountID: number; | ||
| /** Callback when avatar is removed */ | ||
| onImageRemoved: () => void; | ||
| /** Callback to show avatar crop modal */ | ||
| showAvatarCropModal: (image: FileObject) => void; | ||
| /** Callback to clear errors */ | ||
| clearError: () => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Custom hook to create avatar menu items | ||
| */ | ||
| function useAvatarMenu({isUsingDefaultAvatar, isAvatarSelected, accountID, onImageRemoved, showAvatarCropModal, clearError}: UseAvatarMenuParams) { | ||
| const {translate} = useLocalize(); | ||
|
|
||
| /** | ||
| * Create menu items list for avatar menu | ||
| */ | ||
| const createMenuItems = useCallback( | ||
| (openPicker: OpenPicker): Array<DropdownOption<null>> => { | ||
| const menuItems: Array<DropdownOption<null>> = [ | ||
| { | ||
| icon: Expensicons.Upload, | ||
| text: translate('avatarWithImagePicker.uploadPhoto'), | ||
| onSelected: () => { | ||
| openPicker({ | ||
| onPicked: (data) => showAvatarCropModal(data.at(0) ?? {}), | ||
| }); | ||
| }, | ||
| value: null, | ||
| }, | ||
| ]; | ||
| // If current avatar is a default avatar and for no avatar is selected in the form, only show upload option | ||
| if (isUsingDefaultAvatar && !isAvatarSelected) { | ||
| return menuItems; | ||
| } | ||
| menuItems.push({ | ||
| icon: Expensicons.Trashcan, | ||
| text: translate('avatarWithImagePicker.removePhoto'), | ||
| value: null, | ||
| onSelected: () => { | ||
| clearError(); | ||
| onImageRemoved(); | ||
| }, | ||
| }); | ||
| // If an avatar is selected in the form do NOT show view photo | ||
| if (isAvatarSelected) { | ||
| return menuItems; | ||
| } | ||
| return [ | ||
| ...menuItems, | ||
| { | ||
| value: null, | ||
| icon: Expensicons.Eye, | ||
| text: translate('avatarWithImagePicker.viewPhoto'), | ||
| onSelected: () => Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(accountID)), | ||
| }, | ||
| ]; | ||
| }, | ||
| [accountID, isUsingDefaultAvatar, onImageRemoved, showAvatarCropModal, translate, clearError, isAvatarSelected], | ||
| ); | ||
|
|
||
| return {createMenuItems}; | ||
| } | ||
|
|
||
| export default useAvatarMenu; |
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,76 @@ | ||
| import React, {useMemo} from 'react'; | ||
| import type {SvgProps} from 'react-native-svg'; | ||
| import ColoredLetterAvatar from '@components/ColoredLetterAvatar'; | ||
| import {getLetterAvatar, LETTER_AVATAR_COLOR_OPTIONS} from '@libs/Avatars/CustomAvatarCatalog'; | ||
| import getFirstAlphaNumericCharacter from '@libs/getFirstAlphaNumericCharacter'; | ||
| import type {AvatarSizeName} from '@styles/utils'; | ||
|
|
||
| /** | ||
| * Represents a single letter avatar item with its unique identifier and styled component. | ||
| */ | ||
| type LetterAvatarItem = { | ||
| /** Unique identifier combining background color, fill color, and initial letter */ | ||
| id: string; | ||
| /** Styled letter avatar component with applied colors */ | ||
| StyledLetterAvatar: React.FC<SvgProps>; | ||
| }; | ||
|
|
||
| /** | ||
| * Return type for useLetterAvatars hook containing avatar data in multiple formats. | ||
| */ | ||
| type LetterAvatarsResult = { | ||
| /** Array of avatar items for use in AvatarSelector component, containing all color variations */ | ||
| avatarList: LetterAvatarItem[]; | ||
| /** Object mapping avatar IDs to components for quick lookup in AvatarPage component */ | ||
| avatarMap: Record<string, React.FC<SvgProps>>; | ||
| }; | ||
|
|
||
| /** | ||
| * Generates letter avatars based on a user's name initial in all available color combinations. | ||
| * | ||
| * @param name - The user's name from which the first alphanumeric character is extracted | ||
| * @param size - Optional size for the avatars | ||
| * @returns Object with avatarList (array for AvatarSelector) and avatarMap (lookup for AvatarPage) | ||
| */ | ||
| function useLetterAvatars(name: string | undefined, size?: AvatarSizeName): LetterAvatarsResult { | ||
| return useMemo(() => { | ||
| const avatarComponent = getLetterAvatar(name); | ||
|
|
||
| if (!avatarComponent) { | ||
| return {avatarList: [], avatarMap: {}}; | ||
| } | ||
|
|
||
| const avatarList: LetterAvatarItem[] = []; | ||
| const avatarMap: Record<string, React.FC<SvgProps>> = {}; | ||
|
|
||
| LETTER_AVATAR_COLOR_OPTIONS.forEach(({fillColor, backgroundColor}) => { | ||
| function StyledLetterAvatar() { | ||
| return ( | ||
| <ColoredLetterAvatar | ||
| fillColor={fillColor} | ||
| backgroundColor={backgroundColor} | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| component={avatarComponent!} | ||
| size={size} | ||
| /> | ||
| ); | ||
| } | ||
| const id = `letter-avatar-${backgroundColor}-${fillColor}-${getFirstAlphaNumericCharacter(name)}`; | ||
|
|
||
| avatarList.push({ | ||
| id, | ||
| StyledLetterAvatar, | ||
| }); | ||
|
|
||
| avatarMap[id] = StyledLetterAvatar; | ||
| }); | ||
|
|
||
| return { | ||
| avatarList, | ||
| avatarMap, | ||
| }; | ||
| }, [name, size]); | ||
| } | ||
|
|
||
| export default useLetterAvatars; | ||
| export type {LetterAvatarItem, LetterAvatarsResult}; |
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
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.
Do we need this?