import { type FC, type PropsWithChildren, useCallback } from 'react';

import { ErrorBoundary as SentryErrorBoundary } from '@sentry/react';

import ErrorFallback from 'src/components/ErrorFallback';

type ErrorBoundaryProps = PropsWithChildren<{
  resetOnLocationChange?: boolean;
}>;

const ErrorBoundary: FC<ErrorBoundaryProps> = ({
  children,
  resetOnLocationChange = false,
}) => {
  const renderFallback = useCallback(
    ({ resetError }: { resetError: () => void }) => (
      <ErrorFallback
        resetError={resetError}
        resetOnLocationChange={resetOnLocationChange}
      />
    ),
    [resetOnLocationChange],
  );

  return (
    <SentryErrorBoundary fallback={renderFallback}>
      {children}
    </SentryErrorBoundary>
  );
};

export default ErrorBoundary;
