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.

442 lines
14 KiB

6 years ago
6 years ago
6 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import StatusPrepend from './status_prepend';
  5. import StatusHeader from './status_header';
  6. import StatusContent from './status_content';
  7. import StatusActionBar from './status_action_bar';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import { MediaGallery, Video } from 'flavours/glitch/util/async-components';
  10. import { HotKeys } from 'react-hotkeys';
  11. import NotificationOverlayContainer from 'flavours/glitch/features/notifications/containers/overlay_container';
  12. // We use the component (and not the container) since we do not want
  13. // to use the progress bar to show download progress
  14. import Bundle from '../features/ui/components/bundle';
  15. export default class Status extends ImmutablePureComponent {
  16. static contextTypes = {
  17. router: PropTypes.object,
  18. };
  19. static propTypes = {
  20. id: PropTypes.string,
  21. status: ImmutablePropTypes.map,
  22. account: ImmutablePropTypes.map,
  23. onReply: PropTypes.func,
  24. onFavourite: PropTypes.func,
  25. onReblog: PropTypes.func,
  26. onDelete: PropTypes.func,
  27. onPin: PropTypes.func,
  28. onOpenMedia: PropTypes.func,
  29. onOpenVideo: PropTypes.func,
  30. onBlock: PropTypes.func,
  31. onEmbed: PropTypes.func,
  32. onHeightChange: PropTypes.func,
  33. muted: PropTypes.bool,
  34. collapse: PropTypes.bool,
  35. hidden: PropTypes.bool,
  36. prepend: PropTypes.string,
  37. withDismiss: PropTypes.bool,
  38. onMoveUp: PropTypes.func,
  39. onMoveDown: PropTypes.func,
  40. };
  41. state = {
  42. isExpanded: null,
  43. markedForDelete: false,
  44. }
  45. // Avoid checking props that are functions (and whose equality will always
  46. // evaluate to false. See react-immutable-pure-component for usage.
  47. updateOnProps = [
  48. 'status',
  49. 'account',
  50. 'settings',
  51. 'prepend',
  52. 'boostModal',
  53. 'muted',
  54. 'collapse',
  55. 'notification',
  56. ]
  57. updateOnStates = [
  58. 'isExpanded',
  59. 'markedForDelete',
  60. ]
  61. // If our settings have changed to disable collapsed statuses, then we
  62. // need to make sure that we uncollapse every one. We do that by watching
  63. // for changes to `settings.collapsed.enabled` in
  64. // `componentWillReceiveProps()`.
  65. // We also need to watch for changes on the `collapse` prop---if this
  66. // changes to anything other than `undefined`, then we need to collapse or
  67. // uncollapse our status accordingly.
  68. componentWillReceiveProps (nextProps) {
  69. if (!nextProps.settings.getIn(['collapsed', 'enabled'])) {
  70. if (this.state.isExpanded === false) {
  71. this.setExpansion(null);
  72. }
  73. } else if (
  74. nextProps.collapse !== this.props.collapse &&
  75. nextProps.collapse !== undefined
  76. ) this.setExpansion(nextProps.collapse ? false : null);
  77. }
  78. // When mounting, we just check to see if our status should be collapsed,
  79. // and collapse it if so. We don't need to worry about whether collapsing
  80. // is enabled here, because `setExpansion()` already takes that into
  81. // account.
  82. // The cases where a status should be collapsed are:
  83. //
  84. // - The `collapse` prop has been set to `true`
  85. // - The user has decided in local settings to collapse all statuses.
  86. // - The user has decided to collapse all notifications ('muted'
  87. // statuses).
  88. // - The user has decided to collapse long statuses and the status is
  89. // over 400px (without media, or 650px with).
  90. // - The status is a reply and the user has decided to collapse all
  91. // replies.
  92. // - The status contains media and the user has decided to collapse all
  93. // statuses with media.
  94. // - The status is a reblog the user has decided to collapse all
  95. // statuses which are reblogs.
  96. componentDidMount () {
  97. const { node } = this;
  98. const {
  99. status,
  100. settings,
  101. collapse,
  102. muted,
  103. prepend,
  104. } = this.props;
  105. const autoCollapseSettings = settings.getIn(['collapsed', 'auto']);
  106. if (function () {
  107. switch (true) {
  108. case collapse:
  109. case autoCollapseSettings.get('all'):
  110. case autoCollapseSettings.get('notifications') && muted:
  111. case autoCollapseSettings.get('lengthy') && node.clientHeight > (
  112. status.get('media_attachments').size && !muted ? 650 : 400
  113. ):
  114. case autoCollapseSettings.get('reblogs') && prepend === 'reblogged_by':
  115. case autoCollapseSettings.get('replies') && status.get('in_reply_to_id', null) !== null:
  116. case autoCollapseSettings.get('media') && !(status.get('spoiler_text').length) && status.get('media_attachments').size:
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }()) this.setExpansion(false);
  122. }
  123. // `setExpansion()` sets the value of `isExpanded` in our state. It takes
  124. // one argument, `value`, which gives the desired value for `isExpanded`.
  125. // The default for this argument is `null`.
  126. // `setExpansion()` automatically checks for us whether toot collapsing
  127. // is enabled, so we don't have to.
  128. setExpansion = (value) => {
  129. switch (true) {
  130. case value === undefined || value === null:
  131. this.setState({ isExpanded: null });
  132. break;
  133. case !value && this.props.settings.getIn(['collapsed', 'enabled']):
  134. this.setState({ isExpanded: false });
  135. break;
  136. case !!value:
  137. this.setState({ isExpanded: true });
  138. break;
  139. }
  140. }
  141. // `parseClick()` takes a click event and responds appropriately.
  142. // If our status is collapsed, then clicking on it should uncollapse it.
  143. // If `Shift` is held, then clicking on it should collapse it.
  144. // Otherwise, we open the url handed to us in `destination`, if
  145. // applicable.
  146. parseClick = (e, destination) => {
  147. const { router } = this.context;
  148. const { status } = this.props;
  149. const { isExpanded } = this.state;
  150. if (!router) return;
  151. if (destination === undefined) {
  152. destination = `/statuses/${
  153. status.getIn(['reblog', 'id'], status.get('id'))
  154. }`;
  155. }
  156. if (e.button === 0) {
  157. if (isExpanded === false) this.setExpansion(null);
  158. else if (e.shiftKey) {
  159. this.setExpansion(false);
  160. document.getSelection().removeAllRanges();
  161. } else router.history.push(destination);
  162. e.preventDefault();
  163. }
  164. }
  165. handleAccountClick = (e) => {
  166. if (this.context.router && e.button === 0) {
  167. const id = e.currentTarget.getAttribute('data-id');
  168. e.preventDefault();
  169. this.context.router.history.push(`/accounts/${id}`);
  170. }
  171. }
  172. handleExpandedToggle = () => {
  173. this.setExpansion(this.state.isExpanded || !this.props.status.get('spoiler') ? null : true);
  174. };
  175. handleOpenVideo = startTime => {
  176. this.props.onOpenVideo(this.props.status.getIn(['media_attachments', 0]), startTime);
  177. }
  178. handleHotkeyReply = e => {
  179. e.preventDefault();
  180. this.props.onReply(this.props.status, this.context.router.history);
  181. }
  182. handleHotkeyFavourite = () => {
  183. this.props.onFavourite(this.props.status);
  184. }
  185. handleHotkeyBoost = e => {
  186. this.props.onReblog(this.props.status, e);
  187. }
  188. handleHotkeyMention = e => {
  189. e.preventDefault();
  190. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  191. }
  192. handleHotkeyOpen = () => {
  193. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  194. }
  195. handleHotkeyOpenProfile = () => {
  196. this.context.router.history.push(`/accounts/${this.props.status.getIn(['account', 'id'])}`);
  197. }
  198. handleHotkeyMoveUp = () => {
  199. this.props.onMoveUp(this.props.status.get('id'));
  200. }
  201. handleHotkeyMoveDown = () => {
  202. this.props.onMoveDown(this.props.status.get('id'));
  203. }
  204. handleRef = c => {
  205. this.node = c;
  206. }
  207. renderLoadingMediaGallery () {
  208. return <div className='media_gallery' style={{ height: '110px' }} />;
  209. }
  210. renderLoadingVideoPlayer () {
  211. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  212. }
  213. render () {
  214. const {
  215. handleRef,
  216. parseClick,
  217. setExpansion,
  218. } = this;
  219. const { router } = this.context;
  220. const {
  221. status,
  222. account,
  223. settings,
  224. collapsed,
  225. muted,
  226. prepend,
  227. intersectionObserverWrapper,
  228. onOpenVideo,
  229. onOpenMedia,
  230. notification,
  231. hidden,
  232. ...other
  233. } = this.props;
  234. const { isExpanded } = this.state;
  235. let background = null;
  236. let attachments = null;
  237. let media = null;
  238. let mediaIcon = null;
  239. if (status === null) {
  240. return null;
  241. }
  242. if (hidden) {
  243. return (
  244. <div
  245. ref={this.handleRef}
  246. data-id={status.get('id')}
  247. style={{
  248. height: `${this.height}px`,
  249. opacity: 0,
  250. overflow: 'hidden',
  251. }}
  252. >
  253. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  254. {' '}
  255. {status.get('content')}
  256. </div>
  257. );
  258. }
  259. // If user backgrounds for collapsed statuses are enabled, then we
  260. // initialize our background accordingly. This will only be rendered if
  261. // the status is collapsed.
  262. if (settings.getIn(['collapsed', 'backgrounds', 'user_backgrounds'])) {
  263. background = status.getIn(['account', 'header']);
  264. }
  265. // This handles our media attachments. Note that we don't show media on
  266. // muted (notification) statuses. If the media type is unknown, then we
  267. // simply ignore it.
  268. // After we have generated our appropriate media element and stored it in
  269. // `media`, we snatch the thumbnail to use as our `background` if media
  270. // backgrounds for collapsed statuses are enabled.
  271. attachments = status.get('media_attachments');
  272. if (attachments.size > 0 && !muted) {
  273. if (attachments.some(item => item.get('type') === 'unknown')) { // Media type is 'unknown'
  274. /* Do nothing */
  275. } else if (attachments.getIn([0, 'type']) === 'video') { // Media type is 'video'
  276. const video = status.getIn(['media_attachments', 0]);
  277. media = (
  278. <Bundle fetchComponent={Video} loading={this.renderLoadingVideoPlayer} >
  279. {Component => <Component
  280. preview={video.get('preview_url')}
  281. src={video.get('url')}
  282. sensitive={status.get('sensitive')}
  283. letterbox={settings.getIn(['media', 'letterbox'])}
  284. fullwidth={settings.getIn(['media', 'fullwidth'])}
  285. onOpenVideo={this.handleOpenVideo}
  286. />}
  287. </Bundle>
  288. );
  289. mediaIcon = 'video-camera';
  290. } else { // Media type is 'image' or 'gifv'
  291. media = (
  292. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} >
  293. {Component => (
  294. <Component
  295. media={attachments}
  296. sensitive={status.get('sensitive')}
  297. letterbox={settings.getIn(['media', 'letterbox'])}
  298. fullwidth={settings.getIn(['media', 'fullwidth'])}
  299. onOpenMedia={this.props.onOpenMedia}
  300. />
  301. )}
  302. </Bundle>
  303. );
  304. mediaIcon = 'picture-o';
  305. }
  306. if (!status.get('sensitive') && !(status.get('spoiler_text').length > 0) && settings.getIn(['collapsed', 'backgrounds', 'preview_images'])) {
  307. background = attachments.getIn([0, 'preview_url']);
  308. }
  309. }
  310. // Here we prepare extra data-* attributes for CSS selectors.
  311. // Users can use those for theming, hiding avatars etc via UserStyle
  312. const selectorAttribs = {
  313. 'data-status-by': `@${status.getIn(['account', 'acct'])}`,
  314. };
  315. if (prepend && account) {
  316. const notifKind = {
  317. favourite: 'favourited',
  318. reblog: 'boosted',
  319. reblogged_by: 'boosted',
  320. }[prepend];
  321. selectorAttribs[`data-${notifKind}-by`] = `@${account.get('acct')}`;
  322. }
  323. const handlers = {
  324. reply: this.handleHotkeyReply,
  325. favourite: this.handleHotkeyFavourite,
  326. boost: this.handleHotkeyBoost,
  327. mention: this.handleHotkeyMention,
  328. open: this.handleHotkeyOpen,
  329. openProfile: this.handleHotkeyOpenProfile,
  330. moveUp: this.handleHotkeyMoveUp,
  331. moveDown: this.handleHotkeyMoveDown,
  332. };
  333. return (
  334. <HotKeys handlers={handlers}>
  335. <div
  336. className={
  337. `status${
  338. muted ? ' muted' : ''
  339. } status-${status.get('visibility')}${
  340. isExpanded === false ? ' collapsed' : ''
  341. }${
  342. isExpanded === false && background ? ' has-background' : ''
  343. }${
  344. this.state.markedForDelete ? ' marked-for-delete' : ''
  345. }`
  346. }
  347. style={{
  348. backgroundImage: (
  349. isExpanded === false && background ?
  350. `url(${background})` :
  351. 'none'
  352. ),
  353. }}
  354. {...selectorAttribs}
  355. ref={handleRef}
  356. >
  357. {prepend && account ? (
  358. <StatusPrepend
  359. type={prepend}
  360. account={account}
  361. parseClick={parseClick}
  362. notificationId={this.props.notificationId}
  363. />
  364. ) : null}
  365. <StatusHeader
  366. status={status}
  367. friend={account}
  368. mediaIcon={mediaIcon}
  369. collapsible={settings.getIn(['collapsed', 'enabled'])}
  370. collapsed={isExpanded === false}
  371. parseClick={parseClick}
  372. setExpansion={setExpansion}
  373. />
  374. <StatusContent
  375. status={status}
  376. media={media}
  377. mediaIcon={mediaIcon}
  378. expanded={isExpanded}
  379. setExpansion={setExpansion}
  380. parseClick={parseClick}
  381. disabled={!router}
  382. />
  383. {isExpanded !== false ? (
  384. <StatusActionBar
  385. {...other}
  386. status={status}
  387. account={status.get('account')}
  388. />
  389. ) : null}
  390. {notification ? (
  391. <NotificationOverlayContainer
  392. notification={notification}
  393. />
  394. ) : null}
  395. </div>
  396. </HotKeys>
  397. );
  398. }
  399. }