"use client";

import { Component, ReactNode } from "react";

interface Props {
  children: ReactNode;
  fallback?: ReactNode;
}

interface State {
  hasError: boolean;
}

export class ErrorBoundary extends Component<Props, State> {
  state: State = { hasError: false };

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

  render() {
    if (this.state.hasError) {
      return this.props.fallback || (
        <div className="glass-card-static p-8 text-center">
          <div className="text-sm text-red-400 mb-2">页面加载出错</div>
          <button
            onClick={() => this.setState({ hasError: false })}
            className="text-xs px-3 py-1.5 bg-surface-2 rounded-lg text-text-secondary hover:text-text-primary transition-all"
          >
            重试
          </button>
        </div>
      );
    }
    return this.props.children;
  }
}