import React from 'react';

export class ErrorBoundary extends React.Component<
    { children: React.ReactNode },
    { error?: Error }
> {
    constructor(props: { children: React.ReactNode }) {
        super(props);
        this.state = {};
    }

    static getDerivedStateFromError(error: Error) {
        return { error };
    }

    componentDidCatch(error: Error) {
        this.setState({ error });
    }

    render() {
        const { error } = this.state;
        const { children } = this.props;

        if (!error) return children;

        return <div data-testid="Error">{error.message}</div>;
    }
}
