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.

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