feat(errorboundary): add error boundaryComponent

pull/145/head
Andrei Dekterev 3 years ago
parent 5eeaacde4c
commit e48b890fd7
  1. 38
      src/components/Error/index.tsx
  2. 17
      src/components/Error/styled.tsx
  3. 50
      src/components/ErrorBoundary/index.tsx
  4. 10
      src/components/SimplePopup/index.tsx
  5. 1
      src/config/lexics/indexLexics.tsx
  6. 11
      src/features/App/index.tsx

@ -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

@ -15,6 +15,7 @@ import {
type Props = { type Props = {
buttonName?: string, buttonName?: string,
children?: ReactNode,
headerName?: string, headerName?: string,
icon?: ReactNode, icon?: ReactNode,
isModalOpen: boolean, isModalOpen: boolean,
@ -26,6 +27,7 @@ type Props = {
export const SimplePopup = (props: Props) => { export const SimplePopup = (props: Props) => {
const { const {
buttonName, buttonName,
children,
headerName, headerName,
icon, icon,
isModalOpen, isModalOpen,
@ -47,9 +49,11 @@ export const SimplePopup = (props: Props) => {
</ScHeaderTitle> </ScHeaderTitle>
</Header> </Header>
<ScBody> <ScBody>
<ScText> {children || (
<T9n t={mainText} /> <ScText>
</ScText> <T9n t={mainText} />
</ScText>
)}
</ScBody> </ScBody>
{buttonName {buttonName
&& ( && (

@ -163,6 +163,7 @@ export const indexLexics = {
loading: 3527, loading: 3527,
logout: 4306, logout: 4306,
lost_connection: 15699, lost_connection: 15699,
main_error: 20101,
match_status_finished: 12985, match_status_finished: 12985,
match_status_live: 12984, match_status_live: 12984,
match_status_soon: 12986, match_status_soon: 12986,

@ -23,6 +23,7 @@ import { GlobalStyles } from 'features/GlobalStyles'
import { Theme } from 'features/Theme' import { Theme } from 'features/Theme'
import { UnavailableText } from 'components/UnavailableText' import { UnavailableText } from 'components/UnavailableText'
import ErrorBoundary from 'components/ErrorBoundary'
import { AuthenticatedApp } from './AuthenticatedApp' import { AuthenticatedApp } from './AuthenticatedApp'
import { useAuthStore } from '../AuthStore' import { useAuthStore } from '../AuthStore'
@ -40,10 +41,12 @@ const Main = () => {
// имеется действующий токен // имеется действующий токен
return isToken ? ( return isToken ? (
<QueryClientProvider client={queryClient}> <ErrorBoundary>
<ReactQueryDevtools initialIsOpen={isLocalhost} /> <QueryClientProvider client={queryClient}>
<AuthenticatedApp /> <ReactQueryDevtools initialIsOpen={isLocalhost} />
</QueryClientProvider> <AuthenticatedApp />
</QueryClientProvider>
</ErrorBoundary>
) : null ) : null
} }

Loading…
Cancel
Save