import { Component, type ReactNode } from "react";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { AlertTriangle, RefreshCw } from "lucide-react";
interface Props {
children: ReactNode;
fallback?: 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 };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
handleReset = () => {
this.setState({ hasError: false, error: null });
};
render() {
if (this.state.hasError) {
if (this.props.fallback) return this.props.fallback;
return (
<div className="flex items-center justify-center min-h-[300px] p-6">
<Card className="max-w-md w-full">
<CardContent className="pt-6">
<div className="flex flex-col items-center text-center gap-4">
<div className="p-3 rounded-full bg-destructive/10">
<AlertTriangle className="h-8 w-8 text-destructive" />
</div>
<div>
<h3 className="font-semibold text-lg mb-1">Something went wrong</h3>
<p className="text-sm text-muted-foreground">
{this.state.error?.message || "An unexpected error occurred"}
</p>
</div>
<Button onClick={this.handleReset} data-testid="button-error-retry">
<RefreshCw className="h-4 w-4 mr-2" />
Try Again
</Button>
</div>
</CardContent>
</Card>
</div>
);
}
return this.props.children;
}
}