File size: 1,780 Bytes
9df6ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Component, type ReactNode } from "react";

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

interface State {
  hasError: boolean;
  error: Error | null;
}

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

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

  render() {
    if (this.state.hasError) {
      if (this.props.fallback) return this.props.fallback;
      return (
        <div className="flex min-h-screen items-center justify-center bg-slate-50 p-6">

          <div className="max-w-md rounded-xl bg-white p-8 shadow-lg text-center">

            <div className="mb-4 inline-flex h-16 w-16 items-center justify-center rounded-full bg-red-100">

              <svg className="h-8 w-8 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>

                <path strokeLinecap="round" strokeLinejoin="round" d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />

              </svg>

            </div>

            <h2 className="text-lg font-semibold text-slate-800 mb-2">Something went wrong</h2>

            <p className="text-sm text-slate-500 mb-4">

              {this.state.error?.message || "An unexpected error occurred."}

            </p>

            <button

              onClick={() => { this.setState({ hasError: false, error: null }); window.location.reload(); }}

              className="rounded-lg bg-violet-600 px-4 py-2 text-sm font-medium text-white hover:bg-violet-700 transition-colors"

            >

              Reload Page

            </button>

          </div>

        </div>
      );
    }
    return this.props.children;
  }
}