114 lines
3.5 KiB
TypeScript
114 lines
3.5 KiB
TypeScript
"use client";
|
|
|
|
import React, { Component, ErrorInfo, ReactNode } from "react";
|
|
import { motion } from "motion/react";
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
fallback?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error?: Error;
|
|
}
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
public state: State = {
|
|
hasError: false,
|
|
};
|
|
|
|
public static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error("Uncaught error:", error, errorInfo);
|
|
}
|
|
|
|
private handleReset = () => {
|
|
this.setState({ hasError: false, error: undefined });
|
|
};
|
|
|
|
public render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) {
|
|
return this.props.fallback;
|
|
}
|
|
|
|
return (
|
|
<motion.div
|
|
className="flex min-h-screen items-center justify-center p-4"
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
transition={{ duration: 0.5 }}
|
|
>
|
|
<div className="text-center space-y-4 max-w-md">
|
|
<motion.div
|
|
initial={{ scale: 0.8 }}
|
|
animate={{ scale: 1 }}
|
|
transition={{ delay: 0.2, type: "spring", stiffness: 200 }}
|
|
>
|
|
<svg
|
|
className="w-16 h-16 mx-auto text-red-500"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
viewBox="0 0 24 24"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
|
/>
|
|
</svg>
|
|
</motion.div>
|
|
|
|
<motion.h2
|
|
className="text-xl font-semibold text-neutral-900 dark:text-neutral-100"
|
|
initial={{ y: 20, opacity: 0 }}
|
|
animate={{ y: 0, opacity: 1 }}
|
|
transition={{ delay: 0.3 }}
|
|
>
|
|
Something went wrong
|
|
</motion.h2>
|
|
|
|
<motion.p
|
|
className="text-neutral-600 dark:text-neutral-400"
|
|
initial={{ y: 20, opacity: 0 }}
|
|
animate={{ y: 0, opacity: 1 }}
|
|
transition={{ delay: 0.4 }}
|
|
>
|
|
{this.state.error?.message || "An unexpected error occurred"}
|
|
</motion.p>
|
|
|
|
<motion.div
|
|
initial={{ y: 20, opacity: 0 }}
|
|
animate={{ y: 0, opacity: 1 }}
|
|
transition={{ delay: 0.5 }}
|
|
className="space-y-2"
|
|
>
|
|
<button
|
|
onClick={this.handleReset}
|
|
className="px-4 py-2 bg-neutral-900 dark:bg-neutral-100 text-neutral-100 dark:text-neutral-900 rounded-md hover:bg-neutral-800 dark:hover:bg-neutral-200 transition-colors"
|
|
>
|
|
Try again
|
|
</button>
|
|
|
|
<button
|
|
onClick={() => window.location.reload()}
|
|
className="px-4 py-2 text-neutral-600 dark:text-neutral-400 hover:text-neutral-900 dark:hover:text-neutral-100 transition-colors"
|
|
>
|
|
Reload page
|
|
</button>
|
|
</motion.div>
|
|
</div>
|
|
</motion.div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|