闭社主体 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.

140 lines
4.5 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. import IconButton from '../../../components/icon_button';
  6. import Motion from 'react-motion/lib/Motion';
  7. import spring from 'react-motion/lib/spring';
  8. import { connect } from 'react-redux';
  9. import ImmutablePureComponent from 'react-immutable-pure-component';
  10. const messages = defineMessages({
  11. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  12. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  13. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' },
  14. });
  15. const makeMapStateToProps = () => {
  16. const mapStateToProps = state => ({
  17. autoPlayGif: state.getIn(['meta', 'auto_play_gif']),
  18. });
  19. return mapStateToProps;
  20. };
  21. class Avatar extends ImmutablePureComponent {
  22. static propTypes = {
  23. account: ImmutablePropTypes.map.isRequired,
  24. autoPlayGif: PropTypes.bool.isRequired,
  25. };
  26. state = {
  27. isHovered: false,
  28. };
  29. handleMouseOver = () => {
  30. if (this.state.isHovered) return;
  31. this.setState({ isHovered: true });
  32. }
  33. handleMouseOut = () => {
  34. if (!this.state.isHovered) return;
  35. this.setState({ isHovered: false });
  36. }
  37. render () {
  38. const { account, autoPlayGif } = this.props;
  39. const { isHovered } = this.state;
  40. return (
  41. <Motion defaultStyle={{ radius: 90 }} style={{ radius: spring(isHovered ? 30 : 90, { stiffness: 180, damping: 12 }) }}>
  42. {({ radius }) =>
  43. <a
  44. href={account.get('url')}
  45. className='account__header__avatar'
  46. role='presentation'
  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. <span style={{ display: 'none' }}>{account.get('acct')}</span>
  56. </a>
  57. }
  58. </Motion>
  59. );
  60. }
  61. }
  62. @connect(makeMapStateToProps)
  63. @injectIntl
  64. export default class Header extends ImmutablePureComponent {
  65. static propTypes = {
  66. account: ImmutablePropTypes.map,
  67. me: PropTypes.number.isRequired,
  68. onFollow: PropTypes.func.isRequired,
  69. intl: PropTypes.object.isRequired,
  70. autoPlayGif: PropTypes.bool.isRequired,
  71. };
  72. render () {
  73. const { account, me, intl } = this.props;
  74. if (!account) {
  75. return null;
  76. }
  77. let info = '';
  78. let actionBtn = '';
  79. let lockedIcon = '';
  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} active icon='hourglass' title={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />
  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: account.get('note_emojified') };
  102. const displayNameHtml = { __html: account.get('display_name_html') };
  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. }