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.

81 lines
2.5 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 { connect } from 'react-redux';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import { previewState } from './video_modal';
  8. import Footer from 'mastodon/features/picture_in_picture/components/footer';
  9. const mapStateToProps = (state, { statusId }) => ({
  10. accountStaticAvatar: state.getIn(['accounts', state.getIn(['statuses', statusId, 'account']), 'avatar_static']),
  11. });
  12. export default @connect(mapStateToProps)
  13. class AudioModal extends ImmutablePureComponent {
  14. static propTypes = {
  15. media: ImmutablePropTypes.map.isRequired,
  16. statusId: PropTypes.string.isRequired,
  17. accountStaticAvatar: PropTypes.string.isRequired,
  18. options: PropTypes.shape({
  19. autoPlay: PropTypes.bool,
  20. }),
  21. onClose: PropTypes.func.isRequired,
  22. onChangeBackgroundColor: PropTypes.func.isRequired,
  23. };
  24. static contextTypes = {
  25. router: PropTypes.object,
  26. };
  27. componentDidMount () {
  28. if (this.context.router) {
  29. const history = this.context.router.history;
  30. history.push(history.location.pathname, previewState);
  31. this.unlistenHistory = history.listen(() => {
  32. this.props.onClose();
  33. });
  34. }
  35. }
  36. componentWillUnmount () {
  37. if (this.context.router) {
  38. this.unlistenHistory();
  39. if (this.context.router.history.location.state === previewState) {
  40. this.context.router.history.goBack();
  41. }
  42. }
  43. }
  44. render () {
  45. const { media, accountStaticAvatar, statusId, onClose } = this.props;
  46. const options = this.props.options || {};
  47. return (
  48. <div className='modal-root__modal audio-modal'>
  49. <div className='audio-modal__container'>
  50. <Audio
  51. src={media.get('url')}
  52. alt={media.get('description')}
  53. duration={media.getIn(['meta', 'original', 'duration'], 0)}
  54. height={150}
  55. poster={media.get('preview_url') || accountStaticAvatar}
  56. backgroundColor={media.getIn(['meta', 'colors', 'background'])}
  57. foregroundColor={media.getIn(['meta', 'colors', 'foreground'])}
  58. accentColor={media.getIn(['meta', 'colors', 'accent'])}
  59. autoPlay={options.autoPlay}
  60. />
  61. </div>
  62. <div className='media-modal__overlay'>
  63. {statusId && <Footer statusId={statusId} withOpenButton onClose={onClose} />}
  64. </div>
  65. </div>
  66. );
  67. }
  68. }