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';
|
2018-09-27 00:23:17 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import { getInitials } from '../util/getInitials';
|
2020-08-13 20:53:45 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
|
|
|
import { ColorType } from '../types/Colors';
|
2018-09-27 00:23:17 +00:00
|
|
|
|
2020-11-19 18:13:36 +00:00
|
|
|
export enum AvatarSize {
|
|
|
|
TWENTY_EIGHT = 28,
|
|
|
|
THIRTY_TWO = 32,
|
|
|
|
FIFTY_TWO = 52,
|
|
|
|
EIGHTY = 80,
|
|
|
|
NINETY_SIX = 96,
|
|
|
|
ONE_HUNDRED_TWELVE = 112,
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
export type Props = {
|
2018-09-27 00:23:17 +00:00
|
|
|
avatarPath?: string;
|
2020-03-26 21:47:35 +00:00
|
|
|
color?: ColorType;
|
|
|
|
|
2018-09-27 00:23:17 +00:00
|
|
|
conversationType: 'group' | 'direct';
|
2019-01-31 01:45:58 +00:00
|
|
|
noteToSelf?: boolean;
|
2020-07-24 01:35:32 +00:00
|
|
|
title: string;
|
2018-09-27 00:23:17 +00:00
|
|
|
name?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
profileName?: string;
|
2020-11-19 18:13:36 +00:00
|
|
|
size: AvatarSize;
|
2019-10-17 18:22:07 +00:00
|
|
|
|
|
|
|
onClick?: () => unknown;
|
|
|
|
|
|
|
|
// Matches Popper's RefHandler type
|
2020-01-07 01:49:00 +00:00
|
|
|
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'>;
|
2018-09-27 00:23:17 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
imageBroken: boolean;
|
2019-12-03 20:02:50 +00:00
|
|
|
lastAvatarPath?: string;
|
2018-09-27 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2019-12-03 20:02:50 +00:00
|
|
|
lastAvatarPath: props.avatarPath,
|
2018-09-27 00:23:17 +00:00
|
|
|
imageBroken: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
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'
|
|
|
|
);
|
2018-09-27 00:23:17 +00:00
|
|
|
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;
|
2018-09-27 00:23:17 +00:00
|
|
|
const { imageBroken } = this.state;
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
if (!avatarPath || imageBroken) {
|
2018-09-27 00:23:17 +00:00
|
|
|
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);
|
2018-09-27 00:23:17 +00:00
|
|
|
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}`
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:23:17 +00:00
|
|
|
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;
|
2018-09-27 00:23:17 +00:00
|
|
|
const { imageBroken } = this.state;
|
|
|
|
|
2019-01-31 01:45:58 +00:00
|
|
|
const hasImage = !noteToSelf && avatarPath && !imageBroken;
|
2018-09-27 00:23:17 +00:00
|
|
|
|
2020-11-11 17:36:05 +00:00
|
|
|
if (![28, 32, 52, 80, 96, 112].includes(size)) {
|
2018-09-27 00:23:17 +00:00
|
|
|
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
|
|
|
|
2018-09-27 00:23:17 +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
|
2018-09-27 00:23:17 +00:00
|
|
|
)}
|
2019-10-17 18:22:07 +00:00
|
|
|
ref={innerRef}
|
2018-09-27 00:23:17 +00:00
|
|
|
>
|
2019-11-07 21:36:16 +00:00
|
|
|
{contents}
|
2018-09-27 00:23:17 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|