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.

192 lines
6.8 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. class Footer extends ImmutablePureComponent {
  36. static contextTypes = {
  37. router: PropTypes.object,
  38. identity: PropTypes.object,
  39. };
  40. static propTypes = {
  41. statusId: PropTypes.string.isRequired,
  42. status: ImmutablePropTypes.map.isRequired,
  43. intl: PropTypes.object.isRequired,
  44. dispatch: PropTypes.func.isRequired,
  45. askReplyConfirmation: PropTypes.bool,
  46. withOpenButton: PropTypes.bool,
  47. onClose: PropTypes.func,
  48. };
  49. _performReply = () => {
  50. const { dispatch, status, onClose } = this.props;
  51. const { router } = this.context;
  52. if (onClose) {
  53. onClose(true);
  54. }
  55. dispatch(replyCompose(status, router.history));
  56. };
  57. handleReplyClick = () => {
  58. const { dispatch, askReplyConfirmation, status, intl } = this.props;
  59. const { signedIn } = this.context.identity;
  60. if (signedIn) {
  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. } else {
  71. dispatch(openModal('INTERACTION', {
  72. type: 'reply',
  73. accountId: status.getIn(['account', 'id']),
  74. url: status.get('url'),
  75. }));
  76. }
  77. };
  78. handleFavouriteClick = () => {
  79. const { dispatch, status } = this.props;
  80. const { signedIn } = this.context.identity;
  81. if (signedIn) {
  82. if (status.get('favourited')) {
  83. dispatch(unfavourite(status));
  84. } else {
  85. dispatch(favourite(status));
  86. }
  87. } else {
  88. dispatch(openModal('INTERACTION', {
  89. type: 'favourite',
  90. accountId: status.getIn(['account', 'id']),
  91. url: status.get('url'),
  92. }));
  93. }
  94. };
  95. _performReblog = (status, privacy) => {
  96. const { dispatch } = this.props;
  97. dispatch(reblog(status, privacy));
  98. };
  99. handleReblogClick = e => {
  100. const { dispatch, status } = this.props;
  101. const { signedIn } = this.context.identity;
  102. if (signedIn) {
  103. if (status.get('reblogged')) {
  104. dispatch(unreblog(status));
  105. } else if ((e && e.shiftKey) || !boostModal) {
  106. this._performReblog(status);
  107. } else {
  108. dispatch(initBoostModal({ status, onReblog: this._performReblog }));
  109. }
  110. } else {
  111. dispatch(openModal('INTERACTION', {
  112. type: 'reblog',
  113. accountId: status.getIn(['account', 'id']),
  114. url: status.get('url'),
  115. }));
  116. }
  117. };
  118. handleOpenClick = e => {
  119. const { router } = this.context;
  120. if (e.button !== 0 || !router) {
  121. return;
  122. }
  123. const { status, onClose } = this.props;
  124. if (onClose) {
  125. onClose();
  126. }
  127. router.history.push(`/@${status.getIn(['account', 'acct'])}/${status.get('id')}`);
  128. };
  129. render () {
  130. const { status, intl, withOpenButton } = this.props;
  131. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  132. const reblogPrivate = status.getIn(['account', 'id']) === me && status.get('visibility') === 'private';
  133. let replyIcon, replyTitle;
  134. if (status.get('in_reply_to_id', null) === null) {
  135. replyIcon = 'reply';
  136. replyTitle = intl.formatMessage(messages.reply);
  137. } else {
  138. replyIcon = 'reply-all';
  139. replyTitle = intl.formatMessage(messages.replyAll);
  140. }
  141. let reblogTitle = '';
  142. if (status.get('reblogged')) {
  143. reblogTitle = intl.formatMessage(messages.cancel_reblog_private);
  144. } else if (publicStatus) {
  145. reblogTitle = intl.formatMessage(messages.reblog);
  146. } else if (reblogPrivate) {
  147. reblogTitle = intl.formatMessage(messages.reblog_private);
  148. } else {
  149. reblogTitle = intl.formatMessage(messages.cannot_reblog);
  150. }
  151. return (
  152. <div className='picture-in-picture__footer'>
  153. <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 />
  154. <IconButton className={classNames('status__action-bar-button', { reblogPrivate })} disabled={!publicStatus && !reblogPrivate} active={status.get('reblogged')} title={reblogTitle} icon='retweet' onClick={this.handleReblogClick} counter={status.get('reblogs_count')} />
  155. <IconButton className='status__action-bar-button star-icon' animate active={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='star' onClick={this.handleFavouriteClick} counter={status.get('favourites_count')} />
  156. {withOpenButton && <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.open)} icon='external-link' onClick={this.handleOpenClick} href={`/@${status.getIn(['account', 'acct'])}/${status.get('id')}`} />}
  157. </div>
  158. );
  159. }
  160. }
  161. export default connect(makeMapStateToProps)(injectIntl(Footer));