import React from 'react';
import md5 from 'md5';
import PropTypes from 'prop-types';
import { userPageRoot } from '@commons/util/path-utils';
import { UncontrolledTooltip } from 'reactstrap';
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 = ['rounded-circle', 'picture'];
// size
if (this.props.size) {
className.push(`picture-${this.props.size}`);
}
return className.join(' ');
}
renderForNull() {
return (
);
}
RootElmWithoutLink = (props) => {
return {props.children};
}
RootElmWithLink = (props) => {
const { user } = this.props;
const href = userPageRoot(user);
return {props.children};
}
withTooltip = (RootElm) => {
const { user } = this.props;
const id = `user-picture-${Math.random().toString(32).substring(2)}`;
return props => (
<>
{props.children}
@{user.username}
{user.name}
>
);
}
render() {
const user = this.props.user;
if (user == null) {
return this.renderForNull();
}
const { noLink, noTooltip } = this.props;
// determine RootElm
let RootElm = noLink ? this.RootElmWithoutLink : this.RootElmWithLink;
if (!noTooltip) {
RootElm = this.withTooltip(RootElm);
}
return (
);
}
}
UserPicture.propTypes = {
user: PropTypes.object,
size: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
noLink: PropTypes.bool,
noTooltip: PropTypes.bool,
};
UserPicture.defaultProps = {
size: null,
noLink: false,
noTooltip: false,
};