Skip to content

Commit

Permalink
Merge pull request #43366 from software-mansion-labs/kicu/41912-avata…
Browse files Browse the repository at this point in the history
…r-cleanup

Make <Avatar> related code more consistent
  • Loading branch information
grgia authored Jun 14, 2024
2 parents ebafd6c + 6562f74 commit 15a6ef4
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type AvatarProps = {
/** Used to locate fallback icon in end-to-end tests. */
fallbackIconTestID?: string;

/** Denotes whether it is an avatar or a workspace avatar */
type?: AvatarType;

/** Owner of the avatar. If user, displayName. If workspace, policy name */
name?: string;

/** Denotes whether it is an avatar or a workspace avatar */
type: AvatarType;

/** Optional account id if it's user avatar or policy id if it's workspace avatar */
avatarID?: number | string;
};
Expand All @@ -64,7 +64,7 @@ function Avatar({
fill,
fallbackIcon = Expensicons.FallbackAvatar,
fallbackIconTestID = '',
type = CONST.ICON_TYPE_AVATAR,
type,
name = '',
avatarID,
}: AvatarProps) {
Expand All @@ -80,9 +80,9 @@ function Avatar({
}, [originalSource]);

const isWorkspace = type === CONST.ICON_TYPE_WORKSPACE;
const userAccountID = isWorkspace ? undefined : (avatarID as number);

// If it's user avatar then accountID will be a number
const source = isWorkspace ? originalSource : UserUtils.getAvatar(originalSource, avatarID as number);
const source = isWorkspace ? originalSource : UserUtils.getAvatar(originalSource, userAccountID);
const useFallBackAvatar = imageError || !source || source === Expensicons.FallbackAvatar;
const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(name) : fallbackIcon || Expensicons.FallbackAvatar;
const fallbackAvatarTestID = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatarTestID(name) : fallbackIconTestID || 'SvgFallbackAvatar Icon';
Expand Down
1 change: 1 addition & 0 deletions src/components/AvatarWithIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function AvatarWithIndicator({source, accountID, tooltipText = '', fallbackIcon
source={UserUtils.getSmallSizeAvatar(source, accountID)}
fallbackIcon={fallbackIcon}
avatarID={accountID}
type={CONST.ICON_TYPE_AVATAR}
/>
<Indicator />
</>
Expand Down
8 changes: 4 additions & 4 deletions src/components/LHNOptionsList/OptionRowLHN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ function OptionRowLHN({reportID, isFocused = false, onSelectRow = () => {}, opti
>
<View style={sidebarInnerRowStyle}>
<View style={[styles.flexRow, styles.alignItemsCenter]}>
{(optionItem.icons?.length ?? 0) > 0 &&
{!!optionItem.icons?.length &&
(optionItem.shouldShowSubscript ? (
<SubscriptAvatar
backgroundColor={hovered && !isFocused ? hoveredBackgroundColor : subscriptAvatarBorderColor}
mainAvatar={optionItem.icons?.[0]}
secondaryAvatar={optionItem.icons?.[1]}
mainAvatar={optionItem.icons[0]}
secondaryAvatar={optionItem.icons[1]}
size={isInFocusMode ? CONST.AVATAR_SIZE.SMALL : CONST.AVATAR_SIZE.DEFAULT}
/>
) : (
<MultipleAvatars
icons={optionItem.icons ?? []}
icons={optionItem.icons}
isFocusMode={isInFocusMode}
size={isInFocusMode ? CONST.AVATAR_SIZE.SMALL : CONST.AVATAR_SIZE.DEFAULT}
secondAvatarStyle={[
Expand Down
1 change: 1 addition & 0 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ function MenuItem(
avatarID={avatarID}
fallbackIcon={fallbackIcon}
size={avatarSize}
type={CONST.ICON_TYPE_AVATAR}
/>
)}
</View>
Expand Down
4 changes: 2 additions & 2 deletions src/components/SubscriptAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type SubIcon = {
};

type SubscriptAvatarProps = {
/** Avatar URL or icon */
mainAvatar?: IconType;
/** Avatar icon */
mainAvatar: IconType;

/** Subscript avatar URL or icon */
secondaryAvatar?: IconType;
Expand Down
1 change: 0 additions & 1 deletion src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ export {
generateAccountID,
getAvatar,
getAvatarUrl,
getDefaultAvatar,
getDefaultAvatarURL,
getFullSizeAvatar,
getLoginListBrickRoadIndicator,
Expand Down
1 change: 1 addition & 0 deletions src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ function ProfilePage({route}: ProfilePageProps) {
imageStyles={[styles.avatarXLarge]}
source={details.avatar}
avatarID={accountID}
type={CONST.ICON_TYPE_AVATAR}
size={CONST.AVATAR_SIZE.XLARGE}
fallbackIcon={fallbackIcon}
/>
Expand Down
1 change: 1 addition & 0 deletions src/pages/ReportParticipantDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function ReportParticipantDetails({personalDetails, report, route}: ReportPartic
imageStyles={[styles.avatarXLarge]}
source={details.avatar}
avatarID={accountID}
type={CONST.ICON_TYPE_AVATAR}
size={CONST.AVATAR_SIZE.XLARGE}
fallbackIcon={fallbackIcon}
/>
Expand Down
1 change: 1 addition & 0 deletions src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function WorkspaceProfilePage({policy, currencyList = {}, route}: WorkSpaceProfi
<AvatarWithImagePicker
onViewPhotoPress={() => Navigation.navigate(ROUTES.WORKSPACE_AVATAR.getRoute(policy?.id ?? '-1'))}
source={policy?.avatarURL ?? ''}
avatarID={policy?.id}
size={CONST.AVATAR_SIZE.XLARGE}
avatarStyle={styles.avatarXLarge}
enablePreview
Expand Down
1 change: 1 addition & 0 deletions src/pages/workspace/WorkspacesListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ function WorkspacesListRow({
<Avatar
source={ownerDetails.avatar}
avatarID={ownerDetails.accountID}
type={CONST.ICON_TYPE_AVATAR}
size={CONST.AVATAR_SIZE.SMALL}
containerStyles={styles.workspaceOwnerAvatarWrapper}
/>
Expand Down
1 change: 1 addition & 0 deletions src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
imageStyles={[styles.avatarXLarge]}
source={details.avatar}
avatarID={accountID}
type={CONST.ICON_TYPE_AVATAR}
size={CONST.AVATAR_SIZE.XLARGE}
fallbackIcon={fallbackIcon}
/>
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/UserUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import * as UserUtils from '@src/libs/UserUtils';

describe('UserUtils', () => {
it('should return the default avatar from the avatar url', () => {
it('should return default avatar if the url is for default avatar', () => {
const avatarURL = 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png';
const defaultAvatar = UserUtils.getDefaultAvatar(1, avatarURL);
const defaultAvatar = UserUtils.getAvatar(avatarURL, 1);

expect(typeof defaultAvatar).toBe('function');
});

it('should return the same url if url is not for default avatar', () => {
const avatarURL = 'https://test.com/images/some_avatar.png';
const avatar = UserUtils.getAvatar(avatarURL, 1);

expect(avatar).toEqual('https://test.com/images/some_avatar.png');
});
});

0 comments on commit 15a6ef4

Please sign in to comment.