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