status_action_bar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import IconButton from './icon_button';
  6. import DropdownMenuContainer from '../containers/dropdown_menu_container';
  7. import { defineMessages, injectIntl } from 'react-intl';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import { me } from '../initial_state';
  10. import classNames from 'classnames';
  11. import { PERMISSION_MANAGE_USERS } from 'mastodon/permissions';
  12. const messages = defineMessages({
  13. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  14. redraft: { id: 'status.redraft', defaultMessage: 'Delete & re-draft' },
  15. edit: { id: 'status.edit', defaultMessage: 'Edit' },
  16. direct: { id: 'status.direct', defaultMessage: 'Direct message @{name}' },
  17. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  18. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  19. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  20. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  21. share: { id: 'status.share', defaultMessage: 'Share' },
  22. more: { id: 'status.more', defaultMessage: 'More' },
  23. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  24. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  25. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost with original visibility' },
  26. cancel_reblog_private: { id: 'status.cancel_reblog_private', defaultMessage: 'Unboost' },
  27. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  28. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  29. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  30. removeBookmark: { id: 'status.remove_bookmark', defaultMessage: 'Remove bookmark' },
  31. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  32. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  33. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  34. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  35. pin: { id: 'status.pin', defaultMessage: 'Pin on profile' },
  36. unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' },
  37. embed: { id: 'status.embed', defaultMessage: 'Embed' },
  38. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  39. admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' },
  40. copy: { id: 'status.copy', defaultMessage: 'Copy link to status' },
  41. hide: { id: 'status.hide', defaultMessage: 'Hide toot' },
  42. blockDomain: { id: 'account.block_domain', defaultMessage: 'Block domain {domain}' },
  43. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unblock domain {domain}' },
  44. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  45. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  46. filter: { id: 'status.filter', defaultMessage: 'Filter this post' },
  47. });
  48. const mapStateToProps = (state, { status }) => ({
  49. relationship: state.getIn(['relationships', status.getIn(['account', 'id'])]),
  50. });
  51. export default @connect(mapStateToProps)
  52. @injectIntl
  53. class StatusActionBar extends ImmutablePureComponent {
  54. static contextTypes = {
  55. router: PropTypes.object,
  56. identity: PropTypes.object,
  57. };
  58. static propTypes = {
  59. status: ImmutablePropTypes.map.isRequired,
  60. relationship: ImmutablePropTypes.map,
  61. onReply: PropTypes.func,
  62. onFavourite: PropTypes.func,
  63. onReblog: PropTypes.func,
  64. onDelete: PropTypes.func,
  65. onDirect: PropTypes.func,
  66. onMention: PropTypes.func,
  67. onMute: PropTypes.func,
  68. onUnmute: PropTypes.func,
  69. onBlock: PropTypes.func,
  70. onUnblock: PropTypes.func,
  71. onBlockDomain: PropTypes.func,
  72. onUnblockDomain: PropTypes.func,
  73. onReport: PropTypes.func,
  74. onEmbed: PropTypes.func,
  75. onMuteConversation: PropTypes.func,
  76. onPin: PropTypes.func,
  77. onBookmark: PropTypes.func,
  78. onFilter: PropTypes.func,
  79. onAddFilter: PropTypes.func,
  80. onInteractionModal: PropTypes.func,
  81. withDismiss: PropTypes.bool,
  82. withCounters: PropTypes.bool,
  83. scrollKey: PropTypes.string,
  84. intl: PropTypes.object.isRequired,
  85. };
  86. // Avoid checking props that are functions (and whose equality will always
  87. // evaluate to false. See react-immutable-pure-component for usage.
  88. updateOnProps = [
  89. 'status',
  90. 'relationship',
  91. 'withDismiss',
  92. ]
  93. handleReplyClick = () => {
  94. const { signedIn } = this.context.identity;
  95. if (signedIn) {
  96. this.props.onReply(this.props.status, this.context.router.history);
  97. } else {
  98. this.props.onInteractionModal('reply', this.props.status);
  99. }
  100. }
  101. handleShareClick = () => {
  102. navigator.share({
  103. text: this.props.status.get('search_index'),
  104. url: this.props.status.get('url'),
  105. }).catch((e) => {
  106. if (e.name !== 'AbortError') console.error(e);
  107. });
  108. }
  109. handleFavouriteClick = () => {
  110. const { signedIn } = this.context.identity;
  111. if (signedIn) {
  112. this.props.onFavourite(this.props.status);
  113. } else {
  114. this.props.onInteractionModal('favourite', this.props.status);
  115. }
  116. }
  117. handleReblogClick = e => {
  118. const { signedIn } = this.context.identity;
  119. if (signedIn) {
  120. this.props.onReblog(this.props.status, e);
  121. } else {
  122. this.props.onInteractionModal('reblog', this.props.status);
  123. }
  124. }
  125. handleBookmarkClick = () => {
  126. this.props.onBookmark(this.props.status);
  127. }
  128. handleDeleteClick = () => {
  129. this.props.onDelete(this.props.status, this.context.router.history);
  130. }
  131. handleRedraftClick = () => {
  132. this.props.onDelete(this.props.status, this.context.router.history, true);
  133. }
  134. handleEditClick = () => {
  135. this.props.onEdit(this.props.status, this.context.router.history);
  136. }
  137. handlePinClick = () => {
  138. this.props.onPin(this.props.status);
  139. }
  140. handleMentionClick = () => {
  141. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  142. }
  143. handleDirectClick = () => {
  144. this.props.onDirect(this.props.status.get('account'), this.context.router.history);
  145. }
  146. handleMuteClick = () => {
  147. const { status, relationship, onMute, onUnmute } = this.props;
  148. const account = status.get('account');
  149. if (relationship && relationship.get('muting')) {
  150. onUnmute(account);
  151. } else {
  152. onMute(account);
  153. }
  154. }
  155. handleBlockClick = () => {
  156. const { status, relationship, onBlock, onUnblock } = this.props;
  157. const account = status.get('account');
  158. if (relationship && relationship.get('blocking')) {
  159. onUnblock(account);
  160. } else {
  161. onBlock(status);
  162. }
  163. }
  164. handleBlockDomain = () => {
  165. const { status, onBlockDomain } = this.props;
  166. const account = status.get('account');
  167. onBlockDomain(account.get('acct').split('@')[1]);
  168. }
  169. handleUnblockDomain = () => {
  170. const { status, onUnblockDomain } = this.props;
  171. const account = status.get('account');
  172. onUnblockDomain(account.get('acct').split('@')[1]);
  173. }
  174. handleOpen = () => {
  175. this.context.router.history.push(`/@${this.props.status.getIn(['account', 'acct'])}/${this.props.status.get('id')}`);
  176. }
  177. handleEmbed = () => {
  178. this.props.onEmbed(this.props.status);
  179. }
  180. handleReport = () => {
  181. this.props.onReport(this.props.status);
  182. }
  183. handleConversationMuteClick = () => {
  184. this.props.onMuteConversation(this.props.status);
  185. }
  186. handleFilterClick = () => {
  187. this.props.onAddFilter(this.props.status);
  188. }
  189. handleCopy = () => {
  190. const url = this.props.status.get('url');
  191. const textarea = document.createElement('textarea');
  192. textarea.textContent = url;
  193. textarea.style.position = 'fixed';
  194. document.body.appendChild(textarea);
  195. try {
  196. textarea.select();
  197. document.execCommand('copy');
  198. } catch (e) {
  199. } finally {
  200. document.body.removeChild(textarea);
  201. }
  202. }
  203. handleHideClick = () => {
  204. this.props.onFilter();
  205. }
  206. render () {
  207. const { status, relationship, intl, withDismiss, withCounters, scrollKey } = this.props;
  208. const { signedIn } = this.context.identity;
  209. const anonymousAccess = !signedIn;
  210. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  211. const pinnableStatus = ['public', 'unlisted', 'private'].includes(status.get('visibility'));
  212. const mutingConversation = status.get('muted');
  213. const account = status.get('account');
  214. const writtenByMe = status.getIn(['account', 'id']) === me;
  215. let menu = [];
  216. menu.push({ text: intl.formatMessage(messages.open), action: this.handleOpen });
  217. if (publicStatus) {
  218. menu.push({ text: intl.formatMessage(messages.copy), action: this.handleCopy });
  219. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  220. }
  221. menu.push(null);
  222. menu.push({ text: intl.formatMessage(status.get('bookmarked') ? messages.removeBookmark : messages.bookmark), action: this.handleBookmarkClick });
  223. if (writtenByMe && pinnableStatus) {
  224. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  225. }
  226. menu.push(null);
  227. if (writtenByMe || withDismiss) {
  228. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  229. menu.push(null);
  230. }
  231. if (writtenByMe) {
  232. menu.push({ text: intl.formatMessage(messages.edit), action: this.handleEditClick });
  233. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  234. menu.push({ text: intl.formatMessage(messages.redraft), action: this.handleRedraftClick });
  235. } else {
  236. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.handleMentionClick });
  237. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.handleDirectClick });
  238. menu.push(null);
  239. if (relationship && relationship.get('muting')) {
  240. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.handleMuteClick });
  241. } else {
  242. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.handleMuteClick });
  243. }
  244. if (relationship && relationship.get('blocking')) {
  245. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.handleBlockClick });
  246. } else {
  247. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.handleBlockClick });
  248. }
  249. if (!this.props.onFilter) {
  250. menu.push(null);
  251. menu.push({ text: intl.formatMessage(messages.filter), action: this.handleFilterClick });
  252. menu.push(null);
  253. }
  254. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.handleReport });
  255. if (account.get('acct') !== account.get('username')) {
  256. const domain = account.get('acct').split('@')[1];
  257. menu.push(null);
  258. if (relationship && relationship.get('domain_blocking')) {
  259. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.handleUnblockDomain });
  260. } else {
  261. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.handleBlockDomain });
  262. }
  263. }
  264. if ((this.context.identity.permissions & PERMISSION_MANAGE_USERS) === PERMISSION_MANAGE_USERS) {
  265. menu.push(null);
  266. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${status.getIn(['account', 'id'])}` });
  267. menu.push({ text: intl.formatMessage(messages.admin_status), href: `/admin/accounts/${status.getIn(['account', 'id'])}/statuses/${status.get('id')}` });
  268. }
  269. }
  270. let replyIcon;
  271. let replyTitle;
  272. if (status.get('in_reply_to_id', null) === null) {
  273. replyIcon = 'reply';
  274. replyTitle = intl.formatMessage(messages.reply);
  275. } else {
  276. replyIcon = 'reply-all';
  277. replyTitle = intl.formatMessage(messages.replyAll);
  278. }
  279. const reblogPrivate = status.getIn(['account', 'id']) === me && status.get('visibility') === 'private';
  280. let reblogTitle = '';
  281. if (status.get('reblogged')) {
  282. reblogTitle = intl.formatMessage(messages.cancel_reblog_private);
  283. } else if (publicStatus) {
  284. reblogTitle = intl.formatMessage(messages.reblog);
  285. } else if (reblogPrivate) {
  286. reblogTitle = intl.formatMessage(messages.reblog_private);
  287. } else {
  288. reblogTitle = intl.formatMessage(messages.cannot_reblog);
  289. }
  290. const shareButton = ('share' in navigator) && publicStatus && (
  291. <IconButton className='status__action-bar__button' title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShareClick} />
  292. );
  293. const filterButton = this.props.onFilter && (
  294. <IconButton className='status__action-bar__button' title={intl.formatMessage(messages.hide)} icon='eye' onClick={this.handleHideClick} />
  295. );
  296. return (
  297. <div className='status__action-bar'>
  298. <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 />
  299. <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={withCounters ? status.get('reblogs_count') : undefined} />
  300. <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={withCounters ? status.get('favourites_count') : undefined} />
  301. <IconButton className='status__action-bar__button bookmark-icon' disabled={!signedIn} active={status.get('bookmarked')} title={intl.formatMessage(messages.bookmark)} icon='bookmark' onClick={this.handleBookmarkClick} />
  302. {shareButton}
  303. {filterButton}
  304. <div className='status__action-bar__dropdown'>
  305. <DropdownMenuContainer
  306. scrollKey={scrollKey}
  307. disabled={anonymousAccess}
  308. status={status}
  309. items={menu}
  310. icon='ellipsis-h'
  311. size={18}
  312. direction='right'
  313. title={intl.formatMessage(messages.more)}
  314. />
  315. </div>
  316. </div>
  317. );
  318. }
  319. }