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.
 
 
 
 

261 lines
9.7 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 ImmutablePureComponent from 'react-immutable-pure-component';
  12. import scheduleIdleTask from '../features/ui/util/schedule_idle_task';
  13. import { MediaGallery, VideoPlayer } from '../features/ui/util/async-components';
  14. // We use the component (and not the container) since we do not want
  15. // to use the progress bar to show download progress
  16. import Bundle from '../features/ui/components/bundle';
  17. import getRectFromEntry from '../features/ui/util/get_rect_from_entry';
  18. export default 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. onPin: PropTypes.func,
  31. onOpenMedia: PropTypes.func,
  32. onOpenVideo: PropTypes.func,
  33. onBlock: PropTypes.func,
  34. onHeightChange: 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.oneOfType([PropTypes.string, PropTypes.number]),
  41. listLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  42. };
  43. state = {
  44. isExpanded: false,
  45. isHidden: false, // set to true in requestIdleCallback to trigger un-render
  46. }
  47. // Avoid checking props that are functions (and whose equality will always
  48. // evaluate to false. See react-immutable-pure-component for usage.
  49. updateOnProps = [
  50. 'status',
  51. 'account',
  52. 'wrapped',
  53. 'me',
  54. 'boostModal',
  55. 'autoPlayGif',
  56. 'muted',
  57. 'listLength',
  58. ]
  59. updateOnStates = ['isExpanded']
  60. shouldComponentUpdate (nextProps, nextState) {
  61. if (!nextState.isIntersecting && nextState.isHidden) {
  62. // It's only if we're not intersecting (i.e. offscreen) and isHidden is true
  63. // that either "isIntersecting" or "isHidden" matter, and then they're
  64. // the only things that matter (and updated ARIA attributes).
  65. return this.state.isIntersecting || !this.state.isHidden || nextProps.listLength !== this.props.listLength;
  66. } else if (nextState.isIntersecting && !this.state.isIntersecting) {
  67. // If we're going from a non-intersecting state to an intersecting state,
  68. // (i.e. offscreen to onscreen), then we definitely need to re-render
  69. return true;
  70. }
  71. // Otherwise, diff based on "updateOnProps" and "updateOnStates"
  72. return super.shouldComponentUpdate(nextProps, nextState);
  73. }
  74. componentDidMount () {
  75. if (!this.props.intersectionObserverWrapper) {
  76. // TODO: enable IntersectionObserver optimization for notification statuses.
  77. // These are managed in notifications/index.js rather than status_list.js
  78. return;
  79. }
  80. this.props.intersectionObserverWrapper.observe(
  81. this.props.id,
  82. this.node,
  83. this.handleIntersection
  84. );
  85. this.componentMounted = true;
  86. }
  87. componentWillUnmount () {
  88. if (this.props.intersectionObserverWrapper) {
  89. this.props.intersectionObserverWrapper.unobserve(this.props.id, this.node);
  90. }
  91. this.componentMounted = false;
  92. }
  93. handleIntersection = (entry) => {
  94. if (this.node && this.node.children.length !== 0) {
  95. // save the height of the fully-rendered element
  96. this.height = getRectFromEntry(entry).height;
  97. if (this.props.onHeightChange) {
  98. this.props.onHeightChange(this.props.status, this.height);
  99. }
  100. }
  101. this.setState((prevState) => {
  102. if (prevState.isIntersecting && !entry.isIntersecting) {
  103. scheduleIdleTask(this.hideIfNotIntersecting);
  104. }
  105. return {
  106. isIntersecting: entry.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, index, listLength, wrapped, ...other } = this.props;
  153. const { isExpanded, isIntersecting, isHidden } = this.state;
  154. if (status === null) {
  155. return null;
  156. }
  157. const hasIntersectionObserverWrapper = !!this.props.intersectionObserverWrapper;
  158. const isHiddenForSure = isIntersecting === false && isHidden;
  159. const visibilityUnknownButHeightIsCached = isIntersecting === undefined && status.has('height');
  160. if (hasIntersectionObserverWrapper && (isHiddenForSure || visibilityUnknownButHeightIsCached)) {
  161. return (
  162. <article ref={this.handleRef} data-id={status.get('id')} aria-posinset={index} aria-setsize={listLength} tabIndex='0' style={{ height: `${this.height || status.get('height')}px`, opacity: 0, overflow: 'hidden' }}>
  163. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  164. {status.get('content')}
  165. </article>
  166. );
  167. }
  168. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  169. const display_name_html = { __html: status.getIn(['account', 'display_name_html']) };
  170. return (
  171. <article className='status__wrapper' ref={this.handleRef} data-id={status.get('id')} aria-posinset={index} aria-setsize={listLength} tabIndex='0'>
  172. <div className='status__prepend'>
  173. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  174. <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={display_name_html} /></a> }} />
  175. </div>
  176. <Status {...other} wrapped status={status.get('reblog')} account={status.get('account')} />
  177. </article>
  178. );
  179. }
  180. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  181. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  182. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  183. media = (
  184. <Bundle fetchComponent={VideoPlayer} loading={this.renderLoadingVideoPlayer} >
  185. {Component => <Component media={status.getIn(['media_attachments', 0])} sensitive={status.get('sensitive')} onOpenVideo={this.props.onOpenVideo} />}
  186. </Bundle>
  187. );
  188. } else {
  189. media = (
  190. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} >
  191. {Component => <Component media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} autoPlayGif={this.props.autoPlayGif} />}
  192. </Bundle>
  193. );
  194. }
  195. }
  196. if (account === undefined || account === null) {
  197. statusAvatar = <Avatar account={status.get('account')} size={48} />;
  198. }else{
  199. statusAvatar = <AvatarOverlay account={status.get('account')} friend={account} />;
  200. }
  201. return (
  202. <article aria-posinset={index} aria-setsize={listLength} className={`status ${this.props.muted ? 'muted' : ''} status-${status.get('visibility')}`} data-id={status.get('id')} tabIndex={wrapped ? null : '0'} ref={this.handleRef}>
  203. <div className='status__info'>
  204. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  205. <a onClick={this.handleAccountClick} target='_blank' data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name'>
  206. <div className='status__avatar'>
  207. {statusAvatar}
  208. </div>
  209. <DisplayName account={status.get('account')} />
  210. </a>
  211. </div>
  212. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} />
  213. {media}
  214. <StatusActionBar {...this.props} />
  215. </article>
  216. );
  217. }
  218. }