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

137 lines
3.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
2022-11-15 08:00:49 -08:00
import Base from 'soapbox/components/modal-root';
2020-03-27 13:59:38 -07:00
import {
MediaModal,
VideoModal,
BoostModal,
ConfirmationModal,
2020-03-27 13:59:38 -07:00
MuteModal,
ReportModal,
EmbedModal,
CryptoDonateModal,
2020-03-27 13:59:38 -07:00
ListEditor,
ListAdder,
MissingDescriptionModal,
ActionsModal,
HotkeysModal,
ComposeModal,
2022-01-04 12:06:08 -08:00
ReplyMentionsModal,
UnauthorizedModal,
EditFederationModal,
ComponentModal,
ReactionsModal,
FavouritesModal,
ReblogsModal,
MentionsModal,
2022-03-21 11:09:01 -07:00
LandingPageModal,
BirthdaysModal,
AccountNoteModal,
CompareHistoryModal,
VerifySmsModal,
FamiliarFollowersModal,
ComposeEventModal,
JoinEventModal,
2022-09-11 09:25:48 -07:00
AccountModerationModal,
EventMapModal,
EventParticipantsModal,
2022-11-01 10:22:29 -07:00
PolicyModal,
ManageGroupModal,
} from 'soapbox/features/ui/util/async-components';
2022-11-16 05:32:32 -08:00
import BundleContainer from '../containers/bundle-container';
import { BundleProps } from './bundle';
2022-11-16 05:32:32 -08:00
import BundleModalError from './bundle-modal-error';
import ModalLoading from './modal-loading';
2022-01-10 14:01:24 -08:00
2020-03-27 13:59:38 -07:00
const MODAL_COMPONENTS = {
'MEDIA': MediaModal,
'VIDEO': VideoModal,
'BOOST': BoostModal,
'CONFIRM': ConfirmationModal,
'MISSING_DESCRIPTION': MissingDescriptionModal,
2020-03-27 13:59:38 -07:00
'MUTE': MuteModal,
'REPORT': ReportModal,
'ACTIONS': ActionsModal,
2020-03-27 13:59:38 -07:00
'EMBED': EmbedModal,
'LIST_EDITOR': ListEditor,
'LIST_ADDER': ListAdder,
'HOTKEYS': HotkeysModal,
'COMPOSE': ComposeModal,
2022-01-04 12:06:08 -08: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 11:09:01 -07: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 09:25:48 -07:00
'ACCOUNT_MODERATION': AccountModerationModal,
'EVENT_MAP': EventMapModal,
'EVENT_PARTICIPANTS': EventParticipantsModal,
2022-11-01 10:22:29 -07:00
'POLICY': PolicyModal,
'MANAGE_GROUP': ManageGroupModal,
2020-03-27 13:59:38 -07:00
};
export type ModalType = keyof typeof MODAL_COMPONENTS | null;
2020-03-27 13:59:38 -07: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 13:59:38 -07:00
getSnapshotBeforeUpdate() {
2020-03-27 13:59:38 -07:00
return { visible: !!this.props.type };
}
componentDidUpdate(prevProps: IModalRoot, prevState: any, { visible }: any) {
2020-03-27 13:59:38 -07:00
if (visible) {
2022-03-21 11:09:01 -07:00
document.body.classList.add('with-modals');
2020-03-27 13:59:38 -07:00
} else {
2022-03-21 11:09:01 -07:00
document.body.classList.remove('with-modals');
2020-03-27 13:59:38 -07:00
}
}
renderLoading = (modalId: string) => () => {
return !['MEDIA', 'VIDEO', 'BOOST', 'CONFIRM', 'ACTIONS'].includes(modalId) ? <ModalLoading /> : null;
2023-01-05 09:55:08 -08:00
};
2020-03-27 13:59:38 -07:00
renderError: React.ComponentType<{ onRetry: (props?: BundleProps) => void }> = (props) => {
2020-03-27 13:59:38 -07:00
return <BundleModalError {...props} onClose={this.onClickClose} />;
2023-01-05 09:55:08 -08:00
};
2020-03-27 13:59:38 -07:00
onClickClose = (_?: ModalType) => {
2020-03-27 13:59:38 -07:00
const { onClose, type } = this.props;
onClose(type);
2023-01-05 09:55:08 -08:00
};
2020-03-27 13:59:38 -07:00
render() {
const { type, props } = this.props;
2020-03-27 13:59:38 -07:00
const visible = !!type;
return (
<Base onClose={this.onClickClose} type={type}>
2020-03-27 13:59:38 -07:00
{visible && (
<BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
{(SpecificComponent) => <SpecificComponent {...props} onClose={this.onClickClose} />}
</BundleContainer>
)}
</Base>
);
}
}