fx-1 / src /components /ui /error-boundary.tsx
felix-fx's picture
Initial commit from GitHub fx-1
6711fd5 verified
Raw History Blame Contribute Delete
6.11 kB
import React, { Component, ErrorInfo, ReactNode } from "react";
import { AlertTriangle, RefreshCw, Terminal, ChevronDown, ChevronUp } from "lucide-react";
import { Button } from "@/components/ui/button";
interface ErrorLog {
error: Error;
errorInfo: ErrorInfo;
timestamp: number;
}
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
errors: ErrorLog[];
isExpanded: boolean;
}
export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
errors: [],
isExpanded: false
};
public static getDerivedStateFromError(error: Error): Partial<State> {
return { hasError: true };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("CRITICAL UI ERROR:", error, errorInfo);
// معالجة أخطاء الاستيراد الديناميكي (Dynamic Import Errors)
if (error.name === 'TypeError' &&
(error.message.includes('dynamically imported module') ||
error.message.includes('Failed to fetch'))) {
console.warn("Dynamic import failed. Attempting to reload the page to fetch the latest assets...");
window.location.reload();
return;
}
const newError: ErrorLog = {
error,
errorInfo,
timestamp: Date.now()
};
this.setState(prevState => ({
errors: [...prevState.errors, newError]
}));
}
private handleReset = () => {
this.setState({ hasError: false, errors: [], isExpanded: false });
window.location.href = '/';
};
private toggleExpand = () => {
this.setState(prev => ({ isExpanded: !prev }));
};
public render() {
if (this.state.hasError && this.state.errors.length > 0) {
const latestError = this.state.errors[this.state.errors.length - 1];
return (
<div className="fixed inset-0 z-[9999] flex items-center justify-center bg-black/90 p-4 overflow-auto backdrop-blur-sm">
<div className="w-full max-w-4xl bg-zinc-950 border border-destructive/30 rounded-3xl p-6 md:p-10 shadow-[0_0_60px_-15px_rgba(239,68,68,0.4)] space-y-6">
<div className="flex flex-col items-center text-center space-y-3">
<div className="w-16 h-16 bg-destructive/10 rounded-full flex items-center justify-center border border-destructive/20 mb-1">
<AlertTriangle className="w-8 h-8 text-destructive animate-pulse" />
</div>
<h1 className="text-2xl md:text-3xl font-black tracking-tighter text-white uppercase">System Failure</h1>
<p className="text-zinc-400 font-medium max-w-md text-sm">
تعرض التطبيق لعدة أخطاء برمجية. تم تجميع كافة الأخطاء أدناه لمساعدتك في التشخيص.
</p>
</div>
<div className="space-y-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2 text-destructive font-bold text-xs uppercase tracking-widest">
<Terminal className="w-4 h-4" />
Detected Errors ({this.state.errors.length})
</div>
<button
onClick={this.toggleExpand}
className="text-zinc-500 hover:text-white transition-colors flex items-center gap-1 text-xs font-bold uppercase"
>
{this.state.isExpanded ? "Collapse All" : "Expand All"}
{this.state.isExpanded ? <ChevronUp className="w-3 h-3" /> : <ChevronDown className="w-3 h-3" />}
</button>
</div>
<div className="max-h-[400px] overflow-y-auto pr-2 space-y-3 custom-scrollbar">
{this.state.errors.map((err, index) => (
<div key={err.timestamp} className="bg-zinc-900/50 border border-zinc-800 rounded-2xl overflow-hidden transition-all">
<div className="p-4 border-b border-zinc-800 flex items-center justify-between bg-zinc-900/80">
<span className="text-[10px] font-black text-zinc-500 uppercase">Error #{index + 1}</span>
<span className="text-[10px] font-medium text-zinc-600">
{new Date(err.timestamp).toLocaleTimeString()}
</span>
</div>
<div className="p-4 font-mono text-xs text-zinc-300 whitespace-pre-wrap">
<div className="text-destructive font-bold mb-2">Message: {err.error.message || "Unknown Error"}</div>
{(this.state.isExpanded || index === this.state.errors.length - 1) && (
<div className="text-zinc-500 mt-2 pt-2 border-t border-zinc-800/50 leading-relaxed">
{err.error.stack || "No stack trace available"}
</div>
)}
</div>
</div>
)).reverse()}
</div>
</div>
<div className="flex flex-col sm:flex-row gap-4 pt-2">
<Button
onClick={this.handleReset}
className="flex-1 h-14 bg-white text-black hover:bg-zinc-200 font-black text-lg rounded-2xl transition-all"
>
<RefreshCw className="w-5 h-5 ml-2" />
إعادة تشغيل النظام
</Button>
<Button
onClick={() => window.history.back()}
variant="outline"
className="flex-1 h-14 border-zinc-800 text-white hover:bg-zinc-900 font-bold text-lg rounded-2xl transition-all"
>
العودة للخلف
</Button>
</div>
<p className="text-center text-zinc-600 text-[10px] uppercase tracking-widest font-bold">
Minecraft FX Recovery Environment v1.1 • Multiple Error Support
</p>
</div>
</div>
);
}
return this.props.children;
}
}