Skip to content
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

Make <Avatar> related code more consistent #43366

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Kicu marked this conversation as resolved.
Show resolved Hide resolved
/** Optional account id if it's user avatar or policy id if it's workspace avatar */
avatarID?: number | string;
};
Expand Down Expand Up @@ -79,10 +79,10 @@ function Avatar({
setImageError(false);
}, [originalSource]);

const isWorkspace = type === CONST.ICON_TYPE_WORKSPACE;
const isWorkspace = type === 'workspace';
Kicu marked this conversation as resolved.
Show resolved Hide resolved
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
5 changes: 3 additions & 2 deletions src/components/LHNOptionsList/OptionRowLHN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ function OptionRowLHN({reportID, isFocused = false, onSelectRow = () => {}, opti
>
<View style={sidebarInnerRowStyle}>
<View style={[styles.flexRow, styles.alignItemsCenter]}>
{(optionItem.icons?.length ?? 0) > 0 &&
{optionItem.icons &&
optionItem.icons.length > 0 &&
Kicu marked this conversation as resolved.
Show resolved Hide resolved
(optionItem.shouldShowSubscript ? (
<SubscriptAvatar
backgroundColor={hovered && !isFocused ? hoveredBackgroundColor : subscriptAvatarBorderColor}
mainAvatar={optionItem.icons?.[0]}
mainAvatar={optionItem.icons[0]}
secondaryAvatar={optionItem.icons?.[1]}
Kicu marked this conversation as resolved.
Show resolved Hide resolved
size={isInFocusMode ? CONST.AVATAR_SIZE.SMALL : CONST.AVATAR_SIZE.DEFAULT}
/>
Expand Down
1 change: 1 addition & 0 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,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 @@ -141,6 +141,7 @@ function WorkspaceProfilePage({policy, currencyList = {}, route}: WorkSpaceProfi
<AvatarWithImagePicker
onViewPhotoPress={() => Navigation.navigate(ROUTES.WORKSPACE_AVATAR.getRoute(policy?.id ?? ''))}
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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');
});
});
Loading