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.

143 lines
4.6 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import emojify from '../../../emoji';
  5. import escapeTextContentForBrowser from 'escape-html';
  6. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  7. import IconButton from '../../../components/icon_button';
  8. import { Motion, spring } from 'react-motion';
  9. import { connect } from 'react-redux';
  10. import ImmutablePureComponent from 'react-immutable-pure-component';
  11. const messages = defineMessages({
  12. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  13. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  14. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval' }
  15. });
  16. const makeMapStateToProps = () => {
  17. const mapStateToProps = (state, props) => ({
  18. autoPlayGif: state.getIn(['meta', 'auto_play_gif'])
  19. });
  20. return mapStateToProps;
  21. };
  22. class Avatar extends ImmutablePureComponent {
  23. static propTypes = {
  24. account: ImmutablePropTypes.map.isRequired,
  25. autoPlayGif: PropTypes.bool.isRequired
  26. };
  27. state = {
  28. isHovered: false
  29. };
  30. handleMouseOver = () => {
  31. if (this.state.isHovered) return;
  32. this.setState({ isHovered: true });
  33. }
  34. handleMouseOut = () => {
  35. if (!this.state.isHovered) return;
  36. this.setState({ isHovered: false });
  37. }
  38. render () {
  39. const { account, autoPlayGif } = this.props;
  40. const { isHovered } = this.state;
  41. return (
  42. <Motion defaultStyle={{ radius: 90 }} style={{ radius: spring(isHovered ? 30 : 90, { stiffness: 180, damping: 12 }) }}>
  43. {({ radius }) =>
  44. <a
  45. href={account.get('url')}
  46. className='account__header__avatar'
  47. target='_blank'
  48. rel='noopener'
  49. style={{ borderRadius: `${radius}px`, backgroundImage: `url(${autoPlayGif || isHovered ? account.get('avatar') : account.get('avatar_static')})` }}
  50. onMouseOver={this.handleMouseOver}
  51. onMouseOut={this.handleMouseOut}
  52. onFocus={this.handleMouseOver}
  53. onBlur={this.handleMouseOut}
  54. />
  55. }
  56. </Motion>
  57. );
  58. }
  59. }
  60. class Header extends ImmutablePureComponent {
  61. static propTypes = {
  62. account: ImmutablePropTypes.map,
  63. me: PropTypes.number.isRequired,
  64. onFollow: PropTypes.func.isRequired,
  65. intl: PropTypes.object.isRequired,
  66. autoPlayGif: PropTypes.bool.isRequired
  67. };
  68. render () {
  69. const { account, me, intl } = this.props;
  70. if (!account) {
  71. return null;
  72. }
  73. let displayName = account.get('display_name');
  74. let info = '';
  75. let actionBtn = '';
  76. let lockedIcon = '';
  77. if (displayName.length === 0) {
  78. displayName = account.get('username');
  79. }
  80. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  81. info = <span className='account--follows-info'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>
  82. }
  83. if (me !== account.get('id')) {
  84. if (account.getIn(['relationship', 'requested'])) {
  85. actionBtn = (
  86. <div className='account--action-button'>
  87. <IconButton size={26} disabled={true} icon='hourglass' title={intl.formatMessage(messages.requested)} />
  88. </div>
  89. );
  90. } else if (!account.getIn(['relationship', 'blocking'])) {
  91. actionBtn = (
  92. <div className='account--action-button'>
  93. <IconButton size={26} icon={account.getIn(['relationship', 'following']) ? 'user-times' : 'user-plus'} active={account.getIn(['relationship', 'following'])} title={intl.formatMessage(account.getIn(['relationship', 'following']) ? messages.unfollow : messages.follow)} onClick={this.props.onFollow} />
  94. </div>
  95. );
  96. }
  97. }
  98. if (account.get('locked')) {
  99. lockedIcon = <i className='fa fa-lock' />;
  100. }
  101. const content = { __html: emojify(account.get('note')) };
  102. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  103. return (
  104. <div className='account__header' style={{ backgroundImage: `url(${account.get('header')})` }}>
  105. <div>
  106. <Avatar account={account} autoPlayGif={this.props.autoPlayGif} />
  107. <span className='account__header__display-name' dangerouslySetInnerHTML={displayNameHTML} />
  108. <span className='account__header__username'>@{account.get('acct')} {lockedIcon}</span>
  109. <div className='account__header__content' dangerouslySetInnerHTML={content} />
  110. {info}
  111. {actionBtn}
  112. </div>
  113. </div>
  114. );
  115. }
  116. }
  117. export default connect(makeMapStateToProps)(injectIntl(Header));