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.

273 lines
14 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 Button from 'mastodon/components/button';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import { autoPlayGif, me, isStaff } from 'mastodon/initial_state';
  8. import classNames from 'classnames';
  9. import Icon from 'mastodon/components/icon';
  10. import Avatar from 'mastodon/components/avatar';
  11. import { shortNumberFormat } from 'mastodon/utils/numbers';
  12. import { NavLink } from 'react-router-dom';
  13. import DropdownMenuContainer from 'mastodon/containers/dropdown_menu_container';
  14. const messages = defineMessages({
  15. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  16. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  17. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' },
  18. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  19. edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
  20. linkVerifiedOn: { id: 'account.link_verified_on', defaultMessage: 'Ownership of this link was checked on {date}' },
  21. account_locked: { id: 'account.locked_info', defaultMessage: 'This account privacy status is set to locked. The owner manually reviews who can follow them.' },
  22. mention: { id: 'account.mention', defaultMessage: 'Mention @{name}' },
  23. direct: { id: 'account.direct', defaultMessage: 'Direct message @{name}' },
  24. edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
  25. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  26. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  27. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  28. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  29. report: { id: 'account.report', defaultMessage: 'Report @{name}' },
  30. share: { id: 'account.share', defaultMessage: 'Share @{name}\'s profile' },
  31. media: { id: 'account.media', defaultMessage: 'Media' },
  32. blockDomain: { id: 'account.block_domain', defaultMessage: 'Hide everything from {domain}' },
  33. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unhide {domain}' },
  34. hideReblogs: { id: 'account.hide_reblogs', defaultMessage: 'Hide boosts from @{name}' },
  35. showReblogs: { id: 'account.show_reblogs', defaultMessage: 'Show boosts from @{name}' },
  36. pins: { id: 'navigation_bar.pins', defaultMessage: 'Pinned toots' },
  37. preferences: { id: 'navigation_bar.preferences', defaultMessage: 'Preferences' },
  38. follow_requests: { id: 'navigation_bar.follow_requests', defaultMessage: 'Follow requests' },
  39. favourites: { id: 'navigation_bar.favourites', defaultMessage: 'Favourites' },
  40. lists: { id: 'navigation_bar.lists', defaultMessage: 'Lists' },
  41. blocks: { id: 'navigation_bar.blocks', defaultMessage: 'Blocked users' },
  42. domain_blocks: { id: 'navigation_bar.domain_blocks', defaultMessage: 'Hidden domains' },
  43. mutes: { id: 'navigation_bar.mutes', defaultMessage: 'Muted users' },
  44. endorse: { id: 'account.endorse', defaultMessage: 'Feature on profile' },
  45. unendorse: { id: 'account.unendorse', defaultMessage: 'Don\'t feature on profile' },
  46. add_or_remove_from_list: { id: 'account.add_or_remove_from_list', defaultMessage: 'Add or Remove from lists' },
  47. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  48. });
  49. const dateFormatOptions = {
  50. month: 'short',
  51. day: 'numeric',
  52. year: 'numeric',
  53. hour12: false,
  54. hour: '2-digit',
  55. minute: '2-digit',
  56. };
  57. export default @injectIntl
  58. class Header extends ImmutablePureComponent {
  59. static propTypes = {
  60. account: ImmutablePropTypes.map,
  61. onFollow: PropTypes.func.isRequired,
  62. onBlock: PropTypes.func.isRequired,
  63. intl: PropTypes.object.isRequired,
  64. domain: PropTypes.string.isRequired,
  65. };
  66. openEditProfile = () => {
  67. window.open('/settings/profile', '_blank');
  68. }
  69. isStatusesPageActive = (match, location) => {
  70. if (!match) {
  71. return false;
  72. }
  73. return !location.pathname.match(/\/(followers|following)\/?$/);
  74. }
  75. render () {
  76. const { account, intl, domain } = this.props;
  77. if (!account) {
  78. return null;
  79. }
  80. let info = [];
  81. let actionBtn = '';
  82. let lockedIcon = '';
  83. let menu = [];
  84. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  85. info.push(<span className='relationship-tag'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>);
  86. } else if (me !== account.get('id') && account.getIn(['relationship', 'blocking'])) {
  87. info.push(<span className='relationship-tag'><FormattedMessage id='account.blocked' defaultMessage='Blocked' /></span>);
  88. }
  89. if (me !== account.get('id') && account.getIn(['relationship', 'muting'])) {
  90. info.push(<span className='relationship-tag'><FormattedMessage id='account.muted' defaultMessage='Muted' /></span>);
  91. } else if (me !== account.get('id') && account.getIn(['relationship', 'domain_blocking'])) {
  92. info.push(<span className='relationship-tag'><FormattedMessage id='account.domain_blocked' defaultMessage='Domain hidden' /></span>);
  93. }
  94. if (me !== account.get('id')) {
  95. if (!account.get('relationship')) { // Wait until the relationship is loaded
  96. actionBtn = '';
  97. } else if (account.getIn(['relationship', 'requested'])) {
  98. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />;
  99. } else if (!account.getIn(['relationship', 'blocking'])) {
  100. actionBtn = <Button className={classNames('logo-button', { 'button--destructive': account.getIn(['relationship', 'following']) })} text={intl.formatMessage(account.getIn(['relationship', 'following']) ? messages.unfollow : messages.follow)} onClick={this.props.onFollow} />;
  101. } else if (account.getIn(['relationship', 'blocking'])) {
  102. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.props.onBlock} />;
  103. }
  104. } else {
  105. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.edit_profile)} onClick={this.openEditProfile} />;
  106. }
  107. if (account.get('moved') && !account.getIn(['relationship', 'following'])) {
  108. actionBtn = '';
  109. }
  110. if (account.get('locked')) {
  111. lockedIcon = <Icon id='lock' title={intl.formatMessage(messages.account_locked)} />;
  112. }
  113. if (account.get('id') !== me) {
  114. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.props.onMention });
  115. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.props.onDirect });
  116. menu.push(null);
  117. }
  118. if ('share' in navigator) {
  119. menu.push({ text: intl.formatMessage(messages.share, { name: account.get('username') }), action: this.handleShare });
  120. menu.push(null);
  121. }
  122. if (account.get('id') === me) {
  123. menu.push({ text: intl.formatMessage(messages.edit_profile), href: '/settings/profile' });
  124. menu.push({ text: intl.formatMessage(messages.preferences), href: '/settings/preferences' });
  125. menu.push({ text: intl.formatMessage(messages.pins), to: '/pinned' });
  126. menu.push(null);
  127. menu.push({ text: intl.formatMessage(messages.follow_requests), to: '/follow_requests' });
  128. menu.push({ text: intl.formatMessage(messages.favourites), to: '/favourites' });
  129. menu.push({ text: intl.formatMessage(messages.lists), to: '/lists' });
  130. menu.push(null);
  131. menu.push({ text: intl.formatMessage(messages.mutes), to: '/mutes' });
  132. menu.push({ text: intl.formatMessage(messages.blocks), to: '/blocks' });
  133. menu.push({ text: intl.formatMessage(messages.domain_blocks), to: '/domain_blocks' });
  134. } else {
  135. if (account.getIn(['relationship', 'following'])) {
  136. if (account.getIn(['relationship', 'showing_reblogs'])) {
  137. menu.push({ text: intl.formatMessage(messages.hideReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  138. } else {
  139. menu.push({ text: intl.formatMessage(messages.showReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  140. }
  141. menu.push({ text: intl.formatMessage(account.getIn(['relationship', 'endorsed']) ? messages.unendorse : messages.endorse), action: this.props.onEndorseToggle });
  142. menu.push({ text: intl.formatMessage(messages.add_or_remove_from_list), action: this.props.onAddToList });
  143. menu.push(null);
  144. }
  145. if (account.getIn(['relationship', 'muting'])) {
  146. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.props.onMute });
  147. } else {
  148. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.props.onMute });
  149. }
  150. if (account.getIn(['relationship', 'blocking'])) {
  151. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.props.onBlock });
  152. } else {
  153. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.props.onBlock });
  154. }
  155. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.props.onReport });
  156. }
  157. if (account.get('acct') !== account.get('username')) {
  158. const domain = account.get('acct').split('@')[1];
  159. menu.push(null);
  160. if (account.getIn(['relationship', 'domain_blocking'])) {
  161. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.props.onUnblockDomain });
  162. } else {
  163. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.props.onBlockDomain });
  164. }
  165. }
  166. if (account.get('id') !== me && isStaff) {
  167. menu.push(null);
  168. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${account.get('id')}` });
  169. }
  170. const content = { __html: account.get('note_emojified') };
  171. const displayNameHtml = { __html: account.get('display_name_html') };
  172. const fields = account.get('fields');
  173. const badge = account.get('bot') ? (<div className='account-role bot'><FormattedMessage id='account.badges.bot' defaultMessage='Bot' /></div>) : null;
  174. const acct = account.get('acct').indexOf('@') === -1 && domain ? `${account.get('acct')}@${domain}` : account.get('acct');
  175. return (
  176. <div className={classNames('account__header', { inactive: !!account.get('moved') })}>
  177. <div className='account__header__image'>
  178. <div className='account__header__info'>
  179. {info}
  180. </div>
  181. <img src={autoPlayGif ? account.get('header') : account.get('header_static')} alt='' className='parallax' />
  182. </div>
  183. <div className='account__header__bar'>
  184. <div className='account__header__tabs'>
  185. <a className='avatar' href={account.get('url')} target='_blank'>
  186. <Avatar account={account} size={90} />
  187. </a>
  188. <div className='spacer' />
  189. <div className='account__header__tabs__buttons'>
  190. <DropdownMenuContainer items={menu} icon='ellipsis-v' size={24} direction='right' />
  191. {actionBtn}
  192. </div>
  193. </div>
  194. <div className='account__header__tabs__name'>
  195. <h1>
  196. <span dangerouslySetInnerHTML={displayNameHtml} /> {badge}
  197. <small>@{acct} {lockedIcon}</small>
  198. </h1>
  199. </div>
  200. <div className='account__header__extra'>
  201. <div className='account__header__bio'>
  202. {fields.size > 0 && (
  203. <div className='account__header__fields'>
  204. {fields.map((pair, i) => (
  205. <dl key={i}>
  206. <dt dangerouslySetInnerHTML={{ __html: pair.get('name_emojified') }} title={pair.get('name')} />
  207. <dd className={pair.get('verified_at') && 'verified'} title={pair.get('value_plain')}>
  208. {pair.get('verified_at') && <span title={intl.formatMessage(messages.linkVerifiedOn, { date: intl.formatDate(pair.get('verified_at'), dateFormatOptions) })}><Icon id='check' className='verified__mark' /></span>} <span dangerouslySetInnerHTML={{ __html: pair.get('value_emojified') }} />
  209. </dd>
  210. </dl>
  211. ))}
  212. </div>
  213. )}
  214. {account.get('note').length > 0 && account.get('note') !== '<p></p>' && <div className='account__header__content' dangerouslySetInnerHTML={content} />}
  215. </div>
  216. <div className='account__header__extra__links'>
  217. <NavLink isActive={this.isStatusesPageActive} activeClassName='active' to={`/accounts/${account.get('id')}`} title={intl.formatNumber(account.get('statuses_count'))}>
  218. <strong>{shortNumberFormat(account.get('statuses_count'))}</strong> <FormattedMessage id='account.posts' defaultMessage='Toots' />
  219. </NavLink>
  220. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/following`} title={intl.formatNumber(account.get('following_count'))}>
  221. <strong>{shortNumberFormat(account.get('following_count'))}</strong> <FormattedMessage id='account.follows' defaultMessage='Follows' />
  222. </NavLink>
  223. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/followers`} title={intl.formatNumber(account.get('followers_count'))}>
  224. <strong>{shortNumberFormat(account.get('followers_count'))}</strong> <FormattedMessage id='account.followers' defaultMessage='Followers' />
  225. </NavLink>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. );
  231. }
  232. }