-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1564 from nextstrain/feat/web-error-react-compone…
…nt-stack
- Loading branch information
Showing
4 changed files
with
47 additions
and
15 deletions.
There are no files selected for viewing
33 changes: 28 additions & 5 deletions
33
packages/nextclade-web/src/components/Error/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,39 @@ | ||
import React, { ReactNode } from 'react' | ||
|
||
import React, { ReactNode, useState, useCallback, useMemo, ErrorInfo } from 'react' | ||
import { ErrorBoundary as ErrorBoundaryBase, FallbackProps } from 'react-error-boundary' | ||
import ErrorPage from 'src/pages/_error' | ||
|
||
export function ErrorFallback({ error }: FallbackProps) { | ||
return <ErrorPage error={error} /> | ||
export interface ExtendedFallbackProps extends FallbackProps { | ||
errorInfo?: ErrorInfo | ||
} | ||
|
||
export function ErrorFallback({ error, errorInfo }: ExtendedFallbackProps) { | ||
return <ErrorPage error={error} errorInfo={errorInfo} /> | ||
} | ||
|
||
export interface ErrorBoundaryProps { | ||
children?: ReactNode | ||
} | ||
|
||
export function ErrorBoundary({ children }: ErrorBoundaryProps) { | ||
return <ErrorBoundaryBase FallbackComponent={ErrorFallback}>{children}</ErrorBoundaryBase> | ||
const [errorInfo, setErrorInfo] = useState<ErrorInfo | undefined>(undefined) | ||
|
||
const FallbackComponent = useMemo(() => { | ||
return function ErrorFallbackMemoized(props: FallbackProps) { | ||
return <ErrorFallback {...props} errorInfo={errorInfo} /> | ||
} | ||
}, [errorInfo]) | ||
|
||
const handleError = useCallback((_: Error, info: ErrorInfo) => { | ||
setErrorInfo(info) | ||
}, []) | ||
|
||
const handleReset = useCallback(() => { | ||
setErrorInfo(undefined) | ||
}, []) | ||
|
||
return ( | ||
<ErrorBoundaryBase FallbackComponent={FallbackComponent} onError={handleError} onReset={handleReset}> | ||
{children} | ||
</ErrorBoundaryBase> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters