2022-11-25 20:25:03 +00:00
|
|
|
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { Component } from 'react';
|
2020-10-23 04:18:46 +00:00
|
|
|
import tw from 'twin.macro';
|
2022-11-25 20:25:03 +00:00
|
|
|
|
2020-10-23 04:18:46 +00:00
|
|
|
import Icon from '@/components/elements/Icon';
|
2022-11-25 20:25:03 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
children?: ReactNode;
|
|
|
|
}
|
2020-10-23 04:18:46 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
hasError: boolean;
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
class ErrorBoundary extends Component<Props, State> {
|
|
|
|
override state: State = {
|
2020-10-23 04:18:46 +00:00
|
|
|
hasError: false,
|
|
|
|
};
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
static getDerivedStateFromError() {
|
2020-10-23 04:18:46 +00:00
|
|
|
return { hasError: true };
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override componentDidCatch(error: Error) {
|
2020-10-23 04:18:46 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
override render() {
|
2022-06-26 19:13:52 +00:00
|
|
|
return this.state.hasError ? (
|
2020-10-23 04:18:46 +00:00
|
|
|
<div css={tw`flex items-center justify-center w-full my-4`}>
|
|
|
|
<div css={tw`flex items-center bg-neutral-900 rounded p-3 text-red-500`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Icon icon={faExclamationTriangle} css={tw`h-4 w-auto mr-2`} />
|
2022-11-25 20:25:03 +00:00
|
|
|
|
2020-10-23 04:18:46 +00:00
|
|
|
<p css={tw`text-sm text-neutral-100`}>
|
|
|
|
An error was encountered by the application while rendering this view. Try refreshing the page.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
|
|
|
this.props.children
|
|
|
|
);
|
2020-10-23 04:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ErrorBoundary;
|