"use client"; import { Component, type ErrorInfo, type ReactNode } from "react"; import { IconAlertTriangle } from "@tabler/icons-react"; import { t } from "@/lib/i18n"; type DiffErrorBoundaryProps = { filePath: string; children: ReactNode; }; type DiffErrorBoundaryState = { error: Error | null; }; /** * Catches throws from @pierre/diffs renderers so a single malformed diff * (e.g. stale cached hunk header vs live working-tree content for an * untracked file that was edited after the snapshot) cannot tear down * the whole review list. Renders a small per-file fallback instead. */ export class DiffErrorBoundary extends Component { state: DiffErrorBoundaryState = { error: null }; static getDerivedStateFromError(error: Error): DiffErrorBoundaryState { return { error }; } componentDidCatch(error: Error, info: ErrorInfo): void { console.error("[DiffErrorBoundary]", this.props.filePath, error, info.componentStack); } componentDidUpdate(prevProps: DiffErrorBoundaryProps): void { if (prevProps.filePath !== this.props.filePath && this.state.error) { this.setState({ error: null }); } } render(): ReactNode { if (this.state.error) { return (
{t("diff:unableToRenderDiffForThis")}
); } return this.props.children; } }