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.
 
 
 
 

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