import React from "react";

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, message: "" };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true, message: error?.message || "Неизвестная ошибка." };
  }

  componentDidCatch(error, info) {
    // eslint-disable-next-line no-console
    console.error("UI error boundary:", error, info);
  }

  handleReload = () => {
    window.location.reload();
  };

  render() {
    if (this.state.hasError) {
      return (
        <div className="error-boundary">
          <div className="error-boundary-card">
            <h3>Интерфейс временно недоступен</h3>
            <p>{this.state.message}</p>
            <button className="primary" onClick={this.handleReload}>
              Перезагрузить
            </button>
          </div>
        </div>
      );
    }
    return this.props.children;
  }
}

export default ErrorBoundary;
