status.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Avatar from './avatar';
  5. import AvatarOverlay from './avatar_overlay';
  6. import RelativeTimestamp from './relative_timestamp';
  7. import DisplayName from './display_name';
  8. import StatusContent from './status_content';
  9. import StatusActionBar from './status_action_bar';
  10. import AttachmentList from './attachment_list';
  11. import { FormattedMessage } from 'react-intl';
  12. import ImmutablePureComponent from 'react-immutable-pure-component';
  13. import { MediaGallery, Video } from '../features/ui/util/async-components';
  14. import { HotKeys } from 'react-hotkeys';
  15. import classNames from 'classnames';
  16. // We use the component (and not the container) since we do not want
  17. // to use the progress bar to show download progress
  18. import Bundle from '../features/ui/components/bundle';
  19. export default class Status extends ImmutablePureComponent {
  20. static contextTypes = {
  21. router: PropTypes.object,
  22. };
  23. static propTypes = {
  24. status: ImmutablePropTypes.map,
  25. account: ImmutablePropTypes.map,
  26. onReply: PropTypes.func,
  27. onFavourite: PropTypes.func,
  28. onReblog: PropTypes.func,
  29. onDelete: PropTypes.func,
  30. onPin: PropTypes.func,
  31. onOpenMedia: PropTypes.func,
  32. onOpenVideo: PropTypes.func,
  33. onBlock: PropTypes.func,
  34. onEmbed: PropTypes.func,
  35. onHeightChange: PropTypes.func,
  36. onToggleHidden: PropTypes.func,
  37. muted: PropTypes.bool,
  38. hidden: PropTypes.bool,
  39. onMoveUp: PropTypes.func,
  40. onMoveDown: PropTypes.func,
  41. };
  42. // Avoid checking props that are functions (and whose equality will always
  43. // evaluate to false. See react-immutable-pure-component for usage.
  44. updateOnProps = [
  45. 'status',
  46. 'account',
  47. 'muted',
  48. 'hidden',
  49. ]
  50. handleClick = () => {
  51. if (!this.context.router) {
  52. return;
  53. }
  54. const { status } = this.props;
  55. this.context.router.history.push(`/statuses/${status.getIn(['reblog', 'id'], status.get('id'))}`);
  56. }
  57. handleAccountClick = (e) => {
  58. if (this.context.router && e.button === 0) {
  59. const id = e.currentTarget.getAttribute('data-id');
  60. e.preventDefault();
  61. this.context.router.history.push(`/accounts/${id}`);
  62. }
  63. }
  64. handleExpandedToggle = () => {
  65. this.props.onToggleHidden(this._properStatus());
  66. };
  67. renderLoadingMediaGallery () {
  68. return <div className='media_gallery' style={{ height: '110px' }} />;
  69. }
  70. renderLoadingVideoPlayer () {
  71. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  72. }
  73. handleOpenVideo = startTime => {
  74. this.props.onOpenVideo(this._properStatus().getIn(['media_attachments', 0]), startTime);
  75. }
  76. handleHotkeyReply = e => {
  77. e.preventDefault();
  78. this.props.onReply(this._properStatus(), this.context.router.history);
  79. }
  80. handleHotkeyFavourite = () => {
  81. this.props.onFavourite(this._properStatus());
  82. }
  83. handleHotkeyBoost = e => {
  84. this.props.onReblog(this._properStatus(), e);
  85. }
  86. handleHotkeyMention = e => {
  87. e.preventDefault();
  88. this.props.onMention(this._properStatus().get('account'), this.context.router.history);
  89. }
  90. handleHotkeyOpen = () => {
  91. this.context.router.history.push(`/statuses/${this._properStatus().get('id')}`);
  92. }
  93. handleHotkeyOpenProfile = () => {
  94. this.context.router.history.push(`/accounts/${this._properStatus().getIn(['account', 'id'])}`);
  95. }
  96. handleHotkeyMoveUp = () => {
  97. this.props.onMoveUp(this.props.status.get('id'));
  98. }
  99. handleHotkeyMoveDown = () => {
  100. this.props.onMoveDown(this.props.status.get('id'));
  101. }
  102. _properStatus () {
  103. const { status } = this.props;
  104. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  105. return status.get('reblog');
  106. } else {
  107. return status;
  108. }
  109. }
  110. render () {
  111. let media = null;
  112. let statusAvatar, prepend;
  113. const { hidden, featured } = this.props;
  114. let { status, account, ...other } = this.props;
  115. if (status === null) {
  116. return null;
  117. }
  118. if (hidden) {
  119. return (
  120. <div>
  121. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  122. {status.get('content')}
  123. </div>
  124. );
  125. }
  126. if (featured) {
  127. prepend = (
  128. <div className='status__prepend'>
  129. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-thumb-tack status__prepend-icon' /></div>
  130. <FormattedMessage id='status.pinned' defaultMessage='Pinned toot' />
  131. </div>
  132. );
  133. } else if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  134. const display_name_html = { __html: status.getIn(['account', 'display_name_html']) };
  135. prepend = (
  136. <div className='status__prepend'>
  137. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  138. <FormattedMessage id='status.reblogged_by' defaultMessage='{name} boosted' values={{ name: <a onClick={this.handleAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name muted'><bdi><strong dangerouslySetInnerHTML={display_name_html} /></bdi></a> }} />
  139. </div>
  140. );
  141. account = status.get('account');
  142. status = status.get('reblog');
  143. }
  144. if (status.get('media_attachments').size > 0) {
  145. if (this.props.muted || status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  146. media = (
  147. <AttachmentList
  148. compact
  149. media={status.get('media_attachments')}
  150. />
  151. );
  152. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  153. const video = status.getIn(['media_attachments', 0]);
  154. media = (
  155. <Bundle fetchComponent={Video} loading={this.renderLoadingVideoPlayer} >
  156. {Component => (
  157. <Component
  158. preview={video.get('preview_url')}
  159. src={video.get('url')}
  160. width={239}
  161. height={110}
  162. inline
  163. sensitive={status.get('sensitive')}
  164. onOpenVideo={this.handleOpenVideo}
  165. />
  166. )}
  167. </Bundle>
  168. );
  169. } else {
  170. media = (
  171. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} >
  172. {Component => <Component media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} />}
  173. </Bundle>
  174. );
  175. }
  176. }
  177. if (account === undefined || account === null) {
  178. statusAvatar = <Avatar account={status.get('account')} size={48} />;
  179. }else{
  180. statusAvatar = <AvatarOverlay account={status.get('account')} friend={account} />;
  181. }
  182. const handlers = this.props.muted ? {} : {
  183. reply: this.handleHotkeyReply,
  184. favourite: this.handleHotkeyFavourite,
  185. boost: this.handleHotkeyBoost,
  186. mention: this.handleHotkeyMention,
  187. open: this.handleHotkeyOpen,
  188. openProfile: this.handleHotkeyOpenProfile,
  189. moveUp: this.handleHotkeyMoveUp,
  190. moveDown: this.handleHotkeyMoveDown,
  191. };
  192. return (
  193. <HotKeys handlers={handlers}>
  194. <div className={classNames('status__wrapper', `status__wrapper-${status.get('visibility')}`, { focusable: !this.props.muted })} tabIndex={this.props.muted ? null : 0}>
  195. {prepend}
  196. <div className={classNames('status', `status-${status.get('visibility')}`, { muted: this.props.muted })} data-id={status.get('id')}>
  197. <div className='status__info'>
  198. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  199. <a onClick={this.handleAccountClick} target='_blank' data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} title={status.getIn(['account', 'acct'])} className='status__display-name'>
  200. <div className='status__avatar'>
  201. {statusAvatar}
  202. </div>
  203. <DisplayName account={status.get('account')} />
  204. </a>
  205. </div>
  206. <StatusContent status={status} onClick={this.handleClick} expanded={!status.get('hidden')} onExpandedToggle={this.handleExpandedToggle} />
  207. {media}
  208. <StatusActionBar status={status} account={account} {...other} />
  209. </div>
  210. </div>
  211. </HotKeys>
  212. );
  213. }
  214. }