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.
 
 
 
 

257 lines
8.3 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 { MediaGallery, Video } from '../features/ui/util/async-components';
  13. import { HotKeys } from 'react-hotkeys';
  14. import classNames from 'classnames';
  15. // We use the component (and not the container) since we do not want
  16. // to use the progress bar to show download progress
  17. import Bundle from '../features/ui/components/bundle';
  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. onReply: PropTypes.func,
  26. onFavourite: PropTypes.func,
  27. onReblog: PropTypes.func,
  28. onDelete: PropTypes.func,
  29. onPin: PropTypes.func,
  30. onOpenMedia: PropTypes.func,
  31. onOpenVideo: PropTypes.func,
  32. onBlock: PropTypes.func,
  33. onEmbed: PropTypes.func,
  34. onHeightChange: PropTypes.func,
  35. muted: PropTypes.bool,
  36. hidden: PropTypes.bool,
  37. onMoveUp: PropTypes.func,
  38. onMoveDown: PropTypes.func,
  39. };
  40. state = {
  41. isExpanded: false,
  42. }
  43. // Avoid checking props that are functions (and whose equality will always
  44. // evaluate to false. See react-immutable-pure-component for usage.
  45. updateOnProps = [
  46. 'status',
  47. 'account',
  48. 'muted',
  49. 'hidden',
  50. ]
  51. updateOnStates = ['isExpanded']
  52. handleClick = () => {
  53. if (!this.context.router) {
  54. return;
  55. }
  56. const { status } = this.props;
  57. this.context.router.history.push(`/statuses/${status.getIn(['reblog', 'id'], status.get('id'))}`);
  58. }
  59. handleAccountClick = (e) => {
  60. if (this.context.router && e.button === 0) {
  61. const id = e.currentTarget.getAttribute('data-id');
  62. e.preventDefault();
  63. this.context.router.history.push(`/accounts/${id}`);
  64. }
  65. }
  66. handleExpandedToggle = () => {
  67. this.setState({ isExpanded: !this.state.isExpanded });
  68. };
  69. renderLoadingMediaGallery () {
  70. return <div className='media_gallery' style={{ height: '110px' }} />;
  71. }
  72. renderLoadingVideoPlayer () {
  73. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  74. }
  75. handleOpenVideo = startTime => {
  76. this.props.onOpenVideo(this._properStatus().getIn(['media_attachments', 0]), startTime);
  77. }
  78. handleHotkeyReply = e => {
  79. e.preventDefault();
  80. this.props.onReply(this._properStatus(), this.context.router.history);
  81. }
  82. handleHotkeyFavourite = () => {
  83. this.props.onFavourite(this._properStatus());
  84. }
  85. handleHotkeyBoost = e => {
  86. this.props.onReblog(this._properStatus(), e);
  87. }
  88. handleHotkeyMention = e => {
  89. e.preventDefault();
  90. this.props.onMention(this._properStatus().get('account'), this.context.router.history);
  91. }
  92. handleHotkeyOpen = () => {
  93. this.context.router.history.push(`/statuses/${this._properStatus().get('id')}`);
  94. }
  95. handleHotkeyOpenProfile = () => {
  96. this.context.router.history.push(`/accounts/${this._properStatus().getIn(['account', 'id'])}`);
  97. }
  98. handleHotkeyMoveUp = () => {
  99. this.props.onMoveUp(this.props.status.get('id'));
  100. }
  101. handleHotkeyMoveDown = () => {
  102. this.props.onMoveDown(this.props.status.get('id'));
  103. }
  104. _properStatus () {
  105. const { status } = this.props;
  106. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  107. return status.get('reblog');
  108. } else {
  109. return status;
  110. }
  111. }
  112. render () {
  113. let media = null;
  114. let statusAvatar, prepend;
  115. const { hidden, featured } = this.props;
  116. const { isExpanded } = this.state;
  117. let { status, account, ...other } = this.props;
  118. if (status === null) {
  119. return null;
  120. }
  121. if (hidden) {
  122. return (
  123. <div>
  124. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  125. {status.get('content')}
  126. </div>
  127. );
  128. }
  129. if (featured) {
  130. prepend = (
  131. <div className='status__prepend'>
  132. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-thumb-tack status__prepend-icon' /></div>
  133. <FormattedMessage id='status.pinned' defaultMessage='Pinned toot' />
  134. </div>
  135. );
  136. } else if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  137. const display_name_html = { __html: status.getIn(['account', 'display_name_html']) };
  138. prepend = (
  139. <div className='status__prepend'>
  140. <div className='status__prepend-icon-wrapper'><i className='fa fa-fw fa-retweet status__prepend-icon' /></div>
  141. <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'><bdi><strong dangerouslySetInnerHTML={display_name_html} /></bdi></a> }} />
  142. </div>
  143. );
  144. account = status.get('account');
  145. status = status.get('reblog');
  146. }
  147. if (status.get('media_attachments').size > 0 && !this.props.muted) {
  148. if (status.get('media_attachments').some(item => item.get('type') === 'unknown')) {
  149. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  150. const video = status.getIn(['media_attachments', 0]);
  151. media = (
  152. <Bundle fetchComponent={Video} loading={this.renderLoadingVideoPlayer} >
  153. {Component => (
  154. <Component
  155. preview={video.get('preview_url')}
  156. src={video.get('url')}
  157. width={239}
  158. height={110}
  159. inline
  160. sensitive={status.get('sensitive')}
  161. onOpenVideo={this.handleOpenVideo}
  162. />
  163. )}
  164. </Bundle>
  165. );
  166. } else {
  167. media = (
  168. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery} >
  169. {Component => <Component media={status.get('media_attachments')} sensitive={status.get('sensitive')} height={110} onOpenMedia={this.props.onOpenMedia} />}
  170. </Bundle>
  171. );
  172. }
  173. }
  174. if (account === undefined || account === null) {
  175. statusAvatar = <Avatar account={status.get('account')} size={48} />;
  176. }else{
  177. statusAvatar = <AvatarOverlay account={status.get('account')} friend={account} />;
  178. }
  179. const handlers = this.props.muted ? {} : {
  180. reply: this.handleHotkeyReply,
  181. favourite: this.handleHotkeyFavourite,
  182. boost: this.handleHotkeyBoost,
  183. mention: this.handleHotkeyMention,
  184. open: this.handleHotkeyOpen,
  185. openProfile: this.handleHotkeyOpenProfile,
  186. moveUp: this.handleHotkeyMoveUp,
  187. moveDown: this.handleHotkeyMoveDown,
  188. };
  189. return (
  190. <HotKeys handlers={handlers}>
  191. <div className={classNames('status__wrapper', `status__wrapper-${status.get('visibility')}`, { focusable: !this.props.muted })} tabIndex={this.props.muted ? null : 0}>
  192. {prepend}
  193. <div className={classNames('status', `status-${status.get('visibility')}`, { muted: this.props.muted })} data-id={status.get('id')}>
  194. <div className='status__info'>
  195. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  196. <a onClick={this.handleAccountClick} target='_blank' data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} title={status.getIn(['account', 'acct'])} className='status__display-name'>
  197. <div className='status__avatar'>
  198. {statusAvatar}
  199. </div>
  200. <DisplayName account={status.get('account')} />
  201. </a>
  202. </div>
  203. <StatusContent status={status} onClick={this.handleClick} expanded={isExpanded} onExpandedToggle={this.handleExpandedToggle} />
  204. {media}
  205. <StatusActionBar status={status} account={account} {...other} />
  206. </div>
  207. </div>
  208. </HotKeys>
  209. );
  210. }
  211. }