Spaces:
Running
Running
Create shared/ErrorBoundary.tsx
Browse files
components/shared/ErrorBoundary.tsx
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use client'
|
| 2 |
+
|
| 3 |
+
import React, { Component, ErrorInfo, ReactNode } from 'react'
|
| 4 |
+
import { AlertCircle, RefreshCw, Home } from 'lucide-react'
|
| 5 |
+
|
| 6 |
+
interface Props {
|
| 7 |
+
children: ReactNode
|
| 8 |
+
name?: string
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
interface State {
|
| 12 |
+
hasError: boolean
|
| 13 |
+
error: Error | null
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
export class ErrorBoundary extends Component<Props, State> {
|
| 17 |
+
public state: State = {
|
| 18 |
+
hasError: false,
|
| 19 |
+
error: null
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
public static getDerivedStateFromError(error: Error): State {
|
| 23 |
+
return { hasError: true, error }
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
| 27 |
+
console.error(`Uncaught error in ${this.props.name || 'Component'}:`, error, errorInfo)
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
public render() {
|
| 31 |
+
if (this.state.hasError) {
|
| 32 |
+
return (
|
| 33 |
+
<div className="p-8 rounded-3xl bg-red-500/5 border border-red-500/20 text-center space-y-4 my-4">
|
| 34 |
+
<div className="w-16 h-16 rounded-full bg-red-500/20 flex items-center justify-center mx-auto shadow-lg shadow-red-500/10">
|
| 35 |
+
<AlertCircle className="text-red-400" size={32} />
|
| 36 |
+
</div>
|
| 37 |
+
<div className="space-y-2">
|
| 38 |
+
<h2 className="text-xl font-black theme-title uppercase tracking-tight">Oups ! Une erreur est survenue</h2>
|
| 39 |
+
<p className="text-sm theme-description opacity-70 max-w-md mx-auto leading-relaxed">
|
| 40 |
+
L'interface a rencontré une difficulté technique (React Error #31 ou similaire).
|
| 41 |
+
Cela arrive souvent lors d'un changement de structure de données.
|
| 42 |
+
</p>
|
| 43 |
+
</div>
|
| 44 |
+
|
| 45 |
+
{this.state.error && (
|
| 46 |
+
<div className="bg-black/40 p-4 rounded-2xl border border-white/5 text-left overflow-auto max-h-40 scrollbar-thin">
|
| 47 |
+
<p className="text-[10px] font-mono text-red-300 whitespace-pre-wrap">
|
| 48 |
+
{this.state.error.toString()}
|
| 49 |
+
</p>
|
| 50 |
+
</div>
|
| 51 |
+
)}
|
| 52 |
+
|
| 53 |
+
<div className="flex items-center justify-center gap-3 pt-4">
|
| 54 |
+
<button
|
| 55 |
+
onClick={() => window.location.reload()}
|
| 56 |
+
className="flex items-center gap-2 px-6 py-2.5 bg-red-500 text-white rounded-xl text-xs font-black uppercase tracking-widest hover:bg-red-600 transition-all shadow-lg shadow-red-500/20"
|
| 57 |
+
>
|
| 58 |
+
<RefreshCw size={16} />
|
| 59 |
+
Recharger la page
|
| 60 |
+
</button>
|
| 61 |
+
<button
|
| 62 |
+
onClick={() => window.location.href = '/'}
|
| 63 |
+
className="flex items-center gap-2 px-6 py-2.5 bg-white/5 border border-white/10 text-white rounded-xl text-xs font-black uppercase tracking-widest hover:bg-white/10 transition-all"
|
| 64 |
+
>
|
| 65 |
+
<Home size={16} />
|
| 66 |
+
Retour Accueil
|
| 67 |
+
</button>
|
| 68 |
+
</div>
|
| 69 |
+
|
| 70 |
+
<p className="text-[10px] opacity-30 italic">
|
| 71 |
+
ID de l'erreur : {this.props.name || 'Global'} - {new Date().toLocaleTimeString()}
|
| 72 |
+
</p>
|
| 73 |
+
</div>
|
| 74 |
+
)
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return this.props.children
|
| 78 |
+
}
|
| 79 |
+
}
|