UserPicture.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React from 'react';
  2. import md5 from 'md5';
  3. import PropTypes from 'prop-types';
  4. // TODO UserComponent?
  5. export default class UserPicture extends React.Component {
  6. getUserPicture(user) {
  7. // gravatar
  8. if (user.isGravatarEnabled === true) {
  9. return this.generateGravatarSrc(user);
  10. }
  11. // uploaded image
  12. else {
  13. return user.image || '/images/userpicture.png';
  14. }
  15. }
  16. generateGravatarSrc(user) {
  17. const email = user.email || '';
  18. const hash = md5(email.trim().toLowerCase());
  19. return `https://gravatar.com/avatar/${hash}`;
  20. }
  21. getClassName() {
  22. let className = ['picture', 'picture-rounded'];
  23. if (this.props.size) {
  24. className.push('picture-' + this.props.size);
  25. }
  26. return className.join(' ');
  27. }
  28. render() {
  29. const user = this.props.user;
  30. return (
  31. <img
  32. src={this.getUserPicture(user)}
  33. alt={user.username}
  34. className={this.getClassName()}
  35. />
  36. );
  37. }
  38. }
  39. UserPicture.propTypes = {
  40. user: PropTypes.object.isRequired,
  41. size: PropTypes.string,
  42. };
  43. UserPicture.defaultProps = {
  44. user: {},
  45. size: null,
  46. };