UserPicture.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/user.svg';
  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 = ['img-circle', 'picture'];
  23. // size
  24. if (this.props.size) {
  25. className.push('picture-' + this.props.size);
  26. }
  27. return className.join(' ');
  28. }
  29. render() {
  30. const user = this.props.user;
  31. return (
  32. <img
  33. src={this.getUserPicture(user)}
  34. alt={user.username}
  35. className={this.getClassName()}
  36. />
  37. );
  38. }
  39. }
  40. UserPicture.propTypes = {
  41. user: PropTypes.object.isRequired,
  42. size: PropTypes.string,
  43. };
  44. UserPicture.defaultProps = {
  45. user: {},
  46. size: null,
  47. };