import * as React from 'react'; import IReactComponentProps from '../../../common/structures/IReactComponentProps'; import styles from './Avatar.sass'; import classnames from 'classnames'; import ImageCircle from '../ImageCircle/ImageCircle'; import ClippedContent from '../../modules/ClippedContent/ClippedContent'; interface IProps extends IReactComponentProps { color?: 'blue' | 'green' | 'yellow' | 'orange' | 'red' | 'pink' | 'purple'; initials?: string; placeholderSrc?: string; size?: 's' | 'm' | 'l' | string; src?: string; type: 'user' | 'team'; } interface IState { isImageError: boolean; isImageLoaded: boolean; } export default class Avatar extends React.Component { static defaultProps: Partial = { color: 'blue', size: 'm', }; constructor (props: IProps) { super(props); this.state = { isImageError: false, isImageLoaded: false, }; this.onError = this.onError.bind(this); this.onLoad = this.onLoad.bind(this); } UNSAFE_componentWillReceiveProps (nextProps: IProps) { if (this.props.src !== nextProps.src) { this.setState({ isImageError: false, isImageLoaded: false, }); } } onError () { this.setState({ isImageError: true, }); } onLoad () { this.setState({ isImageLoaded: true, }); } getSize (): string { let size: string = this.props.size || Avatar.defaultProps.size as string; switch (size) { case 'l': size = '45px'; break; case 'm': size = '40px'; break; case 's': size = '32px'; break; } return size; } render () { const size: string = this.getSize(); return (
{ this.props.initials && (
{this.props.initials.trim().substring(0, 2)}
) } { this.props.placeholderSrc && !this.state.isImageLoaded && ( ) } { this.props.src && ( ) }
); } }