UserPicture.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 hash = md5(user.email.trim().toLowerCase());
  18. return `https://gravatar.com/avatar/${hash}`;
  19. }
  20. getClassName() {
  21. let className = ['picture', 'picture-rounded'];
  22. if (this.props.size) {
  23. className.push('picture-' + this.props.size);
  24. }
  25. return className.join(' ');
  26. }
  27. render() {
  28. const user = this.props.user;
  29. return (
  30. <img
  31. src={this.getUserPicture(user)}
  32. alt={user.username}
  33. className={this.getClassName()}
  34. />
  35. );
  36. }
  37. }
  38. UserPicture.propTypes = {
  39. user: PropTypes.object.isRequired,
  40. size: PropTypes.string,
  41. };
  42. UserPicture.defaultProps = {
  43. user: {},
  44. size: null,
  45. };