import { PureComponent } from 'react'; import isEqual from 'react-fast-compare'; import PortaledModal, { ModalProps } from '@/components/elements/Modal'; import ModalContext, { ModalContextValues } from '@/context/ModalContext'; export interface AsModalProps { visible: boolean; onModalDismissed?: () => void; } export type SettableModalProps = Omit; interface State { render: boolean; visible: boolean; propOverrides: Partial; } // eslint-disable-next-line @typescript-eslint/ban-types function asModal

( modalProps?: SettableModalProps | ((props: P) => SettableModalProps), ): (Component: any) => any { return function (Component) { return class extends PureComponent

{ static displayName = `asModal(${Component.displayName})`; constructor(props: P & AsModalProps) { super(props); this.state = { render: props.visible, visible: props.visible, propOverrides: {}, }; } get computedModalProps(): Readonly { return { ...(typeof modalProps === 'function' ? modalProps(this.props) : modalProps), ...this.state.propOverrides, visible: this.state.visible, }; } /** * @this {React.PureComponent

} */ override componentDidUpdate(prevProps: Readonly

, prevState: Readonly) { if (prevProps.visible && !this.props.visible) { this.setState({ visible: false, propOverrides: {} }); } else if (!prevProps.visible && this.props.visible) { this.setState({ render: true, visible: true }); } if (!this.state.render && !isEqual(prevState.propOverrides, this.state.propOverrides)) { this.setState({ propOverrides: {} }); } } dismiss = () => this.setState({ visible: false }); setPropOverrides: ModalContextValues['setPropOverrides'] = value => this.setState(state => ({ propOverrides: !value ? {} : typeof value === 'function' ? value(state.propOverrides) : value, })); /** * @this {React.PureComponent

} */ override render() { if (!this.state.render) return null; return ( this.setState({ render: false }, () => { if (typeof this.props.onModalDismissed === 'function') { this.props.onModalDismissed(); } }) } {...this.computedModalProps} > ); } }; }; } export default asModal;