signal-desktop/ts/components/Avatar.tsx

220 lines
5 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React, {
FunctionComponent,
ReactNode,
useEffect,
useState,
} from 'react';
import classNames from 'classnames';
2021-04-27 13:20:17 +00:00
import { noop } from 'lodash';
import { Spinner } from './Spinner';
import { getInitials } from '../util/getInitials';
import { LocalizerType } from '../types/Util';
2021-08-06 21:35:25 +00:00
import { AvatarColorType } from '../types/Colors';
import * as log from '../logging/log';
2021-04-27 13:20:17 +00:00
import { assert } from '../util/assert';
import { shouldBlurAvatar } from '../util/shouldBlurAvatar';
2021-04-27 13:20:17 +00:00
export enum AvatarBlur {
NoBlur,
BlurPicture,
BlurPictureWithClickToView,
}
export enum AvatarSize {
TWENTY_EIGHT = 28,
THIRTY_TWO = 32,
THIRTY_SIX = 36,
FIFTY_TWO = 52,
EIGHTY = 80,
NINETY_SIX = 96,
ONE_HUNDRED_TWELVE = 112,
}
2020-05-27 21:37:06 +00:00
export type Props = {
avatarPath?: string;
2021-04-27 13:20:17 +00:00
blur?: AvatarBlur;
2021-05-28 16:15:17 +00:00
color?: AvatarColorType;
loading?: boolean;
2021-05-07 22:21:10 +00:00
acceptedMessageRequest: boolean;
conversationType: 'group' | 'direct';
2021-05-07 22:21:10 +00:00
isMe: boolean;
name?: string;
noteToSelf?: boolean;
phoneNumber?: string;
profileName?: string;
2021-05-07 22:21:10 +00:00
sharedGroupNames: Array<string>;
size: AvatarSize;
title: string;
unblurredAvatarPath?: string;
2019-10-17 18:22:07 +00:00
onClick?: () => unknown;
// Matches Popper's RefHandler type
innerRef?: React.Ref<HTMLDivElement>;
2019-10-17 18:22:07 +00:00
i18n: LocalizerType;
2020-05-27 21:37:06 +00:00
} & Pick<React.HTMLProps<HTMLDivElement>, 'className'>;
const getDefaultBlur = (
...args: Parameters<typeof shouldBlurAvatar>
): AvatarBlur =>
shouldBlurAvatar(...args) ? AvatarBlur.BlurPicture : AvatarBlur.NoBlur;
export const Avatar: FunctionComponent<Props> = ({
acceptedMessageRequest,
avatarPath,
className,
2021-08-06 21:35:25 +00:00
color,
conversationType,
i18n,
isMe,
innerRef,
loading,
noteToSelf,
onClick,
sharedGroupNames,
size,
title,
unblurredAvatarPath,
blur = getDefaultBlur({
acceptedMessageRequest,
avatarPath,
isMe,
sharedGroupNames,
unblurredAvatarPath,
}),
}) => {
const [imageBroken, setImageBroken] = useState(false);
useEffect(() => {
setImageBroken(false);
}, [avatarPath]);
2021-04-27 13:20:17 +00:00
useEffect(() => {
if (!avatarPath) {
return noop;
}
const image = new Image();
image.src = avatarPath;
image.onerror = () => {
log.warn('Avatar: Image failed to load; failing over to placeholder');
setImageBroken(true);
};
return () => {
image.onerror = noop;
};
}, [avatarPath]);
const initials = getInitials(title);
const hasImage = !noteToSelf && avatarPath && !imageBroken;
const shouldUseInitials =
!hasImage && conversationType === 'direct' && Boolean(initials);
let contents: ReactNode;
if (loading) {
const svgSize = size < 40 ? 'small' : 'normal';
contents = (
<div className="module-Avatar__spinner-container">
<Spinner
size={`${size - 8}px`}
svgSize={svgSize}
direction="on-avatar"
/>
</div>
2020-09-12 00:46:52 +00:00
);
} else if (hasImage) {
2021-04-27 13:20:17 +00:00
assert(avatarPath, 'avatarPath should be defined here');
2021-04-27 13:20:17 +00:00
assert(
blur !== AvatarBlur.BlurPictureWithClickToView || size >= 100,
'Rendering "click to view" for a small avatar. This may not render correctly'
);
const isBlurred =
blur === AvatarBlur.BlurPicture ||
blur === AvatarBlur.BlurPictureWithClickToView;
contents = (
2021-04-27 13:20:17 +00:00
<>
<div
className="module-Avatar__image"
style={{
backgroundImage: `url('${encodeURI(avatarPath)}')`,
...(isBlurred ? { filter: `blur(${Math.ceil(size / 2)}px)` } : {}),
}}
/>
{blur === AvatarBlur.BlurPictureWithClickToView && (
<div className="module-Avatar__click-to-view">{i18n('view')}</div>
)}
</>
);
} else if (noteToSelf) {
contents = (
<div
className={classNames(
'module-Avatar__icon',
2021-08-06 00:17:05 +00:00
`module-Avatar--${color}--icon`,
'module-Avatar__icon--note-to-self'
)}
/>
);
} else if (shouldUseInitials) {
contents = (
<div
2021-04-27 13:20:17 +00:00
aria-hidden="true"
className="module-Avatar__label"
2021-08-06 21:35:25 +00:00
style={{ fontSize: Math.ceil(size * 0.45) }}
>
{initials}
</div>
);
} else {
contents = (
<div
className={classNames(
'module-Avatar__icon',
2021-08-06 00:17:05 +00:00
`module-Avatar--${color}--icon`,
`module-Avatar__icon--${conversationType}`
)}
/>
);
}
if (onClick) {
contents = (
<button className="module-Avatar__button" type="button" onClick={onClick}>
2019-11-07 21:36:16 +00:00
{contents}
</button>
);
}
return (
<div
2021-04-27 13:20:17 +00:00
aria-label={i18n('contactAvatarAlt', [title])}
className={classNames(
'module-Avatar',
hasImage ? 'module-Avatar--with-image' : 'module-Avatar--no-image',
{
[`module-Avatar--${color}`]: !hasImage,
},
className
)}
style={{
minWidth: size,
width: size,
height: size,
}}
ref={innerRef}
>
{contents}
</div>
);
};