import React from 'react'; import classNames from 'classnames'; import { AvatarPlaceHolder, ClosedGroupAvatar } from './AvatarPlaceHolder'; import { ConversationAttributes } from '../../js/models/conversations'; interface Props { avatarPath?: string; name?: string; // display name, profileName or phoneNumber, whatever is set first pubkey?: string; size: number; closedMemberConversations?: Array; onAvatarClick?: () => void; } interface State { imageBroken: boolean; } export class Avatar extends React.PureComponent { public handleImageErrorBound: () => void; public onAvatarClickBound: (e: any) => void; public constructor(props: Props) { super(props); this.handleImageErrorBound = this.handleImageError.bind(this); this.onAvatarClickBound = this.onAvatarClick.bind(this); this.state = { imageBroken: false, }; } public handleImageError() { window.log.warn( 'Avatar: Image failed to load; failing over to placeholder' ); this.setState({ imageBroken: true, }); } public renderIdenticon() { const { size, name, pubkey } = this.props; const userName = name || '0'; return ( ); } public renderImage() { const { avatarPath, name } = this.props; const { imageBroken } = this.state; if (!avatarPath || imageBroken) { return null; } return ( {window.i18n('contactAvatarAlt', ); } public renderNoImage() { const { closedMemberConversations, size } = this.props; // if no image but we have conversations set for the group, renders group members avatars if (closedMemberConversations) { return ( ); } return this.renderIdenticon(); } public render() { const { avatarPath, size } = this.props; const { imageBroken } = this.state; const hasImage = avatarPath && !imageBroken; if ( size !== 28 && size !== 36 && size !== 48 && size !== 64 && size !== 80 && size !== 300 ) { throw new Error(`Size ${size} is not supported!`); } return (
{ this.onAvatarClickBound(e); }} role="button" > {hasImage ? this.renderImage() : this.renderNoImage()}
); } private onAvatarClick(e: any) { if (this.props.onAvatarClick) { e.stopPropagation(); this.props.onAvatarClick(); } } private getAvatarColors(): Array { // const theme = window.Events.getThemedSettings(); // defined in session-android as `profile_picture_placeholder_colors` return ['#5ff8b0', '#26cdb9', '#f3c615', '#fcac5a']; } private getAvatarBorderColor(): string { return '#00000059'; // borderAvatarColor in themes.scss } }