-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.tsx
More file actions
95 lines (85 loc) · 2.91 KB
/
Copy pathError.tsx
File metadata and controls
95 lines (85 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { useCallback, type FC, useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'
import { Box, Typography, Button } from '@mui/material'
import { useLogger } from '../Hooks/Logger'
import { clearMatchState } from '../Store/match'
import { clearPlayerState } from '../Store/player'
import { clearSseState } from '../Store/sse'
import { clearStatsState } from '../Store/stats'
import { clearTeamState } from '../Store/team'
import { clearUserState, logout } from '../Store/users'
import { clearUtilsState } from '../Store/util'
import { useAppDispatch } from '../Utils/store'
import ErrorLayout from '../Components/ErrorLayout'
import * as ls from '../Utils/ls'
const ErrorBoundary: FC<WithChildren> = ({ children }) => {
const { t } = useTranslation()
const navigate = useNavigate()
const dispatch = useAppDispatch()
const Logger = useLogger()
const [errorMessage, setErrorMessage] = useState<string | null>(null)
const resetErrorBoundary = useCallback(() => {
dispatch(clearUserState())
dispatch(clearPlayerState())
dispatch(clearUtilsState())
dispatch(clearTeamState())
dispatch(clearMatchState())
dispatch(clearStatsState())
dispatch(clearSseState())
dispatch(logout())
setErrorMessage(null)
ls.del('tableFootball')
navigate('/login')
}, [dispatch, navigate])
const clearError = useCallback(() => {
setErrorMessage(null)
navigate('/login')
}, [navigate])
useEffect(() => {
const handleError = (error: ErrorEvent | Event): void => {
error instanceof ErrorEvent ? setErrorMessage(error.message) : setErrorMessage(error.type)
Logger.writeException(new Error((error as ErrorEvent).message))
}
window.addEventListener('error', handleError)
return () => {
window.removeEventListener('error', handleError)
}
}, [Logger])
if (errorMessage !== null) {
return (
<ErrorLayout>
<Box
sx={{
display: 'flex',
flexDirection: 'column'
}}
>
<Typography variant="h1" sx={{ mb: 2.5 }}>
{t('error.title')}
</Typography>
<Typography variant="h5" sx={{ mb: 2.5, fontSize: 'large' }}>
{errorMessage} 👨🏻💻
</Typography>
<Typography variant="body2">{t('error.body')}</Typography>
</Box>
<Box
display='flex'
flexDirection='row'
width='100%'
marginTop='4vh'
justifyContent='space-between'
>
<Button variant="contained" onClick={clearError} sx={{ minWidth: '15vw' }}>
{t('error.button')}
</Button>
<Button variant="contained" onClick={resetErrorBoundary} sx={{ minWidth: '15vw' }}>
{t('error.logout')}
</Button>
</Box>
</ErrorLayout>
)
}
return <>{children}</>
}
export default ErrorBoundary