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.

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