avatar.js 848 B

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