112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import { Component, ReactNode, ErrorInfo } from 'react';
|
|
import { AlertCircle, RefreshCw, Home } from 'lucide-react';
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
fallback?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error: Error | null;
|
|
isChunkLoadError: boolean;
|
|
}
|
|
|
|
export class RouteErrorBoundary extends Component<Props, State> {
|
|
public state: State = {
|
|
hasError: false,
|
|
error: null,
|
|
isChunkLoadError: false,
|
|
};
|
|
|
|
public static getDerivedStateFromError(error: Error): State {
|
|
const isChunk =
|
|
error.name === 'ChunkLoadError' ||
|
|
/Failed to fetch dynamically imported module/i.test(error.message) ||
|
|
/Loading chunk .* failed/i.test(error.message) ||
|
|
/dynamically imported module/i.test(error.message);
|
|
|
|
return {
|
|
hasError: true,
|
|
error,
|
|
isChunkLoadError: isChunk,
|
|
};
|
|
}
|
|
|
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error('[RouteErrorBoundary] Uncaught error:', error, errorInfo);
|
|
|
|
// Auto-reload on chunk mismatch once to seamlessly fetch updated assets from server
|
|
if (this.state.isChunkLoadError) {
|
|
const storageKey = 'canina_chunk_reload_ts';
|
|
const lastReload = sessionStorage.getItem(storageKey);
|
|
const now = Date.now();
|
|
|
|
// Only auto-reload if we haven't done so in the last 10 seconds
|
|
if (!lastReload || now - parseInt(lastReload, 10) > 10000) {
|
|
sessionStorage.setItem(storageKey, String(now));
|
|
window.location.reload();
|
|
}
|
|
}
|
|
}
|
|
|
|
private handleReload = () => {
|
|
window.location.reload();
|
|
};
|
|
|
|
private handleGoHome = () => {
|
|
window.location.href = '/';
|
|
};
|
|
|
|
public render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) {
|
|
return this.props.fallback;
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-[400px] flex items-center justify-center p-6 font-vazir" dir="rtl">
|
|
<div className="bg-white p-8 rounded-3xl shadow-xl border border-gray-200 max-w-md w-full text-center space-y-5">
|
|
<div className="w-16 h-16 bg-amber-50 text-amber-600 rounded-3xl flex items-center justify-center mx-auto border border-amber-200">
|
|
<AlertCircle className="w-8 h-8" />
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<h3 className="text-lg font-black text-gray-900">
|
|
{this.state.isChunkLoadError
|
|
? 'نسخه جدیدی از پنل ادمین منتشر شده است'
|
|
: 'خطا در بارگذاری بخش مورد نظر'}
|
|
</h3>
|
|
<p className="text-xs text-gray-500 font-medium leading-relaxed">
|
|
{this.state.isChunkLoadError
|
|
? 'فایلهای جاوااسکریپت این بخش روی سرور بهروزرسانی شدهاند. برای بارگذاری نسخه جدید دکمه بروزرسانی را بزنید.'
|
|
: this.state.error?.message || 'یک خطای غیرمنتظره در بارگذاری صفحه رخ داد.'}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-center gap-3 pt-2">
|
|
<button
|
|
onClick={this.handleReload}
|
|
className="bg-purple-600 hover:bg-purple-700 text-white text-xs font-bold py-2.5 px-5 rounded-xl transition-all flex items-center gap-2 shadow-md shadow-purple-200 cursor-pointer"
|
|
>
|
|
<RefreshCw className="w-4 h-4" />
|
|
<span>بروزرسانی صفحه</span>
|
|
</button>
|
|
|
|
<button
|
|
onClick={this.handleGoHome}
|
|
className="bg-gray-100 hover:bg-gray-200 text-gray-700 text-xs font-bold py-2.5 px-4 rounded-xl transition-all flex items-center gap-1.5 cursor-pointer"
|
|
>
|
|
<Home className="w-4 h-4" />
|
|
<span>داشبورد</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|