| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import React from 'react';
- import md5 from 'md5';
- import PropTypes from 'prop-types';
- const DEFAULT_IMAGE = '/images/icons/user.svg';
- // TODO UserComponent?
- export default class UserPicture extends React.Component {
- getUserPicture(user) {
- // gravatar
- if (user.isGravatarEnabled === true) {
- return this.generateGravatarSrc(user);
- }
- // uploaded image
- if (user.image != null) {
- return user.image;
- }
- if (user.imageAttachment != null) {
- return user.imageAttachment.filePathProxied;
- }
- return DEFAULT_IMAGE;
- }
- generateGravatarSrc(user) {
- const email = user.email || '';
- const hash = md5(email.trim().toLowerCase());
- return `https://gravatar.com/avatar/${hash}`;
- }
- getClassName() {
- const className = ['img-circle', 'picture'];
- // size
- if (this.props.size) {
- className.push(`picture-${this.props.size}`);
- }
- return className.join(' ');
- }
- renderForNull() {
- return (
- <img
- src={DEFAULT_IMAGE}
- alt="someone"
- className={this.getClassName()}
- />
- );
- }
- render() {
- const user = this.props.user;
- if (user == null) {
- return this.renderForNull();
- }
- const imgElem = (
- <img
- src={this.getUserPicture(user)}
- alt={user.username}
- className={this.getClassName()}
- />
- );
- return (
- (this.props.withoutLink)
- ? <span>{imgElem}</span>
- : <a href={`/user/${user.username}`}>{imgElem}</a>
- );
- }
- }
- UserPicture.propTypes = {
- user: PropTypes.object,
- size: PropTypes.string,
- withoutLink: PropTypes.bool,
- };
- UserPicture.defaultProps = {
- size: null,
- };
|