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.

194 lines
7.6 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. account_locked: { id: 'account.locked_info', defaultMessage: 'This account privacy status is set to locked. The owner manually reviews who can follow them.' },
  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. </a>
  63. )}
  64. </Motion>
  65. );
  66. }
  67. }
  68. export default @injectIntl
  69. class Header extends ImmutablePureComponent {
  70. static propTypes = {
  71. account: ImmutablePropTypes.map,
  72. onFollow: PropTypes.func.isRequired,
  73. onBlock: PropTypes.func.isRequired,
  74. intl: PropTypes.object.isRequired,
  75. };
  76. openEditProfile = () => {
  77. window.open('/settings/profile', '_blank');
  78. }
  79. render () {
  80. const { account, intl } = this.props;
  81. if (!account) {
  82. return null;
  83. }
  84. let info = '';
  85. let mutingInfo = '';
  86. let actionBtn = '';
  87. let lockedIcon = '';
  88. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  89. info = <span className='account--follows-info'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>;
  90. } else if (me !== account.get('id') && account.getIn(['relationship', 'blocking'])) {
  91. info = <span className='account--follows-info'><FormattedMessage id='account.blocked' defaultMessage='Blocked' /></span>;
  92. }
  93. if (me !== account.get('id') && account.getIn(['relationship', 'muting'])) {
  94. mutingInfo = <span className='account--muting-info'><FormattedMessage id='account.muted' defaultMessage='Muted' /></span>;
  95. } else if (me !== account.get('id') && account.getIn(['relationship', 'domain_blocking'])) {
  96. mutingInfo = <span className='account--muting-info'><FormattedMessage id='account.domain_blocked' defaultMessage='Domain hidden' /></span>;
  97. }
  98. if (me !== account.get('id')) {
  99. if (!account.get('relationship')) { // Wait until the relationship is loaded
  100. actionBtn = '';
  101. } else if (account.getIn(['relationship', 'requested'])) {
  102. actionBtn = (
  103. <div className='account--action-button'>
  104. <IconButton size={26} active icon='hourglass' title={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />
  105. </div>
  106. );
  107. } else if (!account.getIn(['relationship', 'blocking'])) {
  108. actionBtn = (
  109. <div className='account--action-button'>
  110. <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} />
  111. </div>
  112. );
  113. } else if (account.getIn(['relationship', 'blocking'])) {
  114. actionBtn = (
  115. <div className='account--action-button'>
  116. <IconButton size={26} icon='unlock-alt' title={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.props.onBlock} />
  117. </div>
  118. );
  119. }
  120. } else {
  121. actionBtn = (
  122. <div className='account--action-button'>
  123. <IconButton size={26} icon='pencil' title={intl.formatMessage(messages.edit_profile)} onClick={this.openEditProfile} />
  124. </div>
  125. );
  126. }
  127. if (account.get('moved') && !account.getIn(['relationship', 'following'])) {
  128. actionBtn = '';
  129. }
  130. if (account.get('locked')) {
  131. lockedIcon = <i className='fa fa-lock' title={intl.formatMessage(messages.account_locked)} />;
  132. }
  133. const content = { __html: account.get('note_emojified') };
  134. const displayNameHtml = { __html: account.get('display_name_html') };
  135. const fields = account.get('fields');
  136. const badge = account.get('bot') ? (<div className='roles'><div className='account-role bot'><FormattedMessage id='account.badges.bot' defaultMessage='Bot' /></div></div>) : null;
  137. return (
  138. <div className={classNames('account__header', { inactive: !!account.get('moved') })} style={{ backgroundImage: `url(${autoPlayGif ? account.get('header') : account.get('header_static')})` }}>
  139. <div>
  140. <Avatar account={account} />
  141. <span className='account__header__display-name' dangerouslySetInnerHTML={displayNameHtml} />
  142. <span className='account__header__username'>@{account.get('acct')} {lockedIcon}</span>
  143. {badge}
  144. <div className='account__header__content' dangerouslySetInnerHTML={content} />
  145. {fields.size > 0 && (
  146. <div className='account__header__fields'>
  147. {fields.map((pair, i) => (
  148. <dl key={i}>
  149. <dt dangerouslySetInnerHTML={{ __html: pair.get('name_emojified') }} title={pair.get('name')} />
  150. <dd className={pair.get('verified_at') && 'verified'} title={pair.get('value_plain')}>
  151. {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') }} />
  152. </dd>
  153. </dl>
  154. ))}
  155. </div>
  156. )}
  157. {info}
  158. {mutingInfo}
  159. {actionBtn}
  160. </div>
  161. </div>
  162. );
  163. }
  164. }