parent
5eeaacde4c
commit
e48b890fd7
@ -0,0 +1,38 @@ |
||||
import { useEffect } from 'react' |
||||
|
||||
import { FadeIn } from 'features/Animation' |
||||
import { T9n } from 'features/T9n' |
||||
|
||||
import { useToggle } from 'hooks' |
||||
|
||||
import { WrapperError } from './styled' |
||||
|
||||
type ErrorInfo = { |
||||
error?: string, |
||||
} |
||||
export const Error = ({ error }: ErrorInfo) => { |
||||
const { |
||||
close, |
||||
isOpen, |
||||
open, |
||||
} = useToggle() |
||||
|
||||
useEffect(() => { |
||||
open() |
||||
const timeOut = setTimeout(close, 5000) |
||||
return () => { |
||||
clearInterval(timeOut) |
||||
} |
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []) |
||||
|
||||
return ( |
||||
isOpen ? ( |
||||
<FadeIn> |
||||
<WrapperError> |
||||
{error ?? <T9n t='main_error' />} |
||||
</WrapperError> |
||||
</FadeIn> |
||||
) : null |
||||
) |
||||
} |
||||
@ -0,0 +1,17 @@ |
||||
import styled from 'styled-components/macro' |
||||
|
||||
export const WrapperError = styled.div` |
||||
position: fixed; |
||||
top: 20px; |
||||
right: 20px; |
||||
width: 390px; |
||||
max-height: 500px; |
||||
border-radius: 10px; |
||||
background-color: #333333; |
||||
color: white; |
||||
padding: 20px; |
||||
font-size: 0.8rem; |
||||
text-align: center; |
||||
z-index: 1000000; |
||||
white-space: pre-wrap; |
||||
` |
||||
@ -0,0 +1,50 @@ |
||||
// eslint-disable react/destructuring-assignment
|
||||
|
||||
import { Component } from 'react' |
||||
|
||||
import type{ ErrorInfo, ReactNode } from 'react' |
||||
|
||||
import { Error } from '../Error' |
||||
|
||||
interface Props { |
||||
children?: ReactNode, |
||||
} |
||||
|
||||
interface State { |
||||
hasError: boolean, |
||||
} |
||||
|
||||
class ErrorBoundary extends Component<Props, State> { |
||||
// eslint-disable-next-line react/state-in-constructor
|
||||
public state: State = { |
||||
hasError: false, |
||||
} |
||||
|
||||
public static getDerivedStateFromError(_: Error): State { |
||||
// Update state so the next render will show the fallback UI.
|
||||
return { hasError: true } |
||||
} |
||||
|
||||
public componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
||||
// eslint-disable-next-line no-console
|
||||
console.error( |
||||
'Uncaught error:', |
||||
error, |
||||
errorInfo, |
||||
) |
||||
} |
||||
|
||||
public render() { |
||||
const { hasError } = this.state |
||||
const { children } = this.props |
||||
|
||||
return ( |
||||
<> |
||||
{hasError && <Error />} |
||||
{children} |
||||
</> |
||||
) |
||||
} |
||||
} |
||||
|
||||
export default ErrorBoundary |
||||
Loading…
Reference in new issue