2023-03-24 17:16:46 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import {
|
|
|
|
TextColor,
|
|
|
|
IconColor,
|
|
|
|
AlignItems,
|
|
|
|
DISPLAY,
|
|
|
|
JustifyContent,
|
2023-06-07 15:18:49 +02:00
|
|
|
BackgroundColor,
|
2023-03-24 17:16:46 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
|
|
|
import {
|
|
|
|
AvatarFavicon,
|
|
|
|
BadgeWrapper,
|
|
|
|
BadgeWrapperPosition,
|
|
|
|
AvatarIcon,
|
|
|
|
AvatarBase,
|
2023-04-19 23:16:49 +02:00
|
|
|
IconName,
|
|
|
|
IconSize,
|
2023-03-24 17:16:46 +01:00
|
|
|
} from '../../../component-library';
|
|
|
|
import { getTargetSubjectMetadata } from '../../../../selectors';
|
|
|
|
|
2023-06-09 16:36:38 +02:00
|
|
|
const SnapAvatar = ({
|
|
|
|
snapId,
|
|
|
|
badgeSize = IconSize.Sm,
|
|
|
|
avatarSize = IconSize.Lg,
|
|
|
|
borderWidth = 2,
|
|
|
|
className,
|
|
|
|
}) => {
|
2023-03-24 17:16:46 +01:00
|
|
|
const subjectMetadata = useSelector((state) =>
|
|
|
|
getTargetSubjectMetadata(state, snapId),
|
|
|
|
);
|
|
|
|
|
|
|
|
const friendlyName = snapId && getSnapName(snapId, subjectMetadata);
|
|
|
|
|
|
|
|
const iconUrl = subjectMetadata?.iconUrl;
|
|
|
|
|
|
|
|
const fallbackIcon = friendlyName && friendlyName[0] ? friendlyName[0] : '?';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BadgeWrapper
|
|
|
|
className={classnames('snap-avatar', className)}
|
|
|
|
badge={
|
|
|
|
<AvatarIcon
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.Snaps}
|
2023-06-09 16:36:38 +02:00
|
|
|
size={badgeSize}
|
2023-03-24 17:16:46 +01:00
|
|
|
backgroundColor={IconColor.infoDefault}
|
2023-06-07 15:18:49 +02:00
|
|
|
borderColor={BackgroundColor.backgroundDefault}
|
2023-06-09 16:36:38 +02:00
|
|
|
borderWidth={borderWidth}
|
2023-03-24 17:16:46 +01:00
|
|
|
iconProps={{
|
2023-06-09 16:36:38 +02:00
|
|
|
size: badgeSize,
|
2023-03-24 17:16:46 +01:00
|
|
|
color: IconColor.infoInverse,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
position={BadgeWrapperPosition.bottomRight}
|
|
|
|
>
|
|
|
|
{iconUrl ? (
|
2023-06-09 16:36:38 +02:00
|
|
|
<AvatarFavicon size={avatarSize} src={iconUrl} name={friendlyName} />
|
2023-03-24 17:16:46 +01:00
|
|
|
) : (
|
|
|
|
<AvatarBase
|
2023-06-09 16:36:38 +02:00
|
|
|
size={avatarSize}
|
2023-03-24 17:16:46 +01:00
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
color={TextColor.textAlternative}
|
|
|
|
style={{ borderWidth: '0px' }}
|
|
|
|
>
|
|
|
|
{fallbackIcon}
|
|
|
|
</AvatarBase>
|
|
|
|
)}
|
|
|
|
</BadgeWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SnapAvatar.propTypes = {
|
|
|
|
/**
|
|
|
|
* The id of the snap
|
|
|
|
*/
|
|
|
|
snapId: PropTypes.string,
|
2023-06-09 16:36:38 +02:00
|
|
|
badgeSize: PropTypes.string,
|
|
|
|
avatarSize: PropTypes.string,
|
|
|
|
borderWidth: PropTypes.number,
|
2023-03-24 17:16:46 +01:00
|
|
|
/**
|
|
|
|
* The className of the SnapAvatar
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SnapAvatar;
|