The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

247 рядки
8.9 KiB

  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. // Exclude intersectionObserverWrapper from `other` variable
  145. // because intersection is managed in here.
  146. const { status, account, intersectionObserverWrapper, ...other } = this.props;
  147. const { isExpanded, isIntersecting, isHidden } = this.state;
  148. if (status === null) {
  149. return null;
  150. }
  151. if (!isIntersecting && isHidden) {
  152. return (
  153. <div ref={this.handleRef} data-id={status.get('id')} style={{ height: `${this.height}px`, opacity: 0, overflow: 'hidden' }}>
  154. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  155. {status.get('content')}
  156. </div>
  157. );
  158. }
  159. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  160. let displayName = status.getIn(['account', 'display_name']);
  161. if (displayName.length === 0) {
  162. displayName = status.getIn(['account', 'username']);
  163. }
  164. const displayNameHTML = { __html: emojify(escapeTextContentForBrowser(displayName)) };
  165. return (
  166. <div className='status__wrapper' ref={this.handleRef} data-id={status.get('id')} >
  167. <div className='status__prepend'>
  168. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  169. <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> }} />
  170. </div>
  171. <Status {...other} wrapped status={status.get('reblog')} account={status.get('account')} />
  172. </div>
  173. );
  174. }
  175. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  176. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  177. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  178. media = <VideoPlayer media={status.getIn(['media_attachments', 0])} sensitive={status.get('sensitive')} onOpenVideo={this.props.onOpenVideo} />;
  179. } else {
  180. media = <MediaGallery media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} autoPlayGif={this.props.autoPlayGif} />;
  181. }
  182. }
  183. if (account === undefined || account === null) {
  184. statusAvatar = <Avatar src={status.getIn(['account', 'avatar'])} staticSrc={status.getIn(['account', 'avatar_static'])} size={48} />;
  185. }else{
  186. statusAvatar = <AvatarOverlay staticSrc={status.getIn(['account', 'avatar_static'])} overlaySrc={account.get('avatar_static')} />;
  187. }
  188. return (
  189. <div className={`status ${this.props.muted ? 'muted' : ''} status-${status.get('visibility')}`} data-id={status.get('id')} ref={this.handleRef}>
  190. <div className='status__info'>
  191. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  192. <a onClick={this.handleAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name'>
  193. <div className='status__avatar'>
  194. {statusAvatar}
  195. </div>
  196. <DisplayName account={status.get('account')} />
  197. </a>
  198. </div>
  199. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} onHeightUpdate={this.saveHeight} />
  200. {media}
  201. <StatusActionBar {...this.props} />
  202. </div>
  203. );
  204. }
  205. }
  206. export default Status;