account.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import PropTypes from 'prop-types';
  2. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  3. import classNames from 'classnames';
  4. import { Link } from 'react-router-dom';
  5. import ImmutablePropTypes from 'react-immutable-proptypes';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import { EmptyAccount } from 'mastodon/components/empty_account';
  8. import { ShortNumber } from 'mastodon/components/short_number';
  9. import { VerifiedBadge } from 'mastodon/components/verified_badge';
  10. import { me } from '../initial_state';
  11. import { Avatar } from './avatar';
  12. import { Button } from './button';
  13. import { FollowersCounter } from './counters';
  14. import { DisplayName } from './display_name';
  15. import { RelativeTimestamp } from './relative_timestamp';
  16. const messages = defineMessages({
  17. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  18. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  19. cancel_follow_request: { id: 'account.cancel_follow_request', defaultMessage: 'Withdraw follow request' },
  20. unblock: { id: 'account.unblock_short', defaultMessage: 'Unblock' },
  21. unmute: { id: 'account.unmute_short', defaultMessage: 'Unmute' },
  22. mute_notifications: { id: 'account.mute_notifications_short', defaultMessage: 'Mute notifications' },
  23. unmute_notifications: { id: 'account.unmute_notifications_short', defaultMessage: 'Unmute notifications' },
  24. mute: { id: 'account.mute_short', defaultMessage: 'Mute' },
  25. block: { id: 'account.block_short', defaultMessage: 'Block' },
  26. });
  27. class Account extends ImmutablePureComponent {
  28. static propTypes = {
  29. size: PropTypes.number,
  30. account: ImmutablePropTypes.record,
  31. onFollow: PropTypes.func.isRequired,
  32. onBlock: PropTypes.func.isRequired,
  33. onMute: PropTypes.func.isRequired,
  34. onMuteNotifications: PropTypes.func.isRequired,
  35. intl: PropTypes.object.isRequired,
  36. hidden: PropTypes.bool,
  37. minimal: PropTypes.bool,
  38. defaultAction: PropTypes.string,
  39. withBio: PropTypes.bool,
  40. };
  41. static defaultProps = {
  42. size: 46,
  43. };
  44. handleFollow = () => {
  45. this.props.onFollow(this.props.account);
  46. };
  47. handleBlock = () => {
  48. this.props.onBlock(this.props.account);
  49. };
  50. handleMute = () => {
  51. this.props.onMute(this.props.account);
  52. };
  53. handleMuteNotifications = () => {
  54. this.props.onMuteNotifications(this.props.account, true);
  55. };
  56. handleUnmuteNotifications = () => {
  57. this.props.onMuteNotifications(this.props.account, false);
  58. };
  59. render () {
  60. const { account, intl, hidden, withBio, defaultAction, size, minimal } = this.props;
  61. if (!account) {
  62. return <EmptyAccount size={size} minimal={minimal} />;
  63. }
  64. if (hidden) {
  65. return (
  66. <>
  67. {account.get('display_name')}
  68. {account.get('username')}
  69. </>
  70. );
  71. }
  72. let buttons;
  73. if (account.get('id') !== me && account.get('relationship', null) !== null) {
  74. const following = account.getIn(['relationship', 'following']);
  75. const requested = account.getIn(['relationship', 'requested']);
  76. const blocking = account.getIn(['relationship', 'blocking']);
  77. const muting = account.getIn(['relationship', 'muting']);
  78. if (requested) {
  79. buttons = <Button text={intl.formatMessage(messages.cancel_follow_request)} onClick={this.handleFollow} />;
  80. } else if (blocking) {
  81. buttons = <Button text={intl.formatMessage(messages.unblock)} onClick={this.handleBlock} />;
  82. } else if (muting) {
  83. let hidingNotificationsButton;
  84. if (account.getIn(['relationship', 'muting_notifications'])) {
  85. hidingNotificationsButton = <Button text={intl.formatMessage(messages.unmute_notifications)} onClick={this.handleUnmuteNotifications} />;
  86. } else {
  87. hidingNotificationsButton = <Button text={intl.formatMessage(messages.mute_notifications)} onClick={this.handleMuteNotifications} />;
  88. }
  89. buttons = (
  90. <>
  91. <Button text={intl.formatMessage(messages.unmute)} onClick={this.handleMute} />
  92. {hidingNotificationsButton}
  93. </>
  94. );
  95. } else if (defaultAction === 'mute') {
  96. buttons = <Button title={intl.formatMessage(messages.mute)} onClick={this.handleMute} />;
  97. } else if (defaultAction === 'block') {
  98. buttons = <Button text={intl.formatMessage(messages.block)} onClick={this.handleBlock} />;
  99. } else if (!account.get('suspended') && !account.get('moved') || following) {
  100. buttons = <Button text={intl.formatMessage(following ? messages.unfollow : messages.follow)} onClick={this.handleFollow} />;
  101. }
  102. }
  103. let muteTimeRemaining;
  104. if (account.get('mute_expires_at')) {
  105. muteTimeRemaining = <>· <RelativeTimestamp timestamp={account.get('mute_expires_at')} futureDate /></>;
  106. }
  107. let verification;
  108. const firstVerifiedField = account.get('fields').find(item => !!item.get('verified_at'));
  109. if (firstVerifiedField) {
  110. verification = <VerifiedBadge link={firstVerifiedField.get('value')} />;
  111. }
  112. return (
  113. <div className={classNames('account', { 'account--minimal': minimal })}>
  114. <div className='account__wrapper'>
  115. <Link key={account.get('id')} className='account__display-name' title={account.get('acct')} to={`/@${account.get('acct')}`}>
  116. <div className='account__avatar-wrapper'>
  117. <Avatar account={account} size={size} />
  118. </div>
  119. <div className='account__contents'>
  120. <DisplayName account={account} />
  121. {!minimal && (
  122. <div className='account__details'>
  123. <ShortNumber value={account.get('followers_count')} renderer={FollowersCounter} /> {verification} {muteTimeRemaining}
  124. </div>
  125. )}
  126. </div>
  127. </Link>
  128. {!minimal && (
  129. <div className='account__relationship'>
  130. {buttons}
  131. </div>
  132. )}
  133. </div>
  134. {withBio && (account.get('note').length > 0 ? (
  135. <div
  136. className='account__note translate'
  137. dangerouslySetInnerHTML={{ __html: account.get('note_emojified') }}
  138. />
  139. ) : (
  140. <div className='account__note account__note--missing'><FormattedMessage id='account.no_bio' defaultMessage='No description provided.' /></div>
  141. ))}
  142. </div>
  143. );
  144. }
  145. }
  146. export default injectIntl(Account);