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.

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