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.

232 lines
7.7 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import Avatar from '../../../components/avatar';
  5. import DisplayName from '../../../components/display_name';
  6. import StatusContent from '../../../components/status_content';
  7. import MediaGallery from '../../../components/media_gallery';
  8. import { Link } from 'react-router-dom';
  9. import { FormattedDate, FormattedNumber } from 'react-intl';
  10. import Card from './card';
  11. import ImmutablePureComponent from 'react-immutable-pure-component';
  12. import Video from '../../video';
  13. import Audio from '../../audio';
  14. import scheduleIdleTask from '../../ui/util/schedule_idle_task';
  15. import classNames from 'classnames';
  16. import Icon from 'mastodon/components/icon';
  17. export default class DetailedStatus extends ImmutablePureComponent {
  18. static contextTypes = {
  19. router: PropTypes.object,
  20. };
  21. static propTypes = {
  22. status: ImmutablePropTypes.map,
  23. onOpenMedia: PropTypes.func.isRequired,
  24. onOpenVideo: PropTypes.func.isRequired,
  25. onToggleHidden: PropTypes.func.isRequired,
  26. measureHeight: PropTypes.bool,
  27. onHeightChange: PropTypes.func,
  28. domain: PropTypes.string.isRequired,
  29. compact: PropTypes.bool,
  30. showMedia: PropTypes.bool,
  31. onToggleMediaVisibility: PropTypes.func,
  32. };
  33. state = {
  34. height: null,
  35. };
  36. handleAccountClick = (e) => {
  37. if (e.button === 0 && !(e.ctrlKey || e.metaKey) && this.context.router) {
  38. e.preventDefault();
  39. this.context.router.history.push(`/accounts/${this.props.status.getIn(['account', 'id'])}`);
  40. }
  41. e.stopPropagation();
  42. }
  43. handleOpenVideo = (media, startTime) => {
  44. this.props.onOpenVideo(media, startTime);
  45. }
  46. handleExpandedToggle = () => {
  47. this.props.onToggleHidden(this.props.status);
  48. }
  49. _measureHeight (heightJustChanged) {
  50. if (this.props.measureHeight && this.node) {
  51. scheduleIdleTask(() => this.node && this.setState({ height: Math.ceil(this.node.scrollHeight) + 1 }));
  52. if (this.props.onHeightChange && heightJustChanged) {
  53. this.props.onHeightChange();
  54. }
  55. }
  56. }
  57. setRef = c => {
  58. this.node = c;
  59. this._measureHeight();
  60. }
  61. componentDidUpdate (prevProps, prevState) {
  62. this._measureHeight(prevState.height !== this.state.height);
  63. }
  64. handleModalLink = e => {
  65. e.preventDefault();
  66. let href;
  67. if (e.target.nodeName !== 'A') {
  68. href = e.target.parentNode.href;
  69. } else {
  70. href = e.target.href;
  71. }
  72. window.open(href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  73. }
  74. render () {
  75. const status = (this.props.status && this.props.status.get('reblog')) ? this.props.status.get('reblog') : this.props.status;
  76. const outerStyle = { boxSizing: 'border-box' };
  77. const { compact } = this.props;
  78. if (!status) {
  79. return null;
  80. }
  81. let media = '';
  82. let applicationLink = '';
  83. let reblogLink = '';
  84. let reblogIcon = 'retweet';
  85. let favouriteLink = '';
  86. if (this.props.measureHeight) {
  87. outerStyle.height = `${this.state.height}px`;
  88. }
  89. if (status.get('media_attachments').size > 0) {
  90. if (status.getIn(['media_attachments', 0, 'type']) === 'audio') {
  91. const attachment = status.getIn(['media_attachments', 0]);
  92. media = (
  93. <Audio
  94. src={attachment.get('url')}
  95. alt={attachment.get('description')}
  96. duration={attachment.getIn(['meta', 'original', 'duration'], 0)}
  97. height={110}
  98. preload
  99. />
  100. );
  101. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  102. const attachment = status.getIn(['media_attachments', 0]);
  103. media = (
  104. <Video
  105. preview={attachment.get('preview_url')}
  106. blurhash={attachment.get('blurhash')}
  107. src={attachment.get('url')}
  108. alt={attachment.get('description')}
  109. width={300}
  110. height={150}
  111. inline
  112. onOpenVideo={this.handleOpenVideo}
  113. sensitive={status.get('sensitive')}
  114. visible={this.props.showMedia}
  115. onToggleVisibility={this.props.onToggleMediaVisibility}
  116. />
  117. );
  118. } else {
  119. media = (
  120. <MediaGallery
  121. standalone
  122. sensitive={status.get('sensitive')}
  123. media={status.get('media_attachments')}
  124. height={300}
  125. onOpenMedia={this.props.onOpenMedia}
  126. visible={this.props.showMedia}
  127. onToggleVisibility={this.props.onToggleMediaVisibility}
  128. />
  129. );
  130. }
  131. } else if (status.get('spoiler_text').length === 0) {
  132. media = <Card onOpenMedia={this.props.onOpenMedia} card={status.get('card', null)} />;
  133. }
  134. if (status.get('application')) {
  135. applicationLink = <span> · <a className='detailed-status__application' href={status.getIn(['application', 'website'])} target='_blank' rel='noopener noreferrer'>{status.getIn(['application', 'name'])}</a></span>;
  136. }
  137. if (status.get('visibility') === 'direct') {
  138. reblogIcon = 'envelope';
  139. } else if (status.get('visibility') === 'private') {
  140. reblogIcon = 'lock';
  141. }
  142. if (status.get('visibility') === 'private') {
  143. reblogLink = <Icon id={reblogIcon} />;
  144. } else if (this.context.router) {
  145. reblogLink = (
  146. <Link to={`/statuses/${status.get('id')}/reblogs`} className='detailed-status__link'>
  147. <Icon id={reblogIcon} />
  148. <span className='detailed-status__reblogs'>
  149. <FormattedNumber value={status.get('reblogs_count')} />
  150. </span>
  151. </Link>
  152. );
  153. } else {
  154. reblogLink = (
  155. <a href={`/interact/${status.get('id')}?type=reblog`} className='detailed-status__link' onClick={this.handleModalLink}>
  156. <Icon id={reblogIcon} />
  157. <span className='detailed-status__reblogs'>
  158. <FormattedNumber value={status.get('reblogs_count')} />
  159. </span>
  160. </a>
  161. );
  162. }
  163. if (this.context.router) {
  164. favouriteLink = (
  165. <Link to={`/statuses/${status.get('id')}/favourites`} className='detailed-status__link'>
  166. <Icon id='star' />
  167. <span className='detailed-status__favorites'>
  168. <FormattedNumber value={status.get('favourites_count')} />
  169. </span>
  170. </Link>
  171. );
  172. } else {
  173. favouriteLink = (
  174. <a href={`/interact/${status.get('id')}?type=favourite`} className='detailed-status__link' onClick={this.handleModalLink}>
  175. <Icon id='star' />
  176. <span className='detailed-status__favorites'>
  177. <FormattedNumber value={status.get('favourites_count')} />
  178. </span>
  179. </a>
  180. );
  181. }
  182. return (
  183. <div style={outerStyle}>
  184. <div ref={this.setRef} className={classNames('detailed-status', { compact })}>
  185. <a href={status.getIn(['account', 'url'])} onClick={this.handleAccountClick} className='detailed-status__display-name'>
  186. <div className='detailed-status__display-avatar'><Avatar account={status.get('account')} size={48} /></div>
  187. <DisplayName account={status.get('account')} localDomain={this.props.domain} />
  188. </a>
  189. <StatusContent status={status} expanded={!status.get('hidden')} onExpandedToggle={this.handleExpandedToggle} />
  190. {media}
  191. <div className='detailed-status__meta'>
  192. <a className='detailed-status__datetime' href={status.get('url')} target='_blank' rel='noopener noreferrer'>
  193. <FormattedDate value={new Date(status.get('created_at'))} hour12={false} year='numeric' month='short' day='2-digit' hour='2-digit' minute='2-digit' />
  194. </a>{applicationLink} · {reblogLink} · {favouriteLink}
  195. </div>
  196. </div>
  197. </div>
  198. );
  199. }
  200. }