50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { Component, type ReactNode } from "react";
|
|
import Link from "next/link";
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
fallback?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error?: Error;
|
|
}
|
|
|
|
export default class ErrorBoundary extends Component<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props);
|
|
this.state = { hasError: false };
|
|
}
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
componentDidCatch(error: Error, info: { componentStack: string }) {
|
|
console.error("[ErrorBoundary]", error, info.componentStack);
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) return this.props.fallback;
|
|
return (
|
|
<div className="flex flex-col items-center gap-4 py-20 text-center">
|
|
<p className="text-gray-400 text-sm">Une erreur inattendue s'est produite.</p>
|
|
<button
|
|
onClick={() => this.setState({ hasError: false })}
|
|
className="px-4 py-2 rounded-full border border-gray-300 text-sm text-gray-600 hover:bg-gray-50 transition-colors"
|
|
>
|
|
Réessayer
|
|
</button>
|
|
<Link href="/" className="text-xs text-gray-400 hover:text-gray-600 transition-colors">
|
|
← Accueil
|
|
</Link>
|
|
</div>
|
|
);
|
|
}
|
|
return this.props.children;
|
|
}
|
|
}
|