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.

284 lines
11 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import IconButton from '../../../components/icon_button';
  5. import ImmutablePropTypes from 'react-immutable-proptypes';
  6. import DropdownMenuContainer from '../../../containers/dropdown_menu_container';
  7. import { defineMessages, injectIntl } from 'react-intl';
  8. import { me, isStaff } from '../../../initial_state';
  9. const messages = defineMessages({
  10. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  11. redraft: { id: 'status.redraft', defaultMessage: 'Delete & re-draft' },
  12. direct: { id: 'status.direct', defaultMessage: 'Direct message @{name}' },
  13. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  14. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  15. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  16. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost to original audience' },
  17. cancel_reblog_private: { id: 'status.cancel_reblog_private', defaultMessage: 'Unboost' },
  18. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  19. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  20. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  21. mute: { id: 'status.mute', defaultMessage: 'Mute @{name}' },
  22. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  23. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  24. block: { id: 'status.block', defaultMessage: 'Block @{name}' },
  25. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  26. share: { id: 'status.share', defaultMessage: 'Share' },
  27. pin: { id: 'status.pin', defaultMessage: 'Pin on profile' },
  28. unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' },
  29. embed: { id: 'status.embed', defaultMessage: 'Embed' },
  30. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  31. admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' },
  32. copy: { id: 'status.copy', defaultMessage: 'Copy link to status' },
  33. blockDomain: { id: 'account.block_domain', defaultMessage: 'Hide everything from {domain}' },
  34. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unhide {domain}' },
  35. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  36. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  37. });
  38. const mapStateToProps = (state, { status }) => ({
  39. relationship: state.getIn(['relationships', status.getIn(['account', 'id'])]),
  40. });
  41. export default @connect(mapStateToProps)
  42. @injectIntl
  43. class ActionBar extends React.PureComponent {
  44. static contextTypes = {
  45. router: PropTypes.object,
  46. };
  47. static propTypes = {
  48. status: ImmutablePropTypes.map.isRequired,
  49. relationship: ImmutablePropTypes.map,
  50. onReply: PropTypes.func.isRequired,
  51. onReblog: PropTypes.func.isRequired,
  52. onFavourite: PropTypes.func.isRequired,
  53. onBookmark: PropTypes.func.isRequired,
  54. onDelete: PropTypes.func.isRequired,
  55. onDirect: PropTypes.func.isRequired,
  56. onMention: PropTypes.func.isRequired,
  57. onMute: PropTypes.func,
  58. onUnmute: PropTypes.func,
  59. onBlock: PropTypes.func,
  60. onUnblock: PropTypes.func,
  61. onBlockDomain: PropTypes.func,
  62. onUnblockDomain: PropTypes.func,
  63. onMuteConversation: PropTypes.func,
  64. onReport: PropTypes.func,
  65. onPin: PropTypes.func,
  66. onEmbed: PropTypes.func,
  67. intl: PropTypes.object.isRequired,
  68. };
  69. handleReplyClick = () => {
  70. this.props.onReply(this.props.status);
  71. }
  72. handleReblogClick = (e) => {
  73. this.props.onReblog(this.props.status, e);
  74. }
  75. handleFavouriteClick = () => {
  76. this.props.onFavourite(this.props.status);
  77. }
  78. handleBookmarkClick = (e) => {
  79. this.props.onBookmark(this.props.status, e);
  80. }
  81. handleDeleteClick = () => {
  82. this.props.onDelete(this.props.status, this.context.router.history);
  83. }
  84. handleRedraftClick = () => {
  85. this.props.onDelete(this.props.status, this.context.router.history, true);
  86. }
  87. handleDirectClick = () => {
  88. this.props.onDirect(this.props.status.get('account'), this.context.router.history);
  89. }
  90. handleMentionClick = () => {
  91. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  92. }
  93. handleMuteClick = () => {
  94. const { status, relationship, onMute, onUnmute } = this.props;
  95. const account = status.get('account');
  96. if (relationship && relationship.get('muting')) {
  97. onUnmute(account);
  98. } else {
  99. onMute(account);
  100. }
  101. }
  102. handleBlockClick = () => {
  103. const { status, relationship, onBlock, onUnblock } = this.props;
  104. const account = status.get('account');
  105. if (relationship && relationship.get('blocking')) {
  106. onUnblock(account);
  107. } else {
  108. onBlock(status);
  109. }
  110. }
  111. handleBlockDomain = () => {
  112. const { status, onBlockDomain } = this.props;
  113. const account = status.get('account');
  114. onBlockDomain(account.get('acct').split('@')[1]);
  115. }
  116. handleUnblockDomain = () => {
  117. const { status, onUnblockDomain } = this.props;
  118. const account = status.get('account');
  119. onUnblockDomain(account.get('acct').split('@')[1]);
  120. }
  121. handleConversationMuteClick = () => {
  122. this.props.onMuteConversation(this.props.status);
  123. }
  124. handleReport = () => {
  125. this.props.onReport(this.props.status);
  126. }
  127. handlePinClick = () => {
  128. this.props.onPin(this.props.status);
  129. }
  130. handleShare = () => {
  131. navigator.share({
  132. text: this.props.status.get('search_index'),
  133. url: this.props.status.get('url'),
  134. });
  135. }
  136. handleEmbed = () => {
  137. this.props.onEmbed(this.props.status);
  138. }
  139. handleCopy = () => {
  140. const url = this.props.status.get('url');
  141. const textarea = document.createElement('textarea');
  142. textarea.textContent = url;
  143. textarea.style.position = 'fixed';
  144. document.body.appendChild(textarea);
  145. try {
  146. textarea.select();
  147. document.execCommand('copy');
  148. } catch (e) {
  149. } finally {
  150. document.body.removeChild(textarea);
  151. }
  152. }
  153. render () {
  154. const { status, relationship, intl } = this.props;
  155. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  156. const mutingConversation = status.get('muted');
  157. const account = status.get('account');
  158. let menu = [];
  159. if (publicStatus) {
  160. menu.push({ text: intl.formatMessage(messages.copy), action: this.handleCopy });
  161. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  162. menu.push(null);
  163. }
  164. if (me === status.getIn(['account', 'id'])) {
  165. if (publicStatus) {
  166. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  167. } else {
  168. if (status.get('visibility') === 'private') {
  169. menu.push({ text: intl.formatMessage(status.get('reblogged') ? messages.cancel_reblog_private : messages.reblog_private), action: this.handleReblogClick });
  170. }
  171. }
  172. menu.push(null);
  173. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  174. menu.push(null);
  175. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  176. menu.push({ text: intl.formatMessage(messages.redraft), action: this.handleRedraftClick });
  177. } else {
  178. menu.push({ text: intl.formatMessage(messages.mention, { name: status.getIn(['account', 'username']) }), action: this.handleMentionClick });
  179. menu.push({ text: intl.formatMessage(messages.direct, { name: status.getIn(['account', 'username']) }), action: this.handleDirectClick });
  180. menu.push(null);
  181. if (relationship && relationship.get('muting')) {
  182. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.handleMuteClick });
  183. } else {
  184. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.handleMuteClick });
  185. }
  186. if (relationship && relationship.get('blocking')) {
  187. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.handleBlockClick });
  188. } else {
  189. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.handleBlockClick });
  190. }
  191. menu.push({ text: intl.formatMessage(messages.report, { name: status.getIn(['account', 'username']) }), action: this.handleReport });
  192. if (account.get('acct') !== account.get('username')) {
  193. const domain = account.get('acct').split('@')[1];
  194. menu.push(null);
  195. if (relationship && relationship.get('domain_blocking')) {
  196. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.handleUnblockDomain });
  197. } else {
  198. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.handleBlockDomain });
  199. }
  200. }
  201. if (isStaff) {
  202. menu.push(null);
  203. menu.push({ text: intl.formatMessage(messages.admin_account, { name: status.getIn(['account', 'username']) }), href: `/admin/accounts/${status.getIn(['account', 'id'])}` });
  204. menu.push({ text: intl.formatMessage(messages.admin_status), href: `/admin/accounts/${status.getIn(['account', 'id'])}/statuses/${status.get('id')}` });
  205. }
  206. }
  207. const shareButton = ('share' in navigator) && status.get('visibility') === 'public' && (
  208. <div className='detailed-status__button'><IconButton title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShare} /></div>
  209. );
  210. let replyIcon;
  211. if (status.get('in_reply_to_id', null) === null) {
  212. replyIcon = 'reply';
  213. } else {
  214. replyIcon = 'reply-all';
  215. }
  216. let reblogIcon = 'retweet';
  217. if (status.get('visibility') === 'direct') reblogIcon = 'envelope';
  218. else if (status.get('visibility') === 'private') reblogIcon = 'lock';
  219. let reblog_disabled = (status.get('visibility') === 'direct' || status.get('visibility') === 'private');
  220. return (
  221. <div className='detailed-status__action-bar'>
  222. <div className='detailed-status__button'><IconButton title={intl.formatMessage(messages.reply)} icon={status.get('in_reply_to_account_id') === status.getIn(['account', 'id']) ? 'reply' : replyIcon} onClick={this.handleReplyClick} /></div>
  223. <div className='detailed-status__button'><IconButton disabled={reblog_disabled} active={status.get('reblogged')} title={reblog_disabled ? intl.formatMessage(messages.cannot_reblog) : intl.formatMessage(messages.reblog)} icon={reblogIcon} onClick={this.handleReblogClick} /></div>
  224. <div className='detailed-status__button'><IconButton className='star-icon' animate active={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='star' onClick={this.handleFavouriteClick} /></div>
  225. {shareButton}
  226. <div className='detailed-status__button'><IconButton className='bookmark-icon' active={status.get('bookmarked')} title={intl.formatMessage(messages.bookmark)} icon='bookmark' onClick={this.handleBookmarkClick} /></div>
  227. <div className='detailed-status__action-bar-dropdown'>
  228. <DropdownMenuContainer size={18} icon='ellipsis-h' status={status} items={menu} direction='left' title='More' />
  229. </div>
  230. </div>
  231. );
  232. }
  233. }