avatar.js 886 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* jshint ignore:start */
  2. import React from 'react';
  3. import misago from 'misago';
  4. export default function(props) {
  5. const size = props.size || 100;
  6. const size2x = props.size2x || size;
  7. return (
  8. <img
  9. alt=''
  10. className={props.className || 'user-avatar'}
  11. src={getSrc(props.user, size)}
  12. srcSet={getSrc(props.user, size2x)}
  13. width={size}
  14. height={size}
  15. />
  16. );
  17. }
  18. export function getSrc(user, size) {
  19. if (user && user.id) {
  20. // just avatar hash, size and user id
  21. return resolveAvatarForSize(user.avatars, size).url;
  22. } else {
  23. // just append avatar size to file to produce no-avatar placeholder
  24. return misago.get('BLANK_AVATAR_URL');
  25. }
  26. }
  27. export function resolveAvatarForSize(avatars, size) {
  28. let avatar = avatars[0];
  29. avatars.forEach((av) => {
  30. if (av.size >= size) {
  31. avatar = av;
  32. }
  33. });
  34. return avatar;
  35. }