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.

193 lines
7.3 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 '../../ui/util/optional_motion';
  7. import spring from 'react-motion/lib/spring';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import { autoPlayGif, me } from '../../../initial_state';
  10. import classNames from 'classnames';
  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. Click to cancel follow request' },
  15. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  16. edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
  17. linkVerifiedOn: { id: 'account.link_verified_on', defaultMessage: 'Ownership of this link was checked on {date}' },
  18. });
  19. const dateFormatOptions = {
  20. month: 'short',
  21. day: 'numeric',
  22. year: 'numeric',
  23. hour12: false,
  24. hour: '2-digit',
  25. minute: '2-digit',
  26. };
  27. class Avatar extends ImmutablePureComponent {
  28. static propTypes = {
  29. account: ImmutablePropTypes.map.isRequired,
  30. };
  31. state = {
  32. isHovered: false,
  33. };
  34. handleMouseOver = () => {
  35. if (this.state.isHovered) return;
  36. this.setState({ isHovered: true });
  37. }
  38. handleMouseOut = () => {
  39. if (!this.state.isHovered) return;
  40. this.setState({ isHovered: false });
  41. }
  42. render () {
  43. const { account } = this.props;
  44. const { isHovered } = this.state;
  45. return (
  46. <Motion defaultStyle={{ radius: 90 }} style={{ radius: spring(isHovered ? 30 : 90, { stiffness: 180, damping: 12 }) }}>
  47. {({ radius }) => (
  48. <a
  49. href={account.get('url')}
  50. className='account__header__avatar'
  51. role='presentation'
  52. target='_blank'
  53. rel='noopener'
  54. style={{ borderRadius: `${radius}px`, backgroundImage: `url(${autoPlayGif || isHovered ? account.get('avatar') : account.get('avatar_static')})` }}
  55. onMouseOver={this.handleMouseOver}
  56. onMouseOut={this.handleMouseOut}
  57. onFocus={this.handleMouseOver}
  58. onBlur={this.handleMouseOut}
  59. >
  60. <span style={{ display: 'none' }}>{account.get('acct')}</span>
  61. </a>
  62. )}
  63. </Motion>
  64. );
  65. }
  66. }
  67. export default @injectIntl
  68. class Header extends ImmutablePureComponent {
  69. static propTypes = {
  70. account: ImmutablePropTypes.map,
  71. onFollow: PropTypes.func.isRequired,
  72. onBlock: PropTypes.func.isRequired,
  73. intl: PropTypes.object.isRequired,
  74. };
  75. openEditProfile = () => {
  76. window.open('/settings/profile', '_blank');
  77. }
  78. render () {
  79. const { account, intl } = this.props;
  80. if (!account) {
  81. return null;
  82. }
  83. let info = '';
  84. let mutingInfo = '';
  85. let actionBtn = '';
  86. let lockedIcon = '';
  87. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  88. info = <span className='account--follows-info'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>;
  89. } else if (me !== account.get('id') && account.getIn(['relationship', 'blocking'])) {
  90. info = <span className='account--follows-info'><FormattedMessage id='account.blocked' defaultMessage='Blocked' /></span>;
  91. }
  92. if (me !== account.get('id') && account.getIn(['relationship', 'muting'])) {
  93. mutingInfo = <span className='account--muting-info'><FormattedMessage id='account.muted' defaultMessage='Muted' /></span>;
  94. } else if (me !== account.get('id') && account.getIn(['relationship', 'domain_blocking'])) {
  95. mutingInfo = <span className='account--muting-info'><FormattedMessage id='account.domain_blocked' defaultMessage='Domain hidden' /></span>;
  96. }
  97. if (me !== account.get('id')) {
  98. if (!account.get('relationship')) { // Wait until the relationship is loaded
  99. actionBtn = '';
  100. } else if (account.getIn(['relationship', 'requested'])) {
  101. actionBtn = (
  102. <div className='account--action-button'>
  103. <IconButton size={26} active icon='hourglass' title={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />
  104. </div>
  105. );
  106. } else if (!account.getIn(['relationship', 'blocking'])) {
  107. actionBtn = (
  108. <div className='account--action-button'>
  109. <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} />
  110. </div>
  111. );
  112. } else if (account.getIn(['relationship', 'blocking'])) {
  113. actionBtn = (
  114. <div className='account--action-button'>
  115. <IconButton size={26} icon='unlock-alt' title={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.props.onBlock} />
  116. </div>
  117. );
  118. }
  119. } else {
  120. actionBtn = (
  121. <div className='account--action-button'>
  122. <IconButton size={26} icon='pencil' title={intl.formatMessage(messages.edit_profile)} onClick={this.openEditProfile} />
  123. </div>
  124. );
  125. }
  126. if (account.get('moved') && !account.getIn(['relationship', 'following'])) {
  127. actionBtn = '';
  128. }
  129. if (account.get('locked')) {
  130. lockedIcon = <i className='fa fa-lock' />;
  131. }
  132. const content = { __html: account.get('note_emojified') };
  133. const displayNameHtml = { __html: account.get('display_name_html') };
  134. const fields = account.get('fields');
  135. const badge = account.get('bot') ? (<div className='roles'><div className='account-role bot'><FormattedMessage id='account.badges.bot' defaultMessage='Bot' /></div></div>) : null;
  136. return (
  137. <div className={classNames('account__header', { inactive: !!account.get('moved') })} style={{ backgroundImage: `url(${account.get('header')})` }}>
  138. <div>
  139. <Avatar account={account} />
  140. <span className='account__header__display-name' dangerouslySetInnerHTML={displayNameHtml} />
  141. <span className='account__header__username'>@{account.get('acct')} {lockedIcon}</span>
  142. {badge}
  143. <div className='account__header__content' dangerouslySetInnerHTML={content} />
  144. {fields.size > 0 && (
  145. <div className='account__header__fields'>
  146. {fields.map((pair, i) => (
  147. <dl key={i}>
  148. <dt dangerouslySetInnerHTML={{ __html: pair.get('name_emojified') }} title={pair.get('name')} />
  149. <dd className={pair.get('verified_at') && 'verified'} title={pair.get('value_plain')}>
  150. {pair.get('verified_at') && <span title={intl.formatMessage(messages.linkVerifiedOn, { date: intl.formatDate(pair.get('verified_at'), dateFormatOptions) })}><i className='fa fa-check verified__mark' /></span>} <span dangerouslySetInnerHTML={{ __html: pair.get('value_emojified') }} />
  151. </dd>
  152. </dl>
  153. ))}
  154. </div>
  155. )}
  156. {info}
  157. {mutingInfo}
  158. {actionBtn}
  159. </div>
  160. </div>
  161. );
  162. }
  163. }