72 lines
2.8 KiB
TypeScript
72 lines
2.8 KiB
TypeScript
import React, { Component, ErrorInfo, ReactNode } from "react";
|
|
import { AlertCircle, RefreshCw, Trash2 } from 'lucide-react';
|
|
|
|
interface Props {
|
|
children?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error: Error | null;
|
|
}
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props);
|
|
this.state = {
|
|
hasError: false,
|
|
error: null
|
|
};
|
|
}
|
|
|
|
public static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error("Uncaught error:", error, errorInfo);
|
|
}
|
|
|
|
public render() {
|
|
if (this.state.hasError) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center min-h-screen bg-slate-50 p-6 text-center animate-fade-in">
|
|
<div className="bg-white p-8 rounded-3xl shadow-xl max-w-md w-full border border-slate-100 flex flex-col items-center">
|
|
<div className="w-20 h-20 bg-red-50 text-red-500 rounded-full flex items-center justify-center mb-6 shadow-sm">
|
|
<AlertCircle size={40} />
|
|
</div>
|
|
<h2 className="text-2xl font-extrabold text-slate-800 mb-2">Something went wrong</h2>
|
|
<p className="text-slate-500 mb-8 text-sm leading-relaxed bg-slate-50 p-4 rounded-xl border border-slate-100 w-full font-mono break-all">
|
|
{this.state.error?.message || "An unexpected error occurred."}
|
|
</p>
|
|
|
|
<div className="space-y-3 w-full">
|
|
<button
|
|
onClick={() => window.location.reload()}
|
|
className="w-full py-3.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-xl font-bold flex items-center justify-center gap-2 transition-all active:scale-95 shadow-lg shadow-indigo-200"
|
|
>
|
|
<RefreshCw size={18} /> Reload App
|
|
</button>
|
|
|
|
<button
|
|
onClick={() => {
|
|
localStorage.clear();
|
|
window.location.reload();
|
|
}}
|
|
className="w-full py-3.5 bg-white border-2 border-red-100 text-red-500 hover:bg-red-50 hover:border-red-200 rounded-xl font-bold flex items-center justify-center gap-2 transition-all active:scale-95"
|
|
>
|
|
<Trash2 size={18} /> Clear Data & Reset
|
|
</button>
|
|
</div>
|
|
|
|
<p className="text-[10px] text-slate-400 mt-6 font-medium">
|
|
* If reloading doesn't work, try clearing data.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
} |