Files
explore/ui/shared/ErrorBoundary.tsx
T
tom goriunovandGitHub acb6cb44d6 navigation doesn't work on error pages (#1519)
* navigation doesn't work on error pages

Fixes #1518

* unify API resource thrown errors and disable Sentry logging for them

* test fix
2024-01-22 17:36:30 +04:00

56 lines
1.3 KiB
TypeScript

import type { NextRouter } from 'next/router';
import { useRouter } from 'next/router';
import React from 'react';
interface Props {
children: React.ReactNode;
renderErrorScreen: (error?: Error) => React.ReactNode;
onError?: (error: Error) => void;
}
interface PropsWithRouter extends Props {
router: NextRouter;
}
interface State {
hasError: boolean;
error?: Error;
errorPathname?: string;
}
class ErrorBoundary extends React.PureComponent<PropsWithRouter, State> {
state: State = {
hasError: false,
};
static getDerivedStateFromProps(props: PropsWithRouter, state: State) {
if (state.hasError && state.errorPathname) {
if (props.router.pathname !== state.errorPathname) {
return { hasError: false, error: undefined, errorPathname: undefined };
}
}
return null;
}
componentDidCatch(error: Error) {
this.setState({ hasError: true, error, errorPathname: this.props.router.pathname });
this.props.onError?.(error);
}
render() {
if (this.state.hasError) {
return this.props.renderErrorScreen(this.state.error);
}
return this.props.children;
}
}
const WrappedErrorBoundary = (props: Props) => {
const router = useRouter();
return <ErrorBoundary { ...props } router={ router }/>;
};
export default React.memo(WrappedErrorBoundary);