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.

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