import { Component, type JSX, type ReactNode } from 'react'; import { useAppTranslation } from '@features/localization/renderer'; import { AlertTriangle } from 'lucide-react'; interface DiffErrorBoundaryProps { children: ReactNode; filePath: string; oldString?: string; newString?: string; onRetry?: () => void; } interface DiffErrorBoundaryState { hasError: boolean; error: Error | null; } type ReviewT = ReturnType['t']; interface DiffErrorBoundaryInnerProps extends DiffErrorBoundaryProps { t: ReviewT; } class DiffErrorBoundaryInner extends Component< DiffErrorBoundaryInnerProps, DiffErrorBoundaryState > { constructor(props: DiffErrorBoundaryInnerProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): DiffErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { console.error( '[DiffErrorBoundary] Error rendering diff for', this.props.filePath, error, errorInfo ); } render(): JSX.Element { if (!this.state.hasError) { return this.props.children as JSX.Element; } const { filePath, oldString, newString, onRetry, t } = this.props; const { error } = this.state; return (
{t('review.diffError.title')}

{error?.message ?? t('review.diffError.unexpected')}

{onRetry && ( )}
{(oldString || newString) && (
{t('review.diffError.raw.show')}
{t('review.diffError.raw.file', { file: filePath })}
{oldString && (
{t('review.diffError.raw.original')}
{oldString.slice(0, 2000)}
{oldString.length > 2000 && ( {t('review.diffError.raw.charsTotal', { count: oldString.length })} )}
)} {newString && (
{t('review.diffError.raw.modified')}
{newString.slice(0, 2000)}
{newString.length > 2000 && ( {t('review.diffError.raw.charsTotal', { count: newString.length })} )}
)}
)}
); } } export const DiffErrorBoundary = (props: DiffErrorBoundaryProps): JSX.Element => { const { t } = useAppTranslation('team'); const { children, filePath, newString, oldString, onRetry } = props; return ( {children} ); };