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.

335 lines
16 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. cancel_follow_request: { id: 'account.cancel_follow_request', defaultMessage: 'Cancel follow request' },
  18. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' },
  19. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  20. edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
  21. linkVerifiedOn: { id: 'account.link_verified_on', defaultMessage: 'Ownership of this link was checked on {date}' },
  22. account_locked: { id: 'account.locked_info', defaultMessage: 'This account privacy status is set to locked. The owner manually reviews who can follow them.' },
  23. mention: { id: 'account.mention', defaultMessage: 'Mention @{name}' },
  24. direct: { id: 'account.direct', defaultMessage: 'Direct message @{name}' },
  25. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  26. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  27. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  28. report: { id: 'account.report', defaultMessage: 'Report @{name}' },
  29. share: { id: 'account.share', defaultMessage: 'Share @{name}\'s profile' },
  30. media: { id: 'account.media', defaultMessage: 'Media' },
  31. blockDomain: { id: 'account.block_domain', defaultMessage: 'Block domain {domain}' },
  32. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unblock domain {domain}' },
  33. hideReblogs: { id: 'account.hide_reblogs', defaultMessage: 'Hide boosts from @{name}' },
  34. showReblogs: { id: 'account.show_reblogs', defaultMessage: 'Show boosts from @{name}' },
  35. pins: { id: 'navigation_bar.pins', defaultMessage: 'Pinned toots' },
  36. preferences: { id: 'navigation_bar.preferences', defaultMessage: 'Preferences' },
  37. follow_requests: { id: 'navigation_bar.follow_requests', defaultMessage: 'Follow requests' },
  38. favourites: { id: 'navigation_bar.favourites', defaultMessage: 'Favourites' },
  39. lists: { id: 'navigation_bar.lists', defaultMessage: 'Lists' },
  40. blocks: { id: 'navigation_bar.blocks', defaultMessage: 'Blocked users' },
  41. domain_blocks: { id: 'navigation_bar.domain_blocks', defaultMessage: 'Blocked domains' },
  42. mutes: { id: 'navigation_bar.mutes', defaultMessage: 'Muted users' },
  43. endorse: { id: 'account.endorse', defaultMessage: 'Feature on profile' },
  44. unendorse: { id: 'account.unendorse', defaultMessage: 'Don\'t feature on profile' },
  45. add_or_remove_from_list: { id: 'account.add_or_remove_from_list', defaultMessage: 'Add or Remove from lists' },
  46. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  47. });
  48. const dateFormatOptions = {
  49. month: 'short',
  50. day: 'numeric',
  51. year: 'numeric',
  52. hour12: false,
  53. hour: '2-digit',
  54. minute: '2-digit',
  55. };
  56. export default @injectIntl
  57. class Header extends ImmutablePureComponent {
  58. static propTypes = {
  59. account: ImmutablePropTypes.map,
  60. identity_props: ImmutablePropTypes.list,
  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. _updateEmojis () {
  76. const node = this.node;
  77. if (!node || autoPlayGif) {
  78. return;
  79. }
  80. const emojis = node.querySelectorAll('.custom-emoji');
  81. for (var i = 0; i < emojis.length; i++) {
  82. let emoji = emojis[i];
  83. if (emoji.classList.contains('status-emoji')) {
  84. continue;
  85. }
  86. emoji.classList.add('status-emoji');
  87. emoji.addEventListener('mouseenter', this.handleEmojiMouseEnter, false);
  88. emoji.addEventListener('mouseleave', this.handleEmojiMouseLeave, false);
  89. }
  90. }
  91. componentDidMount () {
  92. this._updateEmojis();
  93. }
  94. componentDidUpdate () {
  95. this._updateEmojis();
  96. }
  97. handleEmojiMouseEnter = ({ target }) => {
  98. target.src = target.getAttribute('data-original');
  99. }
  100. handleEmojiMouseLeave = ({ target }) => {
  101. target.src = target.getAttribute('data-static');
  102. }
  103. setRef = (c) => {
  104. this.node = c;
  105. }
  106. render () {
  107. const { account, intl, domain, identity_proofs } = this.props;
  108. if (!account) {
  109. return null;
  110. }
  111. let info = [];
  112. let actionBtn = '';
  113. let lockedIcon = '';
  114. let menu = [];
  115. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  116. info.push(<span key='followed_by' className='relationship-tag'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>);
  117. } else if (me !== account.get('id') && account.getIn(['relationship', 'blocking'])) {
  118. info.push(<span key='blocked' className='relationship-tag'><FormattedMessage id='account.blocked' defaultMessage='Blocked' /></span>);
  119. }
  120. if (me !== account.get('id') && account.getIn(['relationship', 'muting'])) {
  121. info.push(<span key='muted' className='relationship-tag'><FormattedMessage id='account.muted' defaultMessage='Muted' /></span>);
  122. } else if (me !== account.get('id') && account.getIn(['relationship', 'domain_blocking'])) {
  123. info.push(<span key='domain_blocked' className='relationship-tag'><FormattedMessage id='account.domain_blocked' defaultMessage='Domain blocked' /></span>);
  124. }
  125. if (me !== account.get('id')) {
  126. if (!account.get('relationship')) { // Wait until the relationship is loaded
  127. actionBtn = '';
  128. } else if (account.getIn(['relationship', 'requested'])) {
  129. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.cancel_follow_request)} title={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />;
  130. } else if (!account.getIn(['relationship', 'blocking'])) {
  131. actionBtn = <Button disabled={account.getIn(['relationship', 'blocked_by'])} 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} />;
  132. } else if (account.getIn(['relationship', 'blocking'])) {
  133. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.props.onBlock} />;
  134. }
  135. } else {
  136. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.edit_profile)} onClick={this.openEditProfile} />;
  137. }
  138. if (account.get('moved') && !account.getIn(['relationship', 'following'])) {
  139. actionBtn = '';
  140. }
  141. if (account.get('locked')) {
  142. lockedIcon = <Icon id='lock' title={intl.formatMessage(messages.account_locked)} />;
  143. }
  144. if (account.get('id') !== me) {
  145. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.props.onMention });
  146. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.props.onDirect });
  147. menu.push(null);
  148. }
  149. if ('share' in navigator) {
  150. menu.push({ text: intl.formatMessage(messages.share, { name: account.get('username') }), action: this.handleShare });
  151. menu.push(null);
  152. }
  153. if (account.get('id') === me) {
  154. menu.push({ text: intl.formatMessage(messages.edit_profile), href: '/settings/profile' });
  155. menu.push({ text: intl.formatMessage(messages.preferences), href: '/settings/preferences' });
  156. menu.push({ text: intl.formatMessage(messages.pins), to: '/pinned' });
  157. menu.push(null);
  158. menu.push({ text: intl.formatMessage(messages.follow_requests), to: '/follow_requests' });
  159. menu.push({ text: intl.formatMessage(messages.favourites), to: '/favourites' });
  160. menu.push({ text: intl.formatMessage(messages.lists), to: '/lists' });
  161. menu.push(null);
  162. menu.push({ text: intl.formatMessage(messages.mutes), to: '/mutes' });
  163. menu.push({ text: intl.formatMessage(messages.blocks), to: '/blocks' });
  164. menu.push({ text: intl.formatMessage(messages.domain_blocks), to: '/domain_blocks' });
  165. } else {
  166. if (account.getIn(['relationship', 'following'])) {
  167. if (account.getIn(['relationship', 'showing_reblogs'])) {
  168. menu.push({ text: intl.formatMessage(messages.hideReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  169. } else {
  170. menu.push({ text: intl.formatMessage(messages.showReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  171. }
  172. menu.push({ text: intl.formatMessage(account.getIn(['relationship', 'endorsed']) ? messages.unendorse : messages.endorse), action: this.props.onEndorseToggle });
  173. menu.push({ text: intl.formatMessage(messages.add_or_remove_from_list), action: this.props.onAddToList });
  174. menu.push(null);
  175. }
  176. if (account.getIn(['relationship', 'muting'])) {
  177. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.props.onMute });
  178. } else {
  179. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.props.onMute });
  180. }
  181. if (account.getIn(['relationship', 'blocking'])) {
  182. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.props.onBlock });
  183. } else {
  184. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.props.onBlock });
  185. }
  186. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.props.onReport });
  187. }
  188. if (account.get('acct') !== account.get('username')) {
  189. const domain = account.get('acct').split('@')[1];
  190. menu.push(null);
  191. if (account.getIn(['relationship', 'domain_blocking'])) {
  192. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.props.onUnblockDomain });
  193. } else {
  194. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.props.onBlockDomain });
  195. }
  196. }
  197. if (account.get('id') !== me && isStaff) {
  198. menu.push(null);
  199. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${account.get('id')}` });
  200. }
  201. const content = { __html: account.get('note_emojified') };
  202. const displayNameHtml = { __html: account.get('display_name_html') };
  203. const fields = account.get('fields');
  204. const acct = account.get('acct').indexOf('@') === -1 && domain ? `${account.get('acct')}@${domain}` : account.get('acct');
  205. let badge;
  206. if (account.get('bot')) {
  207. badge = (<div className='account-role bot'><FormattedMessage id='account.badges.bot' defaultMessage='Bot' /></div>);
  208. } else if (account.get('group')) {
  209. badge = (<div className='account-role group'><FormattedMessage id='account.badges.group' defaultMessage='Group' /></div>);
  210. } else {
  211. badge = null;
  212. }
  213. return (
  214. <div className={classNames('account__header', { inactive: !!account.get('moved') })} ref={this.setRef}>
  215. <div className='account__header__image'>
  216. <div className='account__header__info'>
  217. {info}
  218. </div>
  219. <img src={autoPlayGif ? account.get('header') : account.get('header_static')} alt='' className='parallax' />
  220. </div>
  221. <div className='account__header__bar'>
  222. <div className='account__header__tabs'>
  223. <a className='avatar' href={account.get('url')} rel='noopener noreferrer' target='_blank'>
  224. <Avatar account={account} size={90} />
  225. </a>
  226. <div className='spacer' />
  227. <div className='account__header__tabs__buttons'>
  228. {actionBtn}
  229. <DropdownMenuContainer items={menu} icon='ellipsis-v' size={24} direction='right' />
  230. </div>
  231. </div>
  232. <div className='account__header__tabs__name'>
  233. <h1>
  234. <span dangerouslySetInnerHTML={displayNameHtml} /> {badge}
  235. <small>@{acct} {lockedIcon}</small>
  236. </h1>
  237. </div>
  238. <div className='account__header__extra'>
  239. <div className='account__header__bio'>
  240. { (fields.size > 0 || identity_proofs.size > 0) && (
  241. <div className='account__header__fields'>
  242. {identity_proofs.map((proof, i) => (
  243. <dl key={i}>
  244. <dt dangerouslySetInnerHTML={{ __html: proof.get('provider') }} />
  245. <dd className='verified'>
  246. <a href={proof.get('proof_url')} target='_blank' rel='noopener noreferrer'><span title={intl.formatMessage(messages.linkVerifiedOn, { date: intl.formatDate(proof.get('updated_at'), dateFormatOptions) })}>
  247. <Icon id='check' className='verified__mark' />
  248. </span></a>
  249. <a href={proof.get('profile_url')} target='_blank' rel='noopener noreferrer'><span dangerouslySetInnerHTML={{ __html: ' '+proof.get('provider_username') }} /></a>
  250. </dd>
  251. </dl>
  252. ))}
  253. {fields.map((pair, i) => (
  254. <dl key={i}>
  255. <dt dangerouslySetInnerHTML={{ __html: pair.get('name_emojified') }} title={pair.get('name')} />
  256. <dd className={pair.get('verified_at') && 'verified'} title={pair.get('value_plain')}>
  257. {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') }} />
  258. </dd>
  259. </dl>
  260. ))}
  261. </div>
  262. )}
  263. {account.get('note').length > 0 && account.get('note') !== '<p></p>' && <div className='account__header__content' dangerouslySetInnerHTML={content} />}
  264. </div>
  265. <div className='account__header__extra__links'>
  266. <NavLink isActive={this.isStatusesPageActive} activeClassName='active' to={`/accounts/${account.get('id')}`} title={intl.formatNumber(account.get('statuses_count'))}>
  267. <strong>{shortNumberFormat(account.get('statuses_count'))}</strong> <FormattedMessage id='account.posts' defaultMessage='Toots' />
  268. </NavLink>
  269. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/following`} title={intl.formatNumber(account.get('following_count'))}>
  270. <strong>{shortNumberFormat(account.get('following_count'))}</strong> <FormattedMessage id='account.follows' defaultMessage='Follows' />
  271. </NavLink>
  272. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/followers`} title={intl.formatNumber(account.get('followers_count'))}>
  273. <strong>{shortNumberFormat(account.get('followers_count'))}</strong> <FormattedMessage id='account.followers' defaultMessage='Followers' />
  274. </NavLink>
  275. </div>
  276. </div>
  277. </div>
  278. </div>
  279. );
  280. }
  281. }