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.

195 lines
8.0 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 } from 'flavours/glitch/util/initial_state';
  9. import RelativeTimestamp from './relative_timestamp';
  10. const messages = defineMessages({
  11. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  12. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  13. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  14. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  15. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  16. share: { id: 'status.share', defaultMessage: 'Share' },
  17. more: { id: 'status.more', defaultMessage: 'More' },
  18. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  19. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  20. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost to original audience' },
  21. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  22. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  23. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  24. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  25. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  26. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  27. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  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. });
  32. @injectIntl
  33. export default class StatusActionBar extends ImmutablePureComponent {
  34. static contextTypes = {
  35. router: PropTypes.object,
  36. };
  37. static propTypes = {
  38. status: ImmutablePropTypes.map.isRequired,
  39. onReply: PropTypes.func,
  40. onFavourite: PropTypes.func,
  41. onReblog: PropTypes.func,
  42. onDelete: PropTypes.func,
  43. onMention: PropTypes.func,
  44. onMute: PropTypes.func,
  45. onBlock: PropTypes.func,
  46. onReport: PropTypes.func,
  47. onEmbed: PropTypes.func,
  48. onMuteConversation: PropTypes.func,
  49. onPin: PropTypes.func,
  50. onBookmark: PropTypes.func,
  51. withDismiss: PropTypes.bool,
  52. intl: PropTypes.object.isRequired,
  53. };
  54. // Avoid checking props that are functions (and whose equality will always
  55. // evaluate to false. See react-immutable-pure-component for usage.
  56. updateOnProps = [
  57. 'status',
  58. 'withDismiss',
  59. ]
  60. handleReplyClick = () => {
  61. this.props.onReply(this.props.status, this.context.router.history);
  62. }
  63. handleShareClick = () => {
  64. navigator.share({
  65. text: this.props.status.get('search_index'),
  66. url: this.props.status.get('url'),
  67. });
  68. }
  69. handleFavouriteClick = (e) => {
  70. this.props.onFavourite(this.props.status, e);
  71. }
  72. handleBookmarkClick = (e) => {
  73. this.props.onBookmark(this.props.status, e);
  74. }
  75. handleReblogClick = (e) => {
  76. this.props.onReblog(this.props.status, e);
  77. }
  78. handleDeleteClick = () => {
  79. this.props.onDelete(this.props.status);
  80. }
  81. handlePinClick = () => {
  82. this.props.onPin(this.props.status);
  83. }
  84. handleMentionClick = () => {
  85. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  86. }
  87. handleMuteClick = () => {
  88. this.props.onMute(this.props.status.get('account'));
  89. }
  90. handleBlockClick = () => {
  91. this.props.onBlock(this.props.status.get('account'));
  92. }
  93. handleOpen = () => {
  94. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  95. }
  96. handleEmbed = () => {
  97. this.props.onEmbed(this.props.status);
  98. }
  99. handleReport = () => {
  100. this.props.onReport(this.props.status);
  101. }
  102. handleConversationMuteClick = () => {
  103. this.props.onMuteConversation(this.props.status);
  104. }
  105. render () {
  106. const { status, intl, withDismiss } = this.props;
  107. const mutingConversation = status.get('muted');
  108. const anonymousAccess = !me;
  109. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  110. const reblogDisabled = anonymousAccess || (status.get('visibility') === 'direct' || (status.get('visibility') === 'private' && me !== status.getIn(['account', 'id'])));
  111. const reblogMessage = status.get('visibility') === 'private' ? messages.reblog_private : messages.reblog;
  112. let menu = [];
  113. let reblogIcon = 'retweet';
  114. let replyIcon;
  115. let replyTitle;
  116. menu.push({ text: intl.formatMessage(messages.open), action: this.handleOpen });
  117. if (publicStatus) {
  118. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  119. }
  120. menu.push(null);
  121. if (status.getIn(['account', 'id']) === me || withDismiss) {
  122. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  123. menu.push(null);
  124. }
  125. if (status.getIn(['account', 'id']) === me) {
  126. if (publicStatus) {
  127. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  128. }
  129. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  130. } else {
  131. menu.push({ text: intl.formatMessage(messages.mention, { name: status.getIn(['account', 'username']) }), action: this.handleMentionClick });
  132. menu.push(null);
  133. menu.push({ text: intl.formatMessage(messages.mute, { name: status.getIn(['account', 'username']) }), action: this.handleMuteClick });
  134. menu.push({ text: intl.formatMessage(messages.block, { name: status.getIn(['account', 'username']) }), action: this.handleBlockClick });
  135. menu.push({ text: intl.formatMessage(messages.report, { name: status.getIn(['account', 'username']) }), action: this.handleReport });
  136. }
  137. if (status.get('in_reply_to_id', null) === null) {
  138. replyIcon = 'reply';
  139. replyTitle = intl.formatMessage(messages.reply);
  140. } else {
  141. replyIcon = 'reply-all';
  142. replyTitle = intl.formatMessage(messages.replyAll);
  143. }
  144. const shareButton = ('share' in navigator) && status.get('visibility') === 'public' && (
  145. <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShareClick} />
  146. );
  147. return (
  148. <div className='status__action-bar'>
  149. <IconButton className='status__action-bar-button' disabled={anonymousAccess} title={replyTitle} icon={replyIcon} onClick={this.handleReplyClick} />
  150. <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} />
  151. <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} />
  152. {shareButton}
  153. <IconButton className='status__action-bar-button' disabled={anonymousAccess} active={status.get('bookmarked')} pressed={status.get('bookmarked')} title={intl.formatMessage(messages.bookmark)} icon='bookmark' onClick={this.handleBookmarkClick} />
  154. <div className='status__action-bar-dropdown'>
  155. <DropdownMenuContainer disabled={anonymousAccess} status={status} items={menu} icon='ellipsis-h' size={18} direction='right' ariaLabel={intl.formatMessage(messages.more)} />
  156. </div>
  157. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  158. </div>
  159. );
  160. }
  161. }