UserPicture.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createSubscribedElement } from '../UnstatedUtils';
  4. import AppContainer from '../../services/AppContainer';
  5. const DEFAULT_IMAGE = '/images/icons/user.svg';
  6. // TODO UserComponent?
  7. class UserPicture extends React.Component {
  8. getClassName() {
  9. const className = ['img-circle', 'picture'];
  10. // size
  11. if (this.props.size) {
  12. className.push(`picture-${this.props.size}`);
  13. }
  14. return className.join(' ');
  15. }
  16. renderForNull() {
  17. return (
  18. <img
  19. src={DEFAULT_IMAGE}
  20. alt="someone"
  21. className={this.getClassName()}
  22. />
  23. );
  24. }
  25. render() {
  26. const { user, appContainer } = this.props;
  27. if (user == null) {
  28. return this.renderForNull();
  29. }
  30. if (!user.imageUrlCached) {
  31. appContainer.willUpdateImageUrlCacheUserIds.push(user._id);
  32. return this.renderForNull();
  33. }
  34. const imgElem = (
  35. <img
  36. src={user.imageUrlCached}
  37. alt={user.username}
  38. className={this.getClassName()}
  39. />
  40. );
  41. return (
  42. (this.props.withoutLink)
  43. ? <span>{imgElem}</span>
  44. : <a href={`/user/${user.username}`}>{imgElem}</a>
  45. );
  46. }
  47. }
  48. UserPicture.propTypes = {
  49. user: PropTypes.object,
  50. size: PropTypes.string,
  51. withoutLink: PropTypes.bool,
  52. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  53. };
  54. UserPicture.defaultProps = {
  55. size: null,
  56. };
  57. const UserPictureWrapper = (props) => {
  58. return createSubscribedElement(UserPicture, props, [AppContainer]);
  59. };
  60. export default UserPictureWrapper;