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.
 
 
 
 

217 lines
7.1 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 scheduleIdleTask from '../../ui/util/schedule_idle_task';
  14. import classNames from 'classnames';
  15. import Icon from 'mastodon/components/icon';
  16. import PollContainer from 'mastodon/containers/poll_container';
  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. };
  31. state = {
  32. height: null,
  33. };
  34. handleAccountClick = (e) => {
  35. if (e.button === 0 && !(e.ctrlKey || e.metaKey) && this.context.router) {
  36. e.preventDefault();
  37. this.context.router.history.push(`/accounts/${this.props.status.getIn(['account', 'id'])}`);
  38. }
  39. e.stopPropagation();
  40. }
  41. handleOpenVideo = (media, startTime) => {
  42. this.props.onOpenVideo(media, startTime);
  43. }
  44. handleExpandedToggle = () => {
  45. this.props.onToggleHidden(this.props.status);
  46. }
  47. _measureHeight (heightJustChanged) {
  48. if (this.props.measureHeight && this.node) {
  49. scheduleIdleTask(() => this.node && this.setState({ height: Math.ceil(this.node.scrollHeight) + 1 }));
  50. if (this.props.onHeightChange && heightJustChanged) {
  51. this.props.onHeightChange();
  52. }
  53. }
  54. }
  55. setRef = c => {
  56. this.node = c;
  57. this._measureHeight();
  58. }
  59. componentDidUpdate (prevProps, prevState) {
  60. this._measureHeight(prevState.height !== this.state.height);
  61. }
  62. handleModalLink = e => {
  63. e.preventDefault();
  64. let href;
  65. if (e.target.nodeName !== 'A') {
  66. href = e.target.parentNode.href;
  67. } else {
  68. href = e.target.href;
  69. }
  70. window.open(href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  71. }
  72. render () {
  73. const status = (this.props.status && this.props.status.get('reblog')) ? this.props.status.get('reblog') : this.props.status;
  74. const outerStyle = { boxSizing: 'border-box' };
  75. const { compact } = this.props;
  76. if (!status) {
  77. return null;
  78. }
  79. let media = '';
  80. let applicationLink = '';
  81. let reblogLink = '';
  82. let reblogIcon = 'retweet';
  83. let favouriteLink = '';
  84. if (this.props.measureHeight) {
  85. outerStyle.height = `${this.state.height}px`;
  86. }
  87. if (status.get('poll')) {
  88. media = <PollContainer pollId={status.get('poll')} />;
  89. } else if (status.get('media_attachments').size > 0) {
  90. if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  91. const video = status.getIn(['media_attachments', 0]);
  92. media = (
  93. <Video
  94. preview={video.get('preview_url')}
  95. blurhash={video.get('blurhash')}
  96. src={video.get('url')}
  97. alt={video.get('description')}
  98. width={300}
  99. height={150}
  100. inline
  101. onOpenVideo={this.handleOpenVideo}
  102. sensitive={status.get('sensitive')}
  103. />
  104. );
  105. } else {
  106. media = (
  107. <MediaGallery
  108. standalone
  109. sensitive={status.get('sensitive')}
  110. media={status.get('media_attachments')}
  111. height={300}
  112. onOpenMedia={this.props.onOpenMedia}
  113. />
  114. );
  115. }
  116. } else if (status.get('spoiler_text').length === 0) {
  117. media = <Card onOpenMedia={this.props.onOpenMedia} card={status.get('card', null)} />;
  118. }
  119. if (status.get('application')) {
  120. applicationLink = <span> · <a className='detailed-status__application' href={status.getIn(['application', 'website'])} target='_blank' rel='noopener'>{status.getIn(['application', 'name'])}</a></span>;
  121. }
  122. if (status.get('visibility') === 'direct') {
  123. reblogIcon = 'envelope';
  124. } else if (status.get('visibility') === 'private') {
  125. reblogIcon = 'lock';
  126. }
  127. if (status.get('visibility') === 'private') {
  128. reblogLink = <Icon id={reblogIcon} />;
  129. } else if (this.context.router) {
  130. reblogLink = (
  131. <Link to={`/statuses/${status.get('id')}/reblogs`} className='detailed-status__link'>
  132. <Icon id={reblogIcon} />
  133. <span className='detailed-status__reblogs'>
  134. <FormattedNumber value={status.get('reblogs_count')} />
  135. </span>
  136. </Link>
  137. );
  138. } else {
  139. reblogLink = (
  140. <a href={`/interact/${status.get('id')}?type=reblog`} className='detailed-status__link' onClick={this.handleModalLink}>
  141. <Icon id={reblogIcon} />
  142. <span className='detailed-status__reblogs'>
  143. <FormattedNumber value={status.get('reblogs_count')} />
  144. </span>
  145. </a>
  146. );
  147. }
  148. if (this.context.router) {
  149. favouriteLink = (
  150. <Link to={`/statuses/${status.get('id')}/favourites`} className='detailed-status__link'>
  151. <Icon id='star' />
  152. <span className='detailed-status__favorites'>
  153. <FormattedNumber value={status.get('favourites_count')} />
  154. </span>
  155. </Link>
  156. );
  157. } else {
  158. favouriteLink = (
  159. <a href={`/interact/${status.get('id')}?type=favourite`} className='detailed-status__link' onClick={this.handleModalLink}>
  160. <Icon id='star' />
  161. <span className='detailed-status__favorites'>
  162. <FormattedNumber value={status.get('favourites_count')} />
  163. </span>
  164. </a>
  165. );
  166. }
  167. return (
  168. <div style={outerStyle}>
  169. <div ref={this.setRef} className={classNames('detailed-status', { compact })}>
  170. <a href={status.getIn(['account', 'url'])} onClick={this.handleAccountClick} className='detailed-status__display-name'>
  171. <div className='detailed-status__display-avatar'><Avatar account={status.get('account')} size={48} /></div>
  172. <DisplayName account={status.get('account')} localDomain={this.props.domain} />
  173. </a>
  174. <StatusContent status={status} expanded={!status.get('hidden')} onExpandedToggle={this.handleExpandedToggle} />
  175. {media}
  176. <div className='detailed-status__meta'>
  177. <a className='detailed-status__datetime' href={status.get('url')} target='_blank' rel='noopener'>
  178. <FormattedDate value={new Date(status.get('created_at'))} hour12={false} year='numeric' month='short' day='2-digit' hour='2-digit' minute='2-digit' />
  179. </a>{applicationLink} · {reblogLink} · {favouriteLink}
  180. </div>
  181. </div>
  182. </div>
  183. );
  184. }
  185. }