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.

149 lines
6.1 KiB

  1. import React from 'react';
  2. import ImmutablePureComponent from 'react-immutable-pure-component';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import { connect } from 'react-redux';
  6. import { makeGetAccount } from 'mastodon/selectors';
  7. import Avatar from 'mastodon/components/avatar';
  8. import DisplayName from 'mastodon/components/display_name';
  9. import Permalink from 'mastodon/components/permalink';
  10. import RelativeTimestamp from 'mastodon/components/relative_timestamp';
  11. import IconButton from 'mastodon/components/icon_button';
  12. import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';
  13. import { autoPlayGif, me, unfollowModal } from 'mastodon/initial_state';
  14. import { shortNumberFormat } from 'mastodon/utils/numbers';
  15. import { followAccount, unfollowAccount, blockAccount, unblockAccount, unmuteAccount } from 'mastodon/actions/accounts';
  16. import { openModal } from 'mastodon/actions/modal';
  17. import { initMuteModal } from 'mastodon/actions/mutes';
  18. const messages = defineMessages({
  19. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  20. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  21. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval' },
  22. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  23. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  24. });
  25. const makeMapStateToProps = () => {
  26. const getAccount = makeGetAccount();
  27. const mapStateToProps = (state, { id }) => ({
  28. account: getAccount(state, id),
  29. });
  30. return mapStateToProps;
  31. };
  32. const mapDispatchToProps = (dispatch, { intl }) => ({
  33. onFollow (account) {
  34. if (account.getIn(['relationship', 'following']) || account.getIn(['relationship', 'requested'])) {
  35. if (unfollowModal) {
  36. dispatch(openModal('CONFIRM', {
  37. message: <FormattedMessage id='confirmations.unfollow.message' defaultMessage='Are you sure you want to unfollow {name}?' values={{ name: <strong>@{account.get('acct')}</strong> }} />,
  38. confirm: intl.formatMessage(messages.unfollowConfirm),
  39. onConfirm: () => dispatch(unfollowAccount(account.get('id'))),
  40. }));
  41. } else {
  42. dispatch(unfollowAccount(account.get('id')));
  43. }
  44. } else {
  45. dispatch(followAccount(account.get('id')));
  46. }
  47. },
  48. onBlock (account) {
  49. if (account.getIn(['relationship', 'blocking'])) {
  50. dispatch(unblockAccount(account.get('id')));
  51. } else {
  52. dispatch(blockAccount(account.get('id')));
  53. }
  54. },
  55. onMute (account) {
  56. if (account.getIn(['relationship', 'muting'])) {
  57. dispatch(unmuteAccount(account.get('id')));
  58. } else {
  59. dispatch(initMuteModal(account));
  60. }
  61. },
  62. });
  63. export default @injectIntl
  64. @connect(makeMapStateToProps, mapDispatchToProps)
  65. class AccountCard extends ImmutablePureComponent {
  66. static propTypes = {
  67. account: ImmutablePropTypes.map.isRequired,
  68. intl: PropTypes.object.isRequired,
  69. onFollow: PropTypes.func.isRequired,
  70. onBlock: PropTypes.func.isRequired,
  71. onMute: PropTypes.func.isRequired,
  72. };
  73. handleFollow = () => {
  74. this.props.onFollow(this.props.account);
  75. }
  76. handleBlock = () => {
  77. this.props.onBlock(this.props.account);
  78. }
  79. handleMute = () => {
  80. this.props.onMute(this.props.account);
  81. }
  82. render () {
  83. const { account, intl } = this.props;
  84. let buttons;
  85. if (account.get('id') !== me && account.get('relationship', null) !== null) {
  86. const following = account.getIn(['relationship', 'following']);
  87. const requested = account.getIn(['relationship', 'requested']);
  88. const blocking = account.getIn(['relationship', 'blocking']);
  89. const muting = account.getIn(['relationship', 'muting']);
  90. if (requested) {
  91. buttons = <IconButton disabled icon='hourglass' title={intl.formatMessage(messages.requested)} />;
  92. } else if (blocking) {
  93. buttons = <IconButton active icon='unlock' title={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.handleBlock} />;
  94. } else if (muting) {
  95. buttons = <IconButton active icon='volume-up' title={intl.formatMessage(messages.unmute, { name: account.get('username') })} onClick={this.handleMute} />;
  96. } else if (!account.get('moved') || following) {
  97. buttons = <IconButton icon={following ? 'user-times' : 'user-plus'} title={intl.formatMessage(following ? messages.unfollow : messages.follow)} onClick={this.handleFollow} active={following} />;
  98. }
  99. }
  100. return (
  101. <div className='directory__card'>
  102. <div className='directory__card__img'>
  103. <img src={autoPlayGif ? account.get('header') : account.get('header_static')} alt='' className='parallax' />
  104. </div>
  105. <div className='directory__card__bar'>
  106. <Permalink className='directory__card__bar__name' href={account.get('url')} to={`/accounts/${account.get('id')}`}>
  107. <Avatar account={account} size={48} />
  108. <DisplayName account={account} />
  109. </Permalink>
  110. <div className='directory__card__bar__relationship account__relationship'>
  111. {buttons}
  112. </div>
  113. </div>
  114. <div className='directory__card__extra'>
  115. {account.get('note').length > 0 && account.get('note') !== '<p></p>' && <div className='account__header__content' dangerouslySetInnerHTML={{ __html: account.get('note_emojified') }} />}
  116. </div>
  117. <div className='directory__card__extra'>
  118. <div className='accounts-table__count'>{shortNumberFormat(account.get('statuses_count'))} <small><FormattedMessage id='account.posts' defaultMessage='Toots' /></small></div>
  119. <div className='accounts-table__count'>{shortNumberFormat(account.get('followers_count'))} <small><FormattedMessage id='account.followers' defaultMessage='Followers' /></small></div>
  120. <div className='accounts-table__count'>{account.get('last_status_at') === null ? <FormattedMessage id='account.never_active' defaultMessage='Never' /> : <RelativeTimestamp timestamp={account.get('last_status_at')} />} <small><FormattedMessage id='account.last_status' defaultMessage='Last active' /></small></div>
  121. </div>
  122. </div>
  123. );
  124. }
  125. }