You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import BotIcon from './bot_icon';
  5. import { autoPlayGif } from '../initial_state';
  6. export default class Avatar extends React.PureComponent {
  7. static propTypes = {
  8. account: ImmutablePropTypes.map.isRequired,
  9. size: PropTypes.number.isRequired,
  10. style: PropTypes.object,
  11. inline: PropTypes.bool,
  12. animate: PropTypes.bool,
  13. };
  14. static defaultProps = {
  15. animate: autoPlayGif,
  16. size: 20,
  17. inline: false,
  18. };
  19. state = {
  20. hovering: false,
  21. };
  22. handleMouseEnter = () => {
  23. if (this.props.animate) return;
  24. this.setState({ hovering: true });
  25. }
  26. handleMouseLeave = () => {
  27. if (this.props.animate) return;
  28. this.setState({ hovering: false });
  29. }
  30. render () {
  31. const { account, size, animate, inline } = this.props;
  32. const { hovering } = this.state;
  33. const src = account.get('avatar');
  34. const staticSrc = account.get('avatar_static');
  35. let className = 'account__avatar';
  36. if (inline) {
  37. className = className + ' account__avatar-inline';
  38. }
  39. const style = {
  40. ...this.props.style,
  41. width: `${size}px`,
  42. height: `${size}px`,
  43. backgroundSize: `${size}px ${size}px`,
  44. };
  45. if (hovering || animate) {
  46. style.backgroundImage = `url(${src})`;
  47. } else {
  48. style.backgroundImage = `url(${staticSrc})`;
  49. }
  50. return (
  51. <div
  52. className={className}
  53. onMouseEnter={this.handleMouseEnter}
  54. onMouseLeave={this.handleMouseLeave}
  55. style={style}
  56. >
  57. <BotIcon account={account} />
  58. </div>
  59. );
  60. }
  61. }