The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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