import React, { Component, ErrorInfo, ReactNode } from 'react'; import { AlertCircle, RefreshCw } from 'lucide-react'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { handleError } from '@/lib/errorHandler'; interface ModerationErrorBoundaryProps { children: ReactNode; submissionId?: string; fallback?: ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; } interface ModerationErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } type ErrorWithId = Error & { errorId: string }; /** * Error Boundary for Moderation Queue Components * * Prevents individual queue item render errors from crashing the entire queue. * Shows user-friendly error UI with retry functionality. * * Usage: * ```tsx * * * * ``` */ export class ModerationErrorBoundary extends Component< ModerationErrorBoundaryProps, ModerationErrorBoundaryState > { constructor(props: ModerationErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log to database and get error ID for user reference const errorId = handleError(error, { action: 'Moderation queue item render error', metadata: { submissionId: this.props.submissionId, componentStack: errorInfo.componentStack, }, }); // Update state with error info this.setState({ error: { ...error, errorId } as ErrorWithId, errorInfo, }); // Call optional error handler this.props.onError?.(error, errorInfo); } handleRetry = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError) { // Custom fallback if provided if (this.props.fallback) { return this.props.fallback; } // Default error UI return ( Queue Item Error Failed to render submission

{this.state.error?.message || 'An unexpected error occurred'}

{(this.state.error as ErrorWithId)?.errorId && (

Reference ID: {(this.state.error as ErrorWithId).errorId.slice(0, 8)}

)} {this.props.submissionId && (

Submission ID: {this.props.submissionId}

)}
{process.env.NODE_ENV === 'development' && this.state.errorInfo && (
Show Component Stack
                  {this.state.errorInfo.componentStack}
                
)}
); } return this.props.children; } }