/
index.tsx
157 lines (136 loc) · 4.04 KB
/
index.tsx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/** @module Components.Assets.Notification.Error */
import { useCallback, useContext, useEffect, useRef } from 'react'
import { App, Button, Collapse, Space, Typography } from 'antd'
import { v4 as uuid } from 'uuid'
import { IErrorItem, NotificationContext } from '@/context/notification'
import { removeError } from '@/context/notification/actions'
import Sentry from '@/lib/sentry'
/**
* Error notification
* @return ErrorNotification
*/
const ErrorNotification: React.FunctionComponent = () => {
// Ref
const opened = useRef<string[]>([])
// Context
const { errors, dispatch } = useContext(NotificationContext)
// Notification
const { notification } = App.useApp()
/**
* On close
* @param key Key
*/
const onClose = useCallback((key: string): void => {
const index = opened.current.findIndex((open) => open === key)
opened.current = [
...opened.current.slice(0, index),
...opened.current.slice(index + 1)
]
}, [])
/**
* Open server error notification
*/
const openServerErrorNotification = useCallback(() => {
const key = 'server_error'
if (opened.current.includes(key)) return
opened.current.push(key)
notification.error({
key,
message: 'Server error',
description:
'Server is disconnected, please check your internet connection.',
duration: 0,
onClose: () => onClose(key)
})
}, [notification, onClose])
/**
* Open generic error notification
* @param error Error
*/
const openGenericErrorNotification = useCallback(
(error: IErrorItem) => {
const key = 'error_' + uuid()
opened.current.push(key)
notification.error({
key,
message: error.title,
description: error.err ? (
<>
<Typography.Text code>{error.err.message}</Typography.Text>
{(error.err.status || error.err.info) && (
<Collapse
items={[
{
key: 'information',
label: 'More information',
children: (
<Space direction="vertical">
{error.err.status && (
<Typography.Text>
Status: {error.err.status}
</Typography.Text>
)}
{error.err.info?.message && (
<Typography.Text>
Description: {error.err.info?.message}
</Typography.Text>
)}
</Space>
)
}
]}
/>
)}
</>
) : null,
duration: 0,
onClose: () => onClose(key)
})
},
[notification, onClose]
)
/**
* Open notification
*/
const openNotification = useCallback(
(error: IErrorItem) => {
if (error.err?.message === 'Failed to fetch')
openServerErrorNotification()
else if (error.display === undefined || error.display)
openGenericErrorNotification(error)
},
[openServerErrorNotification, openGenericErrorNotification]
)
// Notifications
useEffect(() => {
for (const error of errors ?? []) {
openNotification(error)
error.err && console.error(error.err)
error.err && Sentry.captureException(error.err)
dispatch(removeError(error))
}
}, [errors, openNotification, dispatch])
/**
* Close all
*/
const closeAll = useCallback((): void => {
notification.destroy()
opened.current = []
}, [notification])
// Close all
useEffect(() => {
const key = 'close_all'
if (opened.current.length > 1 && !opened.current.includes(key)) {
notification.warning({
key,
message: 'Close all error notifications',
description: <Button onClick={closeAll}>Close all</Button>,
duration: 0,
placement: 'top'
})
opened.current = [...opened.current, key]
}
})
return null
}
export default ErrorNotification