import React from 'react'; import classNames from 'classnames'; import { getInitials } from '../util/getInitials'; import { LocalizerType } from '../types/Util'; interface Props { avatarPath?: string; color?: string; conversationType: 'group' | 'direct'; i18n: LocalizerType; noteToSelf?: boolean; name?: string; phoneNumber?: string; profileName?: string; size: number; } interface State { imageBroken: boolean; } export class Avatar extends React.Component { public handleImageErrorBound: () => void; public constructor(props: Props) { super(props); this.handleImageErrorBound = this.handleImageError.bind(this); this.state = { imageBroken: false, }; } public handleImageError() { // tslint:disable-next-line no-console console.log('Avatar: Image failed to load; failing over to placeholder'); this.setState({ imageBroken: true, }); } public renderImage() { const { avatarPath, i18n, name, phoneNumber, profileName } = this.props; const { imageBroken } = this.state; if (!avatarPath || imageBroken) { return null; } const title = `${name || phoneNumber}${ !name && profileName ? ` ~${profileName}` : '' }`; return ( {i18n('contactAvatarAlt', ); } public renderNoImage() { const { conversationType, name, noteToSelf, size } = this.props; const initials = getInitials(name); const isGroup = conversationType === 'group'; if (noteToSelf) { return (
); } if (!isGroup && initials) { return (
{initials}
); } return (
); } public render() { const { avatarPath, color, size, noteToSelf } = this.props; const { imageBroken } = this.state; const hasImage = !noteToSelf && avatarPath && !imageBroken; if (size !== 28 && size !== 36 && size !== 48 && size !== 80) { throw new Error(`Size ${size} is not supported!`); } return (
{hasImage ? this.renderImage() : this.renderNoImage()}
); } }