import React from 'react';

// Contains a crash inside an addon-injected component (e.g. window.lazytasksTimeTracker's
// drawer panel) to just that panel instead of the whole app. There is no other
// ErrorBoundary anywhere in this tree, so without this, a bug in third-party addon code —
// which this plugin does not control or build — takes down the entire admin UI with a
// blank white screen and no visible error.
class AddonBoundary extends React.Component {
    constructor(props) {
        super(props);
        this.state = { hasError: false };
    }

    static getDerivedStateFromError() {
        return { hasError: true };
    }

    componentDidCatch(error, errorInfo) {
        // eslint-disable-next-line no-console
        console.error(`[${this.props.label || 'addon'}] crashed and was contained:`, error, errorInfo);
    }

    render() {
        if (this.state.hasError) return null;
        return this.props.children;
    }
}

export default AddonBoundary;
