The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

145 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. class Header extends ImmutablePureComponent {
  62. static propTypes = {
  63. account: ImmutablePropTypes.map,
  64. me: PropTypes.number.isRequired,
  65. onFollow: PropTypes.func.isRequired,
  66. intl: PropTypes.object.isRequired,
  67. autoPlayGif: PropTypes.bool.isRequired,
  68. };
  69. render () {
  70. const { account, me, intl } = this.props;
  71. if (!account) {
  72. return null;
  73. }
  74. let displayName = account.get('display_name');
  75. let info = '';
  76. let actionBtn = '';
  77. let lockedIcon = '';
  78. if (displayName.length === 0) {
  79. displayName = account.get('username');
  80. }
  81. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  82. info = <span className='account--follows-info'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>;
  83. }
  84. if (me !== account.get('id')) {
  85. if (account.getIn(['relationship', 'requested'])) {
  86. actionBtn = (
  87. <div className='account--action-button'>
  88. <IconButton size={26} disabled icon='hourglass' title={intl.formatMessage(messages.requested)} />
  89. </div>
  90. );
  91. } else if (!account.getIn(['relationship', 'blocking'])) {
  92. actionBtn = (
  93. <div className='account--action-button'>
  94. <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} />
  95. </div>
  96. );
  97. }
  98. }
  99. if (account.get('locked')) {
  100. lockedIcon = <i className='fa fa-lock' />;
  101. }
  102. const content = { __html: emojify(account.get('note')) };
  103. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  104. return (
  105. <div className='account__header' style={{ backgroundImage: `url(${account.get('header')})` }}>
  106. <div>
  107. <Avatar account={account} autoPlayGif={this.props.autoPlayGif} />
  108. <span className='account__header__display-name' dangerouslySetInnerHTML={displayNameHTML} />
  109. <span className='account__header__username'>@{account.get('acct')} {lockedIcon}</span>
  110. <div className='account__header__content' dangerouslySetInnerHTML={content} />
  111. {info}
  112. {actionBtn}
  113. </div>
  114. </div>
  115. );
  116. }
  117. }
  118. export default connect(makeMapStateToProps)(injectIntl(Header));