signal-desktop/ts/components/Avatar.tsx

308 lines
7.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 type {
FunctionComponent,
2021-10-12 19:07:58 +00:00
MouseEvent,
ReactChild,
ReactNode,
} from 'react';
import React, { 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 type { LocalizerType } from '../types/Util';
2021-11-02 23:01:13 +00:00
import { ThemeType } from '../types/Util';
import type { AvatarColorType } from '../types/Colors';
2021-11-02 23:01:13 +00:00
import type { BadgeType } from '../badges/types';
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-11-02 23:01:13 +00:00
import { getBadgeImageFileLocalPath } from '../badges/getBadgeImageFileLocalPath';
import { isBadgeVisible } from '../badges/isBadgeVisible';
2021-11-02 23:01:13 +00:00
import { BadgeImageTheme } from '../badges/BadgeImageTheme';
2021-11-09 15:51:56 +00:00
import { shouldShowBadges } from '../badges/shouldShowBadges';
2021-04-27 13:20:17 +00:00
export enum AvatarBlur {
NoBlur,
BlurPicture,
BlurPictureWithClickToView,
}
export enum AvatarSize {
2021-11-01 18:43:02 +00:00
SIXTEEN = 16,
TWENTY_EIGHT = 28,
THIRTY_TWO = 32,
THIRTY_SIX = 36,
FORTY_EIGHT = 48,
FIFTY_TWO = 52,
EIGHTY = 80,
NINETY_SIX = 96,
ONE_HUNDRED_TWELVE = 112,
}
type BadgePlacementType = { size: number; bottom: number; right: number };
2020-05-27 21:37:06 +00:00
export type Props = {
avatarPath?: string;
2021-11-02 23:01:13 +00:00
badge?: BadgeType;
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;
2021-11-02 23:01:13 +00:00
theme?: ThemeType;
title: string;
unblurredAvatarPath?: string;
2021-11-12 01:17:29 +00:00
searchResult?: boolean;
2019-10-17 18:22:07 +00:00
2021-10-12 19:07:58 +00:00
onClick?: (event: MouseEvent<HTMLButtonElement>) => unknown;
2019-10-17 18:22:07 +00:00
// 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 BADGE_PLACEMENT_BY_SIZE = new Map<number, BadgePlacementType>([
[28, { size: 16, bottom: -4, right: -2 }],
[32, { size: 16, bottom: -4, right: -2 }],
[36, { size: 16, bottom: -3, right: 0 }],
[40, { size: 24, bottom: -6, right: -4 }],
[48, { size: 24, bottom: -6, right: -4 }],
[56, { size: 24, bottom: -6, right: 0 }],
[80, { size: 36, bottom: -8, right: 0 }],
[88, { size: 36, bottom: -4, right: 3 }],
]);
const getDefaultBlur = (
...args: Parameters<typeof shouldBlurAvatar>
): AvatarBlur =>
shouldBlurAvatar(...args) ? AvatarBlur.BlurPicture : AvatarBlur.NoBlur;
export const Avatar: FunctionComponent<Props> = ({
acceptedMessageRequest,
avatarPath,
2021-11-02 23:01:13 +00:00
badge,
className,
color = 'A200',
conversationType,
i18n,
isMe,
innerRef,
loading,
noteToSelf,
onClick,
sharedGroupNames,
size,
2021-11-02 23:01:13 +00:00
theme,
title,
unblurredAvatarPath,
2021-11-12 01:17:29 +00:00
searchResult,
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);
2021-10-12 19:07:58 +00:00
let contentsChildren: ReactNode;
if (loading) {
const svgSize = size < 40 ? 'small' : 'normal';
2021-10-12 19:07:58 +00:00
contentsChildren = (
<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;
2021-10-12 19:07:58 +00:00
contentsChildren = (
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>
)}
</>
);
2021-11-12 01:17:29 +00:00
} else if (searchResult) {
contentsChildren = (
<div
className={classNames(
'module-Avatar__icon',
'module-Avatar__icon--search-result'
)}
/>
);
} else if (noteToSelf) {
2021-10-12 19:07:58 +00:00
contentsChildren = (
<div
className={classNames(
'module-Avatar__icon',
'module-Avatar__icon--note-to-self'
)}
/>
);
} else if (shouldUseInitials) {
2021-10-12 19:07:58 +00:00
contentsChildren = (
<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 {
2021-10-12 19:07:58 +00:00
contentsChildren = (
<div
className={classNames(
'module-Avatar__icon',
`module-Avatar__icon--${conversationType}`
)}
/>
);
}
2021-10-12 19:07:58 +00:00
let contents: ReactChild;
const contentsClassName = classNames(
'module-Avatar__contents',
`module-Avatar__contents--${color}`
);
if (onClick) {
contents = (
2021-10-12 19:07:58 +00:00
<button className={contentsClassName} type="button" onClick={onClick}>
{contentsChildren}
</button>
);
2021-10-12 19:07:58 +00:00
} else {
contents = <div className={contentsClassName}>{contentsChildren}</div>;
}
2021-11-02 23:01:13 +00:00
let badgeNode: ReactNode;
2021-11-09 15:51:56 +00:00
if (
badge &&
theme &&
!noteToSelf &&
isBadgeVisible(badge) &&
shouldShowBadges()
) {
const badgePlacement = _getBadgePlacement(size);
2021-11-02 23:01:13 +00:00
const badgeTheme =
theme === ThemeType.light ? BadgeImageTheme.Light : BadgeImageTheme.Dark;
const badgeImagePath = getBadgeImageFileLocalPath(
badge,
badgePlacement.size,
2021-11-02 23:01:13 +00:00
badgeTheme
);
if (badgeImagePath) {
badgeNode = (
<img
alt={badge.name}
className="module-Avatar__badge"
src={badgeImagePath}
style={{
width: badgePlacement.size,
height: badgePlacement.size,
bottom: badgePlacement.bottom,
right: badgePlacement.right,
2021-11-02 23:01:13 +00:00
}}
/>
);
}
} else if (badge && !theme) {
log.error('<Avatar> requires a theme if a badge is provided');
}
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',
className
)}
style={{
minWidth: size,
width: size,
height: size,
}}
ref={innerRef}
>
{contents}
2021-11-02 23:01:13 +00:00
{badgeNode}
</div>
);
};
// This is only exported for testing.
export function _getBadgePlacement(
avatarSize: number
): Readonly<BadgePlacementType> {
return (
BADGE_PLACEMENT_BY_SIZE.get(avatarSize) || {
size: Math.ceil(avatarSize * 0.425),
bottom: 0,
right: 0,
}
);
}