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

150 lines
5.3 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. constructor (props, context) {
  24. super(props, context);
  25. this.state = {
  26. isHovered: false
  27. };
  28. this.handleMouseOver = this.handleMouseOver.bind(this);
  29. this.handleMouseOut = this.handleMouseOut.bind(this);
  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
  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. Avatar.propTypes = {
  62. account: ImmutablePropTypes.map.isRequired,
  63. autoPlayGif: PropTypes.bool.isRequired
  64. };
  65. class Header extends ImmutablePureComponent {
  66. render () {
  67. const { account, me, intl } = this.props;
  68. if (!account) {
  69. return null;
  70. }
  71. let displayName = account.get('display_name');
  72. let info = '';
  73. let actionBtn = '';
  74. let lockedIcon = '';
  75. if (displayName.length === 0) {
  76. displayName = account.get('username');
  77. }
  78. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  79. info = <span className='account--follows-info' style={{ position: 'absolute', top: '10px', right: '10px', opacity: '0.7', display: 'inline-block', verticalAlign: 'top', background: 'rgba(0, 0, 0, 0.4)', textTransform: 'uppercase', fontSize: '11px', fontWeight: '500', padding: '4px', borderRadius: '4px' }}><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>
  80. }
  81. if (me !== account.get('id')) {
  82. if (account.getIn(['relationship', 'requested'])) {
  83. actionBtn = (
  84. <div style={{ position: 'absolute', top: '10px', left: '20px' }}>
  85. <IconButton size={26} disabled={true} icon='hourglass' title={intl.formatMessage(messages.requested)} />
  86. </div>
  87. );
  88. } else if (!account.getIn(['relationship', 'blocking'])) {
  89. actionBtn = (
  90. <div style={{ position: 'absolute', top: '10px', left: '20px' }}>
  91. <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} />
  92. </div>
  93. );
  94. }
  95. }
  96. if (account.get('locked')) {
  97. lockedIcon = <i className='fa fa-lock' />;
  98. }
  99. const content = { __html: emojify(account.get('note')) };
  100. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  101. return (
  102. <div className='account__header' style={{ backgroundImage: `url(${account.get('header')})` }}>
  103. <div style={{ padding: '20px 10px' }}>
  104. <Avatar account={account} autoPlayGif={this.props.autoPlayGif} />
  105. <span style={{ display: 'inline-block', fontSize: '20px', lineHeight: '27px', fontWeight: '500' }} className='account__header__display-name' dangerouslySetInnerHTML={displayNameHTML} />
  106. <span className='account__header__username' style={{ fontSize: '14px', fontWeight: '400', display: 'block', marginBottom: '10px' }}>@{account.get('acct')} {lockedIcon}</span>
  107. <div style={{ fontSize: '14px' }} className='account__header__content' dangerouslySetInnerHTML={content} />
  108. {info}
  109. {actionBtn}
  110. </div>
  111. </div>
  112. );
  113. }
  114. }
  115. Header.propTypes = {
  116. account: ImmutablePropTypes.map,
  117. me: PropTypes.number.isRequired,
  118. onFollow: PropTypes.func.isRequired,
  119. intl: PropTypes.object.isRequired,
  120. autoPlayGif: PropTypes.bool.isRequired
  121. };
  122. export default connect(makeMapStateToProps)(injectIntl(Header));