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

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

import ErrorFallback from './ErrorFallback';

type ErrorBoundaryProps = React.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;
