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.

258 lines
9.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  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 { FormattedMessage } from 'react-intl';
  11. import emojify from '../emoji';
  12. import escapeTextContentForBrowser from 'escape-html';
  13. import ImmutablePureComponent from 'react-immutable-pure-component';
  14. import scheduleIdleTask from '../features/ui/util/schedule_idle_task';
  15. import { MediaGallery, VideoPlayer } from '../features/ui/util/async-components';
  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. wrapped: PropTypes.bool,
  27. onReply: PropTypes.func,
  28. onFavourite: PropTypes.func,
  29. onReblog: PropTypes.func,
  30. onDelete: PropTypes.func,
  31. onOpenMedia: PropTypes.func,
  32. onOpenVideo: PropTypes.func,
  33. onBlock: PropTypes.func,
  34. me: PropTypes.number,
  35. boostModal: PropTypes.bool,
  36. autoPlayGif: PropTypes.bool,
  37. muted: PropTypes.bool,
  38. intersectionObserverWrapper: PropTypes.object,
  39. };
  40. state = {
  41. isExpanded: false,
  42. isIntersecting: true, // assume intersecting until told otherwise
  43. isHidden: false, // set to true in requestIdleCallback to trigger un-render
  44. }
  45. // Avoid checking props that are functions (and whose equality will always
  46. // evaluate to false. See react-immutable-pure-component for usage.
  47. updateOnProps = [
  48. 'status',
  49. 'account',
  50. 'wrapped',
  51. 'me',
  52. 'boostModal',
  53. 'autoPlayGif',
  54. 'muted',
  55. ]
  56. updateOnStates = ['isExpanded']
  57. shouldComponentUpdate (nextProps, nextState) {
  58. if (!nextState.isIntersecting && nextState.isHidden) {
  59. // It's only if we're not intersecting (i.e. offscreen) and isHidden is true
  60. // that either "isIntersecting" or "isHidden" matter, and then they're
  61. // the only things that matter.
  62. return this.state.isIntersecting || !this.state.isHidden;
  63. } else if (nextState.isIntersecting && !this.state.isIntersecting) {
  64. // If we're going from a non-intersecting state to an intersecting state,
  65. // (i.e. offscreen to onscreen), then we definitely need to re-render
  66. return true;
  67. }
  68. // Otherwise, diff based on "updateOnProps" and "updateOnStates"
  69. return super.shouldComponentUpdate(nextProps, nextState);
  70. }
  71. componentDidMount () {
  72. if (!this.props.intersectionObserverWrapper) {
  73. // TODO: enable IntersectionObserver optimization for notification statuses.
  74. // These are managed in notifications/index.js rather than status_list.js
  75. return;
  76. }
  77. this.props.intersectionObserverWrapper.observe(
  78. this.props.id,
  79. this.node,
  80. this.handleIntersection
  81. );
  82. this.componentMounted = true;
  83. }
  84. componentWillUnmount () {
  85. if (this.props.intersectionObserverWrapper) {
  86. this.props.intersectionObserverWrapper.unobserve(this.props.id, this.node);
  87. }
  88. this.componentMounted = false;
  89. }
  90. handleIntersection = (entry) => {
  91. // Edge 15 doesn't support isIntersecting, but we can infer it
  92. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12156111/
  93. // https://github.com/WICG/IntersectionObserver/issues/211
  94. const isIntersecting = (typeof entry.isIntersecting === 'boolean') ?
  95. entry.isIntersecting : entry.intersectionRect.height > 0;
  96. this.setState((prevState) => {
  97. if (prevState.isIntersecting && !isIntersecting) {
  98. scheduleIdleTask(this.hideIfNotIntersecting);
  99. }
  100. return {
  101. isIntersecting: isIntersecting,
  102. isHidden: false,
  103. };
  104. });
  105. }
  106. hideIfNotIntersecting = () => {
  107. if (!this.componentMounted) {
  108. return;
  109. }
  110. // When the browser gets a chance, test if we're still not intersecting,
  111. // and if so, set our isHidden to true to trigger an unrender. The point of
  112. // this is to save DOM nodes and avoid using up too much memory.
  113. // See: https://github.com/tootsuite/mastodon/issues/2900
  114. this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }));
  115. }
  116. saveHeight = () => {
  117. if (this.node && this.node.children.length !== 0) {
  118. this.height = this.node.getBoundingClientRect().height;
  119. }
  120. }
  121. handleRef = (node) => {
  122. this.node = node;
  123. this.saveHeight();
  124. }
  125. handleClick = () => {
  126. const { status } = this.props;
  127. this.context.router.history.push(`/statuses/${status.getIn(['reblog', 'id'], status.get('id'))}`);
  128. }
  129. handleAccountClick = (e) => {
  130. if (e.button === 0) {
  131. const id = Number(e.currentTarget.getAttribute('data-id'));
  132. e.preventDefault();
  133. this.context.router.history.push(`/accounts/${id}`);
  134. }
  135. }
  136. handleExpandedToggle = () => {
  137. this.setState({ isExpanded: !this.state.isExpanded });
  138. };
  139. renderLoadingMediaGallery () {
  140. return <div className='media_gallery' style={{ height: '110px' }} />;
  141. }
  142. renderLoadingVideoPlayer () {
  143. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  144. }
  145. render () {
  146. let media = null;
  147. let statusAvatar;
  148. // Exclude intersectionObserverWrapper from `other` variable
  149. // because intersection is managed in here.
  150. const { status, account, intersectionObserverWrapper, ...other } = this.props;
  151. const { isExpanded, isIntersecting, isHidden } = this.state;
  152. if (status === null) {
  153. return null;
  154. }
  155. if (!isIntersecting && isHidden) {
  156. return (
  157. <div ref={this.handleRef} data-id={status.get('id')} style={{ height: `${this.height}px`, opacity: 0, overflow: 'hidden' }}>
  158. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  159. {status.get('content')}
  160. </div>
  161. );
  162. }
  163. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  164. let displayName = status.getIn(['account', 'display_name']);
  165. if (displayName.length === 0) {
  166. displayName = status.getIn(['account', 'username']);
  167. }
  168. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  169. return (
  170. <div className='status__wrapper' ref={this.handleRef} data-id={status.get('id')} >
  171. <div className='status__prepend'>
  172. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  173. <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'><strong dangerouslySetInnerHTML={displayNameHTML} /></a> }} />
  174. </div>
  175. <Status {...other} wrapped status={status.get('reblog')} account={status.get('account')} />
  176. </div>
  177. );
  178. }
  179. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  180. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  181. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  182. media = (
  183. <Bundle fetchComponent={VideoPlayer} loading={this.renderLoadingVideoPlayer} onRender={this.saveHeight} >
  184. {Component => <Component media={status.getIn(['media_attachments', 0])} sensitive={status.get('sensitive')} onOpenVideo={this.props.onOpenVideo} />}
  185. </Bundle>
  186. );
  187. } else {
  188. media = (
  189. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} onRender={this.saveHeight} >
  190. {Component => <Component media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} autoPlayGif={this.props.autoPlayGif} />}
  191. </Bundle>
  192. );
  193. }
  194. }
  195. if (account === undefined || account === null) {
  196. statusAvatar = <Avatar src={status.getIn(['account', 'avatar'])} staticSrc={status.getIn(['account', 'avatar_static'])} size={48} />;
  197. }else{
  198. statusAvatar = <AvatarOverlay staticSrc={status.getIn(['account', 'avatar_static'])} overlaySrc={account.get('avatar_static')} />;
  199. }
  200. return (
  201. <div className={`status ${this.props.muted ? 'muted' : ''} status-${status.get('visibility')}`} data-id={status.get('id')} ref={this.handleRef}>
  202. <div className='status__info'>
  203. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  204. <a onClick={this.handleAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name'>
  205. <div className='status__avatar'>
  206. {statusAvatar}
  207. </div>
  208. <DisplayName account={status.get('account')} />
  209. </a>
  210. </div>
  211. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} onHeightUpdate={this.saveHeight} />
  212. {media}
  213. <StatusActionBar {...this.props} />
  214. </div>
  215. );
  216. }
  217. }