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.

79 lines
2.4 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Audio from 'mastodon/features/audio';
  5. import ImmutablePureComponent from 'react-immutable-pure-component';
  6. import { FormattedMessage } from 'react-intl';
  7. import { previewState } from './video_modal';
  8. import classNames from 'classnames';
  9. import Icon from 'mastodon/components/icon';
  10. export default class AudioModal extends ImmutablePureComponent {
  11. static propTypes = {
  12. media: ImmutablePropTypes.map.isRequired,
  13. status: ImmutablePropTypes.map,
  14. onClose: PropTypes.func.isRequired,
  15. };
  16. static contextTypes = {
  17. router: PropTypes.object,
  18. };
  19. componentDidMount () {
  20. if (this.context.router) {
  21. const history = this.context.router.history;
  22. history.push(history.location.pathname, previewState);
  23. this.unlistenHistory = history.listen(() => {
  24. this.props.onClose();
  25. });
  26. }
  27. }
  28. componentWillUnmount () {
  29. if (this.context.router) {
  30. this.unlistenHistory();
  31. if (this.context.router.history.location.state === previewState) {
  32. this.context.router.history.goBack();
  33. }
  34. }
  35. }
  36. handleStatusClick = e => {
  37. if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  38. e.preventDefault();
  39. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  40. }
  41. }
  42. render () {
  43. const { media, status } = this.props;
  44. return (
  45. <div className='modal-root__modal audio-modal'>
  46. <div className='audio-modal__container'>
  47. <Audio
  48. src={media.get('url')}
  49. alt={media.get('description')}
  50. duration={media.getIn(['meta', 'original', 'duration'], 0)}
  51. height={150}
  52. poster={media.get('preview_url') || status.getIn(['account', 'avatar_static'])}
  53. backgroundColor={media.getIn(['meta', 'colors', 'background'])}
  54. foregroundColor={media.getIn(['meta', 'colors', 'foreground'])}
  55. accentColor={media.getIn(['meta', 'colors', 'accent'])}
  56. />
  57. </div>
  58. {status && (
  59. <div className={classNames('media-modal__meta')}>
  60. <a href={status.get('url')} onClick={this.handleStatusClick}><Icon id='comments' /> <FormattedMessage id='lightbox.view_context' defaultMessage='View context' /></a>
  61. </div>
  62. )}
  63. </div>
  64. );
  65. }
  66. }