soapbox/app/soapbox/features/ui/components/modal-root.tsx

131 wiersze
3.4 KiB
TypeScript
Czysty Zwykły widok Historia

import React from 'react';
2022-11-15 16:00:49 +00:00
import Base from 'soapbox/components/modal-root';
2020-03-27 20:59:38 +00:00
import {
MediaModal,
VideoModal,
BoostModal,
ConfirmationModal,
2020-03-27 20:59:38 +00:00
MuteModal,
ReportModal,
EmbedModal,
CryptoDonateModal,
2020-03-27 20:59:38 +00:00
ListEditor,
ListAdder,
MissingDescriptionModal,
ActionsModal,
HotkeysModal,
ComposeModal,
2022-01-04 20:06:08 +00:00
ReplyMentionsModal,
UnauthorizedModal,
EditFederationModal,
ComponentModal,
ReactionsModal,
FavouritesModal,
ReblogsModal,
MentionsModal,
2022-03-21 18:09:01 +00:00
LandingPageModal,
BirthdaysModal,
AccountNoteModal,
CompareHistoryModal,
VerifySmsModal,
FamiliarFollowersModal,
ComposeEventModal,
JoinEventModal,
2022-09-11 16:25:48 +00:00
AccountModerationModal,
EventParticipantsModal,
} from 'soapbox/features/ui/util/async-components';
2022-11-16 13:32:32 +00:00
import BundleContainer from '../containers/bundle-container';
import { BundleProps } from './bundle';
2022-11-16 13:32:32 +00:00
import BundleModalError from './bundle-modal-error';
import ModalLoading from './modal-loading';
2022-01-10 22:01:24 +00:00
2020-03-27 20:59:38 +00:00
const MODAL_COMPONENTS = {
'MEDIA': MediaModal,
'VIDEO': VideoModal,
'BOOST': BoostModal,
'CONFIRM': ConfirmationModal,
'MISSING_DESCRIPTION': MissingDescriptionModal,
2020-03-27 20:59:38 +00:00
'MUTE': MuteModal,
'REPORT': ReportModal,
'ACTIONS': ActionsModal,
2020-03-27 20:59:38 +00:00
'EMBED': EmbedModal,
'LIST_EDITOR': ListEditor,
'LIST_ADDER': ListAdder,
'HOTKEYS': HotkeysModal,
'COMPOSE': ComposeModal,
2022-01-04 20:06:08 +00:00
'REPLY_MENTIONS': ReplyMentionsModal,
'UNAUTHORIZED': UnauthorizedModal,
'CRYPTO_DONATE': CryptoDonateModal,
'EDIT_FEDERATION': EditFederationModal,
'COMPONENT': ComponentModal,
'REBLOGS': ReblogsModal,
'FAVOURITES': FavouritesModal,
'REACTIONS': ReactionsModal,
'MENTIONS': MentionsModal,
2022-03-21 18:09:01 +00:00
'LANDING_PAGE': LandingPageModal,
'BIRTHDAYS': BirthdaysModal,
'ACCOUNT_NOTE': AccountNoteModal,
'COMPARE_HISTORY': CompareHistoryModal,
'VERIFY_SMS': VerifySmsModal,
'FAMILIAR_FOLLOWERS': FamiliarFollowersModal,
'COMPOSE_EVENT': ComposeEventModal,
'JOIN_EVENT': JoinEventModal,
2022-09-11 16:25:48 +00:00
'ACCOUNT_MODERATION': AccountModerationModal,
'EVENT_PARTICIPANTS': EventParticipantsModal,
2020-03-27 20:59:38 +00:00
};
export type ModalType = keyof typeof MODAL_COMPONENTS | null;
2020-03-27 20:59:38 +00:00
interface IModalRoot {
type: ModalType,
props?: Record<string, any> | null,
onClose: (type?: ModalType) => void,
}
export default class ModalRoot extends React.PureComponent<IModalRoot> {
2020-03-27 20:59:38 +00:00
getSnapshotBeforeUpdate() {
2020-03-27 20:59:38 +00:00
return { visible: !!this.props.type };
}
componentDidUpdate(prevProps: IModalRoot, prevState: any, { visible }: any) {
2020-03-27 20:59:38 +00:00
if (visible) {
2022-03-21 18:09:01 +00:00
document.body.classList.add('with-modals');
2020-03-27 20:59:38 +00:00
} else {
2022-03-21 18:09:01 +00:00
document.body.classList.remove('with-modals');
2020-03-27 20:59:38 +00:00
}
}
renderLoading = (modalId: string) => () => {
return !['MEDIA', 'VIDEO', 'BOOST', 'CONFIRM', 'ACTIONS'].includes(modalId) ? <ModalLoading /> : null;
2020-03-27 20:59:38 +00:00
}
renderError: React.ComponentType<{ onRetry: (props?: BundleProps) => void }> = (props) => {
2020-03-27 20:59:38 +00:00
return <BundleModalError {...props} onClose={this.onClickClose} />;
}
onClickClose = (_?: ModalType) => {
2020-03-27 20:59:38 +00:00
const { onClose, type } = this.props;
onClose(type);
2020-03-27 20:59:38 +00:00
}
render() {
const { type, props } = this.props;
2020-03-27 20:59:38 +00:00
const visible = !!type;
return (
<Base onClose={this.onClickClose} type={type}>
2020-03-27 20:59:38 +00:00
{visible && (
<BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
{(SpecificComponent) => <SpecificComponent {...props} onClose={this.onClickClose} />}
</BundleContainer>
)}
</Base>
);
}
}