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.

90 lines
2.8 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Base from '../../../components/modal_root';
  4. import BundleContainer from '../containers/bundle_container';
  5. import BundleModalError from './bundle_modal_error';
  6. import ModalLoading from './modal_loading';
  7. import ActionsModal from './actions_modal';
  8. import MediaModal from './media_modal';
  9. import VideoModal from './video_modal';
  10. import BoostModal from './boost_modal';
  11. import FavouriteModal from './favourite_modal';
  12. import DoodleModal from './doodle_modal';
  13. import ConfirmationModal from './confirmation_modal';
  14. import FocalPointModal from './focal_point_modal';
  15. import {
  16. OnboardingModal,
  17. MuteModal,
  18. ReportModal,
  19. SettingsModal,
  20. EmbedModal,
  21. ListEditor,
  22. ListAdder,
  23. PinnedAccountsEditor,
  24. } from 'flavours/glitch/util/async-components';
  25. const MODAL_COMPONENTS = {
  26. 'MEDIA': () => Promise.resolve({ default: MediaModal }),
  27. 'ONBOARDING': OnboardingModal,
  28. 'VIDEO': () => Promise.resolve({ default: VideoModal }),
  29. 'BOOST': () => Promise.resolve({ default: BoostModal }),
  30. 'FAVOURITE': () => Promise.resolve({ default: FavouriteModal }),
  31. 'DOODLE': () => Promise.resolve({ default: DoodleModal }),
  32. 'CONFIRM': () => Promise.resolve({ default: ConfirmationModal }),
  33. 'MUTE': MuteModal,
  34. 'REPORT': ReportModal,
  35. 'SETTINGS': SettingsModal,
  36. 'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
  37. 'EMBED': EmbedModal,
  38. 'LIST_EDITOR': ListEditor,
  39. 'LIST_ADDER':ListAdder,
  40. 'FOCAL_POINT': () => Promise.resolve({ default: FocalPointModal }),
  41. 'PINNED_ACCOUNTS_EDITOR': PinnedAccountsEditor,
  42. };
  43. export default class ModalRoot extends React.PureComponent {
  44. static propTypes = {
  45. type: PropTypes.string,
  46. props: PropTypes.object,
  47. onClose: PropTypes.func.isRequired,
  48. };
  49. getSnapshotBeforeUpdate () {
  50. return { visible: !!this.props.type };
  51. }
  52. componentDidUpdate (prevProps, prevState, { visible }) {
  53. if (visible) {
  54. document.body.classList.add('with-modals--active');
  55. } else {
  56. document.body.classList.remove('with-modals--active');
  57. }
  58. }
  59. renderLoading = modalId => () => {
  60. return ['MEDIA', 'VIDEO', 'BOOST', 'FAVOURITE', 'DOODLE', 'CONFIRM', 'ACTIONS'].indexOf(modalId) === -1 ? <ModalLoading /> : null;
  61. }
  62. renderError = (props) => {
  63. const { onClose } = this.props;
  64. return <BundleModalError {...props} onClose={onClose} />;
  65. }
  66. render () {
  67. const { type, props, onClose } = this.props;
  68. const visible = !!type;
  69. return (
  70. <Base onClose={onClose} noEsc={props ? props.noEsc : false}>
  71. {visible && (
  72. <BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
  73. {(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
  74. </BundleContainer>
  75. )}
  76. </Base>
  77. );
  78. }
  79. }