account.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import React, { Fragment } from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Avatar from './avatar';
  5. import DisplayName from './display_name';
  6. import Permalink from './permalink';
  7. import IconButton from './icon_button';
  8. import { defineMessages, injectIntl } from 'react-intl';
  9. import ImmutablePureComponent from 'react-immutable-pure-component';
  10. import { me } from '../initial_state';
  11. import RelativeTimestamp from './relative_timestamp';
  12. import Skeleton from 'mastodon/components/skeleton';
  13. const messages = defineMessages({
  14. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  15. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  16. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval' },
  17. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  18. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  19. mute_notifications: { id: 'account.mute_notifications', defaultMessage: 'Mute notifications from @{name}' },
  20. unmute_notifications: { id: 'account.unmute_notifications', defaultMessage: 'Unmute notifications from @{name}' },
  21. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  22. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  23. });
  24. export default @injectIntl
  25. class Account extends ImmutablePureComponent {
  26. static propTypes = {
  27. size: PropTypes.number,
  28. account: ImmutablePropTypes.map,
  29. onFollow: PropTypes.func.isRequired,
  30. onBlock: PropTypes.func.isRequired,
  31. onMute: PropTypes.func.isRequired,
  32. onMuteNotifications: PropTypes.func.isRequired,
  33. intl: PropTypes.object.isRequired,
  34. hidden: PropTypes.bool,
  35. actionIcon: PropTypes.string,
  36. actionTitle: PropTypes.string,
  37. defaultAction: PropTypes.string,
  38. onActionClick: PropTypes.func,
  39. };
  40. static defaultProps = {
  41. size: 46,
  42. };
  43. handleFollow = () => {
  44. this.props.onFollow(this.props.account);
  45. }
  46. handleBlock = () => {
  47. this.props.onBlock(this.props.account);
  48. }
  49. handleMute = () => {
  50. this.props.onMute(this.props.account);
  51. }
  52. handleMuteNotifications = () => {
  53. this.props.onMuteNotifications(this.props.account, true);
  54. }
  55. handleUnmuteNotifications = () => {
  56. this.props.onMuteNotifications(this.props.account, false);
  57. }
  58. handleAction = () => {
  59. this.props.onActionClick(this.props.account);
  60. }
  61. render () {
  62. const { account, intl, hidden, onActionClick, actionIcon, actionTitle, defaultAction, size } = this.props;
  63. if (!account) {
  64. return (
  65. <div className='account'>
  66. <div className='account__wrapper'>
  67. <div className='account__display-name'>
  68. <div className='account__avatar-wrapper'><Skeleton width={36} height={36} /></div>
  69. <DisplayName />
  70. </div>
  71. </div>
  72. </div>
  73. );
  74. }
  75. if (hidden) {
  76. return (
  77. <Fragment>
  78. {account.get('display_name')}
  79. {account.get('username')}
  80. </Fragment>
  81. );
  82. }
  83. let buttons;
  84. if (actionIcon) {
  85. if (onActionClick) {
  86. buttons = <IconButton icon={actionIcon} title={actionTitle} onClick={this.handleAction} />;
  87. }
  88. } else if (account.get('id') !== me && account.get('relationship', null) !== null) {
  89. const following = account.getIn(['relationship', 'following']);
  90. const requested = account.getIn(['relationship', 'requested']);
  91. const blocking = account.getIn(['relationship', 'blocking']);
  92. const muting = account.getIn(['relationship', 'muting']);
  93. if (requested) {
  94. buttons = <IconButton disabled icon='hourglass' title={intl.formatMessage(messages.requested)} />;
  95. } else if (blocking) {
  96. buttons = <IconButton active icon='unlock' title={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.handleBlock} />;
  97. } else if (muting) {
  98. let hidingNotificationsButton;
  99. if (account.getIn(['relationship', 'muting_notifications'])) {
  100. hidingNotificationsButton = <IconButton active icon='bell' title={intl.formatMessage(messages.unmute_notifications, { name: account.get('username') })} onClick={this.handleUnmuteNotifications} />;
  101. } else {
  102. hidingNotificationsButton = <IconButton active icon='bell-slash' title={intl.formatMessage(messages.mute_notifications, { name: account.get('username') })} onClick={this.handleMuteNotifications} />;
  103. }
  104. buttons = (
  105. <Fragment>
  106. <IconButton active icon='volume-up' title={intl.formatMessage(messages.unmute, { name: account.get('username') })} onClick={this.handleMute} />
  107. {hidingNotificationsButton}
  108. </Fragment>
  109. );
  110. } else if (defaultAction === 'mute') {
  111. buttons = <IconButton icon='volume-off' title={intl.formatMessage(messages.mute, { name: account.get('username') })} onClick={this.handleMute} />;
  112. } else if (defaultAction === 'block') {
  113. buttons = <IconButton icon='lock' title={intl.formatMessage(messages.block, { name: account.get('username') })} onClick={this.handleBlock} />;
  114. } else if (!account.get('moved') || following) {
  115. buttons = <IconButton icon={following ? 'user-times' : 'user-plus'} title={intl.formatMessage(following ? messages.unfollow : messages.follow)} onClick={this.handleFollow} active={following} />;
  116. }
  117. }
  118. let mute_expires_at;
  119. if (account.get('mute_expires_at')) {
  120. mute_expires_at = <div><RelativeTimestamp timestamp={account.get('mute_expires_at')} futureDate /></div>;
  121. }
  122. return (
  123. <div className='account'>
  124. <div className='account__wrapper'>
  125. <Permalink key={account.get('id')} className='account__display-name' title={account.get('acct')} href={account.get('url')} to={`/@${account.get('acct')}`}>
  126. <div className='account__avatar-wrapper'><Avatar account={account} size={size} /></div>
  127. {mute_expires_at}
  128. <DisplayName account={account} />
  129. </Permalink>
  130. <div className='account__relationship'>
  131. {buttons}
  132. </div>
  133. </div>
  134. </div>
  135. );
  136. }
  137. }