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.

243 lines
8.7 KiB

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