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";
|
2026-08-07 17:33:12 +07:00
|
|
|
import { Card } from "@/components/ui/card";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-07-28 10:05:08 +07:00
|
|
|
|
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 || (
|
2026-08-07 17:33:12 +07:00
|
|
|
<Card
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex flex-col items-center gap-2 py-8",
|
|
|
|
|
"border border-red-500/30 ring-red-500/20",
|
|
|
|
|
"[--card-spacing:0px]",
|
|
|
|
|
"rounded-2xl",
|
|
|
|
|
)}
|
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>
|
2026-08-07 17:33:12 +07:00
|
|
|
</Card>
|
2026-08-01 22:09:02 +07:00
|
|
|
)
|
2026-07-28 10:05:08 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.props.children;
|
|
|
|
|
}
|
|
|
|
|
}
|