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.

256 lines
10 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import IconButton from './icon_button';
  5. import DropdownMenuContainer from 'flavours/glitch/containers/dropdown_menu_container';
  6. import { defineMessages, injectIntl } from 'react-intl';
  7. import ImmutablePureComponent from 'react-immutable-pure-component';
  8. import { me, isStaff } from 'flavours/glitch/util/initial_state';
  9. import RelativeTimestamp from './relative_timestamp';
  10. import { accountAdminLink, statusAdminLink } from 'flavours/glitch/util/backend_links';
  11. const messages = defineMessages({
  12. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  13. redraft: { id: 'status.redraft', defaultMessage: 'Delete & re-draft' },
  14. direct: { id: 'status.direct', defaultMessage: 'Direct message @{name}' },
  15. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  16. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  17. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  18. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  19. share: { id: 'status.share', defaultMessage: 'Share' },
  20. more: { id: 'status.more', defaultMessage: 'More' },
  21. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  22. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  23. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost to original audience' },
  24. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  25. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  26. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  27. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  28. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  29. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  30. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  31. pin: { id: 'status.pin', defaultMessage: 'Pin on profile' },
  32. unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' },
  33. embed: { id: 'status.embed', defaultMessage: 'Embed' },
  34. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  35. admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' },
  36. });
  37. const obfuscatedCount = count => {
  38. if (count < 0) {
  39. return 0;
  40. } else if (count <= 1) {
  41. return count;
  42. } else {
  43. return '1+';
  44. }
  45. };
  46. @injectIntl
  47. export default class StatusActionBar extends ImmutablePureComponent {
  48. static contextTypes = {
  49. router: PropTypes.object,
  50. };
  51. static propTypes = {
  52. status: ImmutablePropTypes.map.isRequired,
  53. onReply: PropTypes.func,
  54. onFavourite: PropTypes.func,
  55. onReblog: PropTypes.func,
  56. onDelete: PropTypes.func,
  57. onDirect: PropTypes.func,
  58. onMention: PropTypes.func,
  59. onMute: PropTypes.func,
  60. onBlock: PropTypes.func,
  61. onReport: PropTypes.func,
  62. onEmbed: PropTypes.func,
  63. onMuteConversation: PropTypes.func,
  64. onPin: PropTypes.func,
  65. onBookmark: PropTypes.func,
  66. withDismiss: PropTypes.bool,
  67. showReplyCount: PropTypes.bool,
  68. intl: PropTypes.object.isRequired,
  69. };
  70. // Avoid checking props that are functions (and whose equality will always
  71. // evaluate to false. See react-immutable-pure-component for usage.
  72. updateOnProps = [
  73. 'status',
  74. 'showReplyCount',
  75. 'withDismiss',
  76. ]
  77. handleReplyClick = () => {
  78. this.props.onReply(this.props.status, this.context.router.history);
  79. }
  80. handleShareClick = () => {
  81. navigator.share({
  82. text: this.props.status.get('search_index'),
  83. url: this.props.status.get('url'),
  84. });
  85. }
  86. handleFavouriteClick = (e) => {
  87. this.props.onFavourite(this.props.status, e);
  88. }
  89. handleBookmarkClick = (e) => {
  90. this.props.onBookmark(this.props.status, e);
  91. }
  92. handleReblogClick = (e) => {
  93. this.props.onReblog(this.props.status, e);
  94. }
  95. handleDeleteClick = () => {
  96. this.props.onDelete(this.props.status, this.context.router.history);
  97. }
  98. handleRedraftClick = () => {
  99. this.props.onDelete(this.props.status, this.context.router.history, true);
  100. }
  101. handlePinClick = () => {
  102. this.props.onPin(this.props.status);
  103. }
  104. handleMentionClick = () => {
  105. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  106. }
  107. handleDirectClick = () => {
  108. this.props.onDirect(this.props.status.get('account'), this.context.router.history);
  109. }
  110. handleMuteClick = () => {
  111. this.props.onMute(this.props.status.get('account'));
  112. }
  113. handleBlockClick = () => {
  114. this.props.onBlock(this.props.status.get('account'));
  115. }
  116. handleOpen = () => {
  117. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  118. }
  119. handleEmbed = () => {
  120. this.props.onEmbed(this.props.status);
  121. }
  122. handleReport = () => {
  123. this.props.onReport(this.props.status);
  124. }
  125. handleConversationMuteClick = () => {
  126. this.props.onMuteConversation(this.props.status);
  127. }
  128. render () {
  129. const { status, intl, withDismiss, showReplyCount } = this.props;
  130. const mutingConversation = status.get('muted');
  131. const anonymousAccess = !me;
  132. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  133. const reblogDisabled = anonymousAccess || (status.get('visibility') === 'direct' || (status.get('visibility') === 'private' && me !== status.getIn(['account', 'id'])));
  134. const reblogMessage = status.get('visibility') === 'private' ? messages.reblog_private : messages.reblog;
  135. let menu = [];
  136. let reblogIcon = 'retweet';
  137. let replyIcon;
  138. let replyTitle;
  139. menu.push({ text: intl.formatMessage(messages.open), action: this.handleOpen });
  140. if (publicStatus) {
  141. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  142. }
  143. menu.push(null);
  144. if (status.getIn(['account', 'id']) === me || withDismiss) {
  145. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  146. menu.push(null);
  147. }
  148. if (status.getIn(['account', 'id']) === me) {
  149. if (publicStatus) {
  150. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  151. }
  152. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  153. menu.push({ text: intl.formatMessage(messages.redraft), action: this.handleRedraftClick });
  154. } else {
  155. menu.push({ text: intl.formatMessage(messages.mention, { name: status.getIn(['account', 'username']) }), action: this.handleMentionClick });
  156. menu.push({ text: intl.formatMessage(messages.direct, { name: status.getIn(['account', 'username']) }), action: this.handleDirectClick });
  157. menu.push(null);
  158. menu.push({ text: intl.formatMessage(messages.mute, { name: status.getIn(['account', 'username']) }), action: this.handleMuteClick });
  159. menu.push({ text: intl.formatMessage(messages.block, { name: status.getIn(['account', 'username']) }), action: this.handleBlockClick });
  160. menu.push({ text: intl.formatMessage(messages.report, { name: status.getIn(['account', 'username']) }), action: this.handleReport });
  161. if (isStaff && (accountAdminLink || statusAdminLink)) {
  162. menu.push(null);
  163. if (accountAdminLink !== undefined) {
  164. menu.push({
  165. text: intl.formatMessage(messages.admin_account, { name: status.getIn(['account', 'username']) }),
  166. href: accountAdminLink(status.getIn(['account', 'id'])),
  167. });
  168. }
  169. if (statusAdminLink !== undefined) {
  170. menu.push({
  171. text: intl.formatMessage(messages.admin_status),
  172. href: statusAdminLink(status.getIn(['account', 'id']), status.get('id')),
  173. });
  174. }
  175. }
  176. }
  177. if (status.get('in_reply_to_id', null) === null) {
  178. replyIcon = 'reply';
  179. replyTitle = intl.formatMessage(messages.reply);
  180. } else {
  181. replyIcon = 'reply-all';
  182. replyTitle = intl.formatMessage(messages.replyAll);
  183. }
  184. const shareButton = ('share' in navigator) && status.get('visibility') === 'public' && (
  185. <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShareClick} />
  186. );
  187. let replyButton = (
  188. <IconButton
  189. className='status__action-bar-button'
  190. disabled={anonymousAccess}
  191. title={replyTitle}
  192. icon={replyIcon}
  193. onClick={this.handleReplyClick}
  194. />
  195. );
  196. if (showReplyCount) {
  197. replyButton = (
  198. <div className='status__action-bar__counter'>
  199. {replyButton}
  200. <span className='status__action-bar__counter__label' >{obfuscatedCount(status.get('replies_count'))}</span>
  201. </div>
  202. );
  203. }
  204. return (
  205. <div className='status__action-bar'>
  206. {replyButton}
  207. <IconButton className='status__action-bar-button' disabled={reblogDisabled} active={status.get('reblogged')} pressed={status.get('reblogged')} title={reblogDisabled ? intl.formatMessage(messages.cannot_reblog) : intl.formatMessage(reblogMessage)} icon={reblogIcon} onClick={this.handleReblogClick} />
  208. <IconButton className='status__action-bar-button star-icon' disabled={anonymousAccess} animate active={status.get('favourited')} pressed={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='star' onClick={this.handleFavouriteClick} />
  209. {shareButton}
  210. <IconButton className='status__action-bar-button bookmark-icon' disabled={anonymousAccess} active={status.get('bookmarked')} pressed={status.get('bookmarked')} title={intl.formatMessage(messages.bookmark)} icon='bookmark' onClick={this.handleBookmarkClick} />
  211. <div className='status__action-bar-dropdown'>
  212. <DropdownMenuContainer disabled={anonymousAccess} status={status} items={menu} icon='ellipsis-h' size={18} direction='right' ariaLabel={intl.formatMessage(messages.more)} />
  213. </div>
  214. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  215. </div>
  216. );
  217. }
  218. }