闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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