ghost-keychain/packages/extension-ui/src/components/ErrorBoundary.tsx
Ryan Lee 727c61663c
chore: update module resolution to bundler (#1387)
* chore: update module resolution to bundler

* fix: linter issues

* fix jest

* fix: build

* use import type

* lint
2024-07-08 15:15:17 -04:00

63 lines
1.5 KiB
TypeScript

// Copyright 2019-2024 @polkadot/extension-ui authors & contributor
// SPDX-License-Identifier: Apache-2.0
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import Header from '../partials/Header';
import Button from './Button';
import ButtonArea from './ButtonArea';
import VerticalSpace from './VerticalSpace';
interface ErrorBoundaryProps {
children: React.ReactNode;
className?: string;
error?: Error | null;
trigger?: string;
}
const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children, error: propError, trigger }) => {
const { t } = useTranslation();
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
if (error !== null && trigger) {
setError(null);
}
}, [error, trigger]);
useEffect(() => {
if (propError) {
setError(propError);
}
}, [propError]);
const goHome = useCallback(() => {
setError(null);
window.location.hash = '/';
}, [setError]);
if (error) {
return (
<>
<Header text={t('An error occurred')} />
<div>
{t('Something went wrong with the query and rendering of this component. {{message}}', {
replace: { message: error.message }
})}
</div>
<VerticalSpace />
<ButtonArea>
<Button onClick={goHome}>
{t('Back to home')}
</Button>
</ButtonArea>
</>
);
}
return <>{children}</>;
};
export default ErrorBoundary;