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.

164 lines
5.9 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ImmutablePureComponent from 'react-immutable-pure-component';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import IconButton from 'mastodon/components/icon_button';
  7. import classNames from 'classnames';
  8. import { me, boostModal } from 'mastodon/initial_state';
  9. import { defineMessages, injectIntl } from 'react-intl';
  10. import { replyCompose } from 'mastodon/actions/compose';
  11. import { reblog, favourite, unreblog, unfavourite } from 'mastodon/actions/interactions';
  12. import { makeGetStatus } from 'mastodon/selectors';
  13. import { initBoostModal } from 'mastodon/actions/boosts';
  14. import { openModal } from 'mastodon/actions/modal';
  15. const messages = defineMessages({
  16. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  17. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  18. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  19. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost with original visibility' },
  20. cancel_reblog_private: { id: 'status.cancel_reblog_private', defaultMessage: 'Unboost' },
  21. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  22. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  23. replyConfirm: { id: 'confirmations.reply.confirm', defaultMessage: 'Reply' },
  24. replyMessage: { id: 'confirmations.reply.message', defaultMessage: 'Replying now will overwrite the message you are currently composing. Are you sure you want to proceed?' },
  25. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  26. });
  27. const makeMapStateToProps = () => {
  28. const getStatus = makeGetStatus();
  29. const mapStateToProps = (state, { statusId }) => ({
  30. status: getStatus(state, { id: statusId }),
  31. askReplyConfirmation: state.getIn(['compose', 'text']).trim().length !== 0,
  32. });
  33. return mapStateToProps;
  34. };
  35. export default @connect(makeMapStateToProps)
  36. @injectIntl
  37. class Footer extends ImmutablePureComponent {
  38. static contextTypes = {
  39. router: PropTypes.object,
  40. };
  41. static propTypes = {
  42. statusId: PropTypes.string.isRequired,
  43. status: ImmutablePropTypes.map.isRequired,
  44. intl: PropTypes.object.isRequired,
  45. dispatch: PropTypes.func.isRequired,
  46. askReplyConfirmation: PropTypes.bool,
  47. withOpenButton: PropTypes.bool,
  48. onClose: PropTypes.func,
  49. };
  50. _performReply = () => {
  51. const { dispatch, status, onClose } = this.props;
  52. const { router } = this.context;
  53. if (onClose) {
  54. onClose();
  55. }
  56. dispatch(replyCompose(status, router.history));
  57. };
  58. handleReplyClick = () => {
  59. const { dispatch, askReplyConfirmation, intl } = this.props;
  60. if (askReplyConfirmation) {
  61. dispatch(openModal('CONFIRM', {
  62. message: intl.formatMessage(messages.replyMessage),
  63. confirm: intl.formatMessage(messages.replyConfirm),
  64. onConfirm: this._performReply,
  65. }));
  66. } else {
  67. this._performReply();
  68. }
  69. };
  70. handleFavouriteClick = () => {
  71. const { dispatch, status } = this.props;
  72. if (status.get('favourited')) {
  73. dispatch(unfavourite(status));
  74. } else {
  75. dispatch(favourite(status));
  76. }
  77. };
  78. _performReblog = (status, privacy) => {
  79. const { dispatch } = this.props;
  80. dispatch(reblog(status, privacy));
  81. }
  82. handleReblogClick = e => {
  83. const { dispatch, status } = this.props;
  84. if (status.get('reblogged')) {
  85. dispatch(unreblog(status));
  86. } else if ((e && e.shiftKey) || !boostModal) {
  87. this._performReblog(status);
  88. } else {
  89. dispatch(initBoostModal({ status, onReblog: this._performReblog }));
  90. }
  91. };
  92. handleOpenClick = e => {
  93. const { router } = this.context;
  94. if (e.button !== 0 || !router) {
  95. return;
  96. }
  97. const { status, onClose } = this.props;
  98. if (onClose) {
  99. onClose();
  100. }
  101. router.history.push(`/statuses/${status.get('id')}`);
  102. }
  103. render () {
  104. const { status, intl, withOpenButton } = this.props;
  105. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  106. const reblogPrivate = status.getIn(['account', 'id']) === me && status.get('visibility') === 'private';
  107. let replyIcon, replyTitle;
  108. if (status.get('in_reply_to_id', null) === null) {
  109. replyIcon = 'reply';
  110. replyTitle = intl.formatMessage(messages.reply);
  111. } else {
  112. replyIcon = 'reply-all';
  113. replyTitle = intl.formatMessage(messages.replyAll);
  114. }
  115. let reblogTitle = '';
  116. if (status.get('reblogged')) {
  117. reblogTitle = intl.formatMessage(messages.cancel_reblog_private);
  118. } else if (publicStatus) {
  119. reblogTitle = intl.formatMessage(messages.reblog);
  120. } else if (reblogPrivate) {
  121. reblogTitle = intl.formatMessage(messages.reblog_private);
  122. } else {
  123. reblogTitle = intl.formatMessage(messages.cannot_reblog);
  124. }
  125. return (
  126. <div className='picture-in-picture__footer'>
  127. <IconButton className='status__action-bar-button' title={replyTitle} icon={status.get('in_reply_to_account_id') === status.getIn(['account', 'id']) ? 'reply' : replyIcon} onClick={this.handleReplyClick} counter={status.get('replies_count')} obfuscateCount />
  128. <IconButton className={classNames('status__action-bar-button', { reblogPrivate })} disabled={!publicStatus && !reblogPrivate} active={status.get('reblogged')} pressed={status.get('reblogged')} title={reblogTitle} icon='retweet' onClick={this.handleReblogClick} counter={status.get('reblogs_count')} />
  129. <IconButton className='status__action-bar-button star-icon' animate active={status.get('favourited')} pressed={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='star' onClick={this.handleFavouriteClick} counter={status.get('favourites_count')} />
  130. {withOpenButton && <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.open)} icon='external-link' onClick={this.handleOpenClick} />}
  131. </div>
  132. );
  133. }
  134. }