import { Component, type ReactNode } from "react";

type PastePreviewErrorBoundaryProps = {
  children: ReactNode;
  onError: () => void;
};

type PastePreviewErrorBoundaryState = {
  hasError: boolean;
};

export default class PastePreviewErrorBoundary extends Component<
  PastePreviewErrorBoundaryProps,
  PastePreviewErrorBoundaryState
> {
  state: PastePreviewErrorBoundaryState = { hasError: false };

  static getDerivedStateFromError(): PastePreviewErrorBoundaryState {
    return { hasError: true };
  }

  componentDidCatch(): void {
    this.props.onError();
  }

  render() {
    if (this.state.hasError) return null;
    return this.props.children;
  }
}
