2026-07-28 10:05:08 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-01 22:09:02 +07:00
|
|
|
import { AlertCircle, RefreshCw } from "lucide-react";
|
2026-07-28 10:05:08 +07:00
|
|
|
import { Component, type ReactNode } from "react";
|
|
|
|
|
import { GlassCard } from "@/components/glass/card";
|
|
|
|
|
|
2026-08-01 22:09:02 +07:00
|
|
|
interface Props {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
fallback?: ReactNode;
|
|
|
|
|
}
|
|
|
|
|
interface State {
|
|
|
|
|
hasError: boolean;
|
|
|
|
|
error?: Error;
|
|
|
|
|
}
|
2026-07-28 10:05:08 +07:00
|
|
|
|
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
|
|
|
state: State = { hasError: false };
|
|
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
|
|
|
return { hasError: true, error };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (this.state.hasError) {
|
2026-08-01 22:09:02 +07:00
|
|
|
return (
|
|
|
|
|
this.props.fallback || (
|
|
|
|
|
<GlassCard
|
|
|
|
|
variant="danger"
|
|
|
|
|
className="flex flex-col items-center gap-2 py-8"
|
2026-07-28 10:05:08 +07:00
|
|
|
>
|
2026-08-01 22:09:02 +07:00
|
|
|
<AlertCircle className="size-6 text-destructive" />
|
|
|
|
|
<p className="text-sm text-text-secondary">
|
|
|
|
|
{this.state.error?.message || "Something went wrong"}
|
|
|
|
|
</p>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => this.setState({ hasError: false })}
|
|
|
|
|
className="flex items-center gap-1 text-xs text-primary hover:text-primary/80 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
<RefreshCw className="size-3" /> Try again
|
|
|
|
|
</button>
|
|
|
|
|
</GlassCard>
|
|
|
|
|
)
|
2026-07-28 10:05:08 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.props.children;
|
|
|
|
|
}
|
|
|
|
|
}
|