闭社主体 forked from https://github.com/tootsuite/mastodon
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.

225 lines
8.3 KiB

8 years ago
8 years ago
8 years ago
8 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. }
  82. handleIntersection = (entry) => {
  83. // Edge 15 doesn't support isIntersecting, but we can infer it
  84. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12156111/
  85. // https://github.com/WICG/IntersectionObserver/issues/211
  86. const isIntersecting = (typeof entry.isIntersecting === 'boolean') ?
  87. entry.isIntersecting : entry.intersectionRect.height > 0;
  88. this.setState((prevState) => {
  89. if (prevState.isIntersecting && !isIntersecting) {
  90. scheduleIdleTask(this.hideIfNotIntersecting);
  91. }
  92. return {
  93. isIntersecting: isIntersecting,
  94. isHidden: false,
  95. };
  96. });
  97. }
  98. hideIfNotIntersecting = () => {
  99. // When the browser gets a chance, test if we're still not intersecting,
  100. // and if so, set our isHidden to true to trigger an unrender. The point of
  101. // this is to save DOM nodes and avoid using up too much memory.
  102. // See: https://github.com/tootsuite/mastodon/issues/2900
  103. this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }));
  104. }
  105. saveHeight = () => {
  106. if (this.node && this.node.children.length !== 0) {
  107. this.height = this.node.clientHeight;
  108. }
  109. }
  110. handleRef = (node) => {
  111. this.node = node;
  112. this.saveHeight();
  113. }
  114. handleClick = () => {
  115. const { status } = this.props;
  116. this.context.router.push(`/statuses/${status.getIn(['reblog', 'id'], status.get('id'))}`);
  117. }
  118. handleAccountClick = (e) => {
  119. if (e.button === 0) {
  120. const id = Number(e.currentTarget.getAttribute('data-id'));
  121. e.preventDefault();
  122. this.context.router.push(`/accounts/${id}`);
  123. }
  124. }
  125. handleExpandedToggle = () => {
  126. this.setState({ isExpanded: !this.state.isExpanded });
  127. };
  128. render () {
  129. let media = null;
  130. let statusAvatar;
  131. const { status, account, ...other } = this.props;
  132. const { isExpanded, isIntersecting, isHidden } = this.state;
  133. if (status === null) {
  134. return null;
  135. }
  136. if (!isIntersecting && isHidden) {
  137. return (
  138. <div ref={this.handleRef} data-id={status.get('id')} style={{ height: `${this.height}px`, opacity: 0, overflow: 'hidden' }}>
  139. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  140. {status.get('content')}
  141. </div>
  142. );
  143. }
  144. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  145. let displayName = status.getIn(['account', 'display_name']);
  146. if (displayName.length === 0) {
  147. displayName = status.getIn(['account', 'username']);
  148. }
  149. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  150. return (
  151. <div className='status__wrapper' ref={this.handleRef} data-id={status.get('id')} >
  152. <div className='status__prepend'>
  153. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  154. <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> }} />
  155. </div>
  156. <Status {...other} wrapped status={status.get('reblog')} account={status.get('account')} />
  157. </div>
  158. );
  159. }
  160. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  161. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  162. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  163. media = <VideoPlayer media={status.getIn(['media_attachments', 0])} sensitive={status.get('sensitive')} onOpenVideo={this.props.onOpenVideo} />;
  164. } else {
  165. media = <MediaGallery media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} autoPlayGif={this.props.autoPlayGif} />;
  166. }
  167. }
  168. if (account === undefined || account === null) {
  169. statusAvatar = <Avatar src={status.getIn(['account', 'avatar'])} staticSrc={status.getIn(['account', 'avatar_static'])} size={48} />;
  170. }else{
  171. statusAvatar = <AvatarOverlay staticSrc={status.getIn(['account', 'avatar_static'])} overlaySrc={account.get('avatar_static')} />;
  172. }
  173. return (
  174. <div className={`status ${this.props.muted ? 'muted' : ''} status-${status.get('visibility')}`} data-id={status.get('id')} ref={this.handleRef}>
  175. <div className='status__info'>
  176. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  177. <a onClick={this.handleAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name'>
  178. <div className='status__avatar'>
  179. {statusAvatar}
  180. </div>
  181. <DisplayName account={status.get('account')} />
  182. </a>
  183. </div>
  184. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} onHeightUpdate={this.saveHeight} />
  185. {media}
  186. <StatusActionBar {...this.props} />
  187. </div>
  188. );
  189. }
  190. }
  191. export default Status;