Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error boundary #1331

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions idea/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.10",
"react-final-form": "^6.5.9",
"react-final-form-listeners": "^1.0.3",
"react-number-format": "^4.9.3",
Expand Down
11 changes: 9 additions & 2 deletions idea/frontend/src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useLocation, useSearchParams } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary';
import { useAccount, useApi } from '@gear-js/react-hooks';
import 'simplebar-react/dist/simplebar.min.css';

Expand All @@ -11,12 +12,14 @@ import { MobileDisclaimer } from 'widgets/mobileDisclaimer';
import { Routing } from 'pages';
import { LocalStorage, NODE_ADRESS_URL_PARAM } from 'shared/config';
import { Loader } from 'shared/ui/loader';
import { ErrorFallback } from 'shared/ui/errorFallback';

import { withProviders } from './providers';
import './App.scss';

const App = withProviders(() => {
const { nodeAddress } = useApp();
const { pathname } = useLocation();
const [searchParams, setSearchParams] = useSearchParams();

const { api, isApiReady } = useApi();
Expand Down Expand Up @@ -52,7 +55,11 @@ const App = withProviders(() => {
<Menu />
<div className="content">
<Header />
{isAppReady ? <Routing /> : <Loader />}

{/* key to reset on route change */}
<ErrorBoundary key={pathname} fallbackRender={ErrorFallback}>
{isAppReady ? <Routing /> : <Loader />}
</ErrorBoundary>
</div>

{isMobileDisclaimerVisible && <MobileDisclaimer onCloseButtonClick={closeMobileDisclaimer} />}
Expand Down
23 changes: 15 additions & 8 deletions idea/frontend/src/hooks/useChangeEffect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useEffect, useRef } from 'react';
import { DependencyList, EffectCallback, useEffect, useRef } from 'react';

export const useChangeEffect = (callback: () => void, dependencies?: unknown[]) => {
const isMounted = useRef(false);
export const useChangeEffect = (callback: EffectCallback, dependencies: DependencyList) => {
const mounted = useRef(false);

useEffect(
() => () => {
mounted.current = false;
},
[],
);

useEffect(() => {
if (isMounted.current) {
callback();
if (mounted.current) {
return callback();
}

return () => {
isMounted.current = true;
};
mounted.current = true;
return undefined;

// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.heading {
margin-bottom: 8px;
}

.error {
margin-bottom: 32px;
color: #f24a4a;
}
16 changes: 16 additions & 0 deletions idea/frontend/src/shared/ui/errorFallback/ErrorFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FallbackProps } from 'react-error-boundary';

import { Subheader } from '../subheader';
import { BackButton } from '../backButton';
import styles from './ErrorFallback.module.scss';

const ErrorFallback = ({ error }: FallbackProps) => (
<>
<Subheader title="An unexpected error occured:" />
<p className={styles.error}>{error.message}</p>

<BackButton />
</>
);

export { ErrorFallback };
3 changes: 3 additions & 0 deletions idea/frontend/src/shared/ui/errorFallback/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ErrorFallback } from './ErrorFallback';

export { ErrorFallback };
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,7 @@ __metadata:
react-dnd: ^16.0.1
react-dnd-html5-backend: ^16.0.1
react-dom: ^18.2.0
react-error-boundary: ^4.0.10
react-final-form: ^6.5.9
react-final-form-listeners: ^1.0.3
react-number-format: ^4.9.3
Expand Down Expand Up @@ -16743,6 +16744,17 @@ __metadata:
languageName: node
linkType: hard

"react-error-boundary@npm:^4.0.10":
version: 4.0.10
resolution: "react-error-boundary@npm:4.0.10"
dependencies:
"@babel/runtime": ^7.12.5
peerDependencies:
react: ">=16.13.1"
checksum: 4ad4864d2a5fc2264a24d03e83176e6a70d7adbe3c1edbdc5b0bd452a695104bc59456e23b5aea1b9729220672fe46614221daa8b3bd59327968d4aa7eb8bc71
languageName: node
linkType: hard

"react-error-overlay@npm:^6.0.11":
version: 6.0.11
resolution: "react-error-overlay@npm:6.0.11"
Expand Down
Loading