"use client";
import React, { Component, ErrorInfo, ReactNode } from "react";
import { Button } from "@/components/ui/button";
import { AlertTriangle, Film } from "lucide-react";
interface Props {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, errorInfo: ErrorInfo) => void;
context?: string;
}
interface State {
hasError: boolean;
error: Error | null;
}
/**
* Global Error Boundary component for catching React component errors.
* Use this to wrap client components that might crash, especially the video player.
*
* @example
* ```tsx
*
*
*
* ```
*/
export class ErrorBoundary extends Component {
public state: State = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error(`ErrorBoundary${this.props.context ? ` [${this.props.context}]` : ""} caught an error:`, error, errorInfo);
this.props.onError?.(error, errorInfo);
}
private handleReset = () => {
this.setState({ hasError: false, error: null });
};
private handleReload = () => {
window.location.reload();
};
public render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
);
}
return this.props.children;
}
}
interface ErrorFallbackProps {
error: Error | null;
context?: string;
onReset: () => void;
onReload: () => void;
}
function ErrorFallback({ error, context, onReset, onReload }: ErrorFallbackProps) {
const isVideoContext = context?.toLowerCase().includes("video");
return (
{isVideoContext ? (
<>
>
) : (
)}
{context ? `${context} crashed` : "Something went wrong"}
{isVideoContext
? "The video player encountered an error. Try reloading or go back to the project."
: "An unexpected error occurred. Try resetting the component or reload the page."}