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.

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