status_action_bar.js 14 KB

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