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.

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