signal-desktop/ts/components/Avatar.tsx

180 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-05-27 21:37:06 +00:00
import * as React from 'react';
import classNames from 'classnames';
import { getInitials } from '../util/getInitials';
import { LocalizerType } from '../types/Util';
import { ColorType } from '../types/Colors';
2020-05-27 21:37:06 +00:00
export type Props = {
avatarPath?: string;
color?: ColorType;
conversationType: 'group' | 'direct';
2019-01-31 01:45:58 +00:00
noteToSelf?: boolean;
2020-07-24 01:35:32 +00:00
title: string;
name?: string;
phoneNumber?: string;
profileName?: string;
size: 28 | 32 | 52 | 80 | 96 | 112;
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'>;
interface State {
imageBroken: boolean;
lastAvatarPath?: string;
}
export class Avatar extends React.Component<Props, State> {
public handleImageErrorBound: () => void;
public constructor(props: Props) {
super(props);
this.handleImageErrorBound = this.handleImageError.bind(this);
this.state = {
lastAvatarPath: props.avatarPath,
imageBroken: false,
};
}
public static getDerivedStateFromProps(props: Props, state: State): State {
if (props.avatarPath !== state.lastAvatarPath) {
return {
...state,
lastAvatarPath: props.avatarPath,
imageBroken: false,
};
}
return state;
}
2020-09-12 00:46:52 +00:00
public handleImageError(): void {
window.log.info(
'Avatar: Image failed to load; failing over to placeholder'
);
this.setState({
imageBroken: true,
});
}
2020-09-12 00:46:52 +00:00
public renderImage(): JSX.Element | null {
2020-07-24 01:35:32 +00:00
const { avatarPath, i18n, title } = this.props;
const { imageBroken } = this.state;
2019-01-14 21:49:58 +00:00
if (!avatarPath || imageBroken) {
return null;
}
return (
<img
onError={this.handleImageErrorBound}
alt={i18n('contactAvatarAlt', [title])}
src={avatarPath}
/>
);
}
2020-09-12 00:46:52 +00:00
public renderNoImage(): JSX.Element {
2020-11-20 17:30:45 +00:00
const { conversationType, noteToSelf, size, title } = this.props;
2019-10-17 18:22:07 +00:00
2020-11-20 17:30:45 +00:00
const initials = getInitials(title);
const isGroup = conversationType === 'group';
2019-01-31 01:45:58 +00:00
if (noteToSelf) {
return (
<div
className={classNames(
'module-avatar__icon',
'module-avatar__icon--note-to-self',
`module-avatar__icon--${size}`
)}
/>
);
}
if (!isGroup && initials) {
return (
<div
className={classNames(
'module-avatar__label',
`module-avatar__label--${size}`
)}
>
{initials}
</div>
);
}
return (
<div
className={classNames(
'module-avatar__icon',
`module-avatar__icon--${conversationType}`,
`module-avatar__icon--${size}`
)}
/>
);
}
2020-09-12 00:46:52 +00:00
public render(): JSX.Element {
2019-10-17 18:22:07 +00:00
const {
avatarPath,
color,
innerRef,
noteToSelf,
onClick,
size,
2020-05-27 21:37:06 +00:00
className,
2019-10-17 18:22:07 +00:00
} = this.props;
const { imageBroken } = this.state;
2019-01-31 01:45:58 +00:00
const hasImage = !noteToSelf && avatarPath && !imageBroken;
if (![28, 32, 52, 80, 96, 112].includes(size)) {
throw new Error(`Size ${size} is not supported!`);
}
2019-11-07 21:36:16 +00:00
let contents;
if (onClick) {
contents = (
2020-09-12 00:46:52 +00:00
<button
type="button"
className="module-avatar-button"
onClick={onClick}
>
2019-11-07 21:36:16 +00:00
{hasImage ? this.renderImage() : this.renderNoImage()}
</button>
);
} else {
contents = hasImage ? this.renderImage() : this.renderNoImage();
}
2019-10-17 18:22:07 +00:00
return (
<div
className={classNames(
'module-avatar',
`module-avatar--${size}`,
hasImage ? 'module-avatar--with-image' : 'module-avatar--no-image',
2020-05-27 21:37:06 +00:00
!hasImage ? `module-avatar--${color}` : null,
className
)}
2019-10-17 18:22:07 +00:00
ref={innerRef}
>
2019-11-07 21:36:16 +00:00
{contents}
</div>
);
}
}