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.

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