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.

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