avatar.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import classNames from 'classnames';
  2. import type { Account } from 'mastodon/models/account';
  3. import { useHovering } from '../../hooks/useHovering';
  4. import { autoPlayGif } from '../initial_state';
  5. interface Props {
  6. account: Account | undefined; // FIXME: remove `undefined` once we know for sure its always there
  7. size: number;
  8. style?: React.CSSProperties;
  9. inline?: boolean;
  10. animate?: boolean;
  11. }
  12. export const Avatar: React.FC<Props> = ({
  13. account,
  14. animate = autoPlayGif,
  15. size = 20,
  16. inline = false,
  17. style: styleFromParent,
  18. }) => {
  19. const { hovering, handleMouseEnter, handleMouseLeave } = useHovering(animate);
  20. const style = {
  21. ...styleFromParent,
  22. width: `${size}px`,
  23. height: `${size}px`,
  24. };
  25. const src =
  26. hovering || animate
  27. ? account?.get('avatar')
  28. : account?.get('avatar_static');
  29. return (
  30. <div
  31. className={classNames('account__avatar', {
  32. 'account__avatar-inline': inline,
  33. })}
  34. onMouseEnter={handleMouseEnter}
  35. onMouseLeave={handleMouseLeave}
  36. style={style}
  37. >
  38. {src && <img src={src} alt='' />}
  39. </div>
  40. );
  41. };