闭社主体 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.

261 lines
9.5 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 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. // Edge 15 doesn't support isIntersecting, but we can infer it
  97. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12156111/
  98. // https://github.com/WICG/IntersectionObserver/issues/211
  99. const isIntersecting = (typeof entry.isIntersecting === 'boolean') ?
  100. entry.isIntersecting : entry.intersectionRect.height > 0;
  101. this.setState((prevState) => {
  102. if (prevState.isIntersecting && !isIntersecting) {
  103. scheduleIdleTask(this.hideIfNotIntersecting);
  104. }
  105. return {
  106. isIntersecting: isIntersecting,
  107. isHidden: false,
  108. };
  109. });
  110. }
  111. hideIfNotIntersecting = () => {
  112. if (!this.componentMounted) {
  113. return;
  114. }
  115. // When the browser gets a chance, test if we're still not intersecting,
  116. // and if so, set our isHidden to true to trigger an unrender. The point of
  117. // this is to save DOM nodes and avoid using up too much memory.
  118. // See: https://github.com/tootsuite/mastodon/issues/2900
  119. this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }));
  120. }
  121. handleRef = (node) => {
  122. this.node = node;
  123. }
  124. handleClick = () => {
  125. if (!this.context.router) {
  126. return;
  127. }
  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 (this.context.router && 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. renderLoadingMediaGallery () {
  142. return <div className='media_gallery' style={{ height: '110px' }} />;
  143. }
  144. renderLoadingVideoPlayer () {
  145. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  146. }
  147. render () {
  148. let media = null;
  149. let statusAvatar;
  150. // Exclude intersectionObserverWrapper from `other` variable
  151. // because intersection is managed in here.
  152. const { status, account, intersectionObserverWrapper, ...other } = this.props;
  153. const { isExpanded, isIntersecting, isHidden } = this.state;
  154. if (status === null) {
  155. return null;
  156. }
  157. if (!isIntersecting && isHidden) {
  158. return (
  159. <div ref={this.handleRef} data-id={status.get('id')} style={{ height: `${this.height}px`, opacity: 0, overflow: 'hidden' }}>
  160. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  161. {status.get('content')}
  162. </div>
  163. );
  164. }
  165. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  166. let displayName = status.getIn(['account', 'display_name']);
  167. if (displayName.length === 0) {
  168. displayName = status.getIn(['account', 'username']);
  169. }
  170. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  171. return (
  172. <div className='status__wrapper' ref={this.handleRef} data-id={status.get('id')} >
  173. <div className='status__prepend'>
  174. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  175. <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> }} />
  176. </div>
  177. <Status {...other} wrapped status={status.get('reblog')} account={status.get('account')} />
  178. </div>
  179. );
  180. }
  181. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  182. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  183. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  184. media = (
  185. <Bundle fetchComponent={VideoPlayer} loading={this.renderLoadingVideoPlayer} >
  186. {Component => <Component media={status.getIn(['media_attachments', 0])} sensitive={status.get('sensitive')} onOpenVideo={this.props.onOpenVideo} />}
  187. </Bundle>
  188. );
  189. } else {
  190. media = (
  191. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} >
  192. {Component => <Component media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} autoPlayGif={this.props.autoPlayGif} />}
  193. </Bundle>
  194. );
  195. }
  196. }
  197. if (account === undefined || account === null) {
  198. statusAvatar = <Avatar src={status.getIn(['account', 'avatar'])} staticSrc={status.getIn(['account', 'avatar_static'])} size={48} />;
  199. }else{
  200. statusAvatar = <AvatarOverlay staticSrc={status.getIn(['account', 'avatar_static'])} overlaySrc={account.get('avatar_static')} />;
  201. }
  202. return (
  203. <div className={`status ${this.props.muted ? 'muted' : ''} status-${status.get('visibility')}`} data-id={status.get('id')} ref={this.handleRef}>
  204. <div className='status__info'>
  205. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  206. <a onClick={this.handleAccountClick} target='_blank' data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name'>
  207. <div className='status__avatar'>
  208. {statusAvatar}
  209. </div>
  210. <DisplayName account={status.get('account')} />
  211. </a>
  212. </div>
  213. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} />
  214. {media}
  215. <StatusActionBar {...this.props} />
  216. </div>
  217. );
  218. }
  219. }