|
1 | | -import { FC, ReactNode, useState } from 'react'; |
2 | | -import { useTranslation } from 'react-i18next'; |
| 1 | +import { FC, ReactNode } from 'react'; |
3 | 2 | import * as Sentry from '@sentry/react'; |
4 | | -import Container from '@mui/material/Container'; |
5 | | -import Alert from '@mui/material/Alert'; |
6 | | - |
7 | | -import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; |
8 | | -import Accordion from '@mui/material/Accordion'; |
9 | | -import AccordionSummary from '@mui/material/AccordionSummary'; |
10 | | -import AccordionDetails from '@mui/material/AccordionDetails'; |
11 | | -import { |
12 | | - Box, |
13 | | - Button, |
14 | | - Paper, |
15 | | - Stack, |
16 | | - TextField, |
17 | | - Typography, |
18 | | - useTheme, |
19 | | -} from '@mui/material'; |
20 | | -import SendIcon from '@mui/icons-material/Send'; |
21 | | -import WarningIcon from '@mui/icons-material/WarningAmber'; |
22 | | - |
23 | | -interface ErrorFallbackProps { |
24 | | - error: Error; |
25 | | - componentStack: string; |
26 | | - eventId: string; |
27 | | - // resetError(): void; |
28 | | -} |
| 3 | +import { ErrorFallback } from '@graasp/ui/apps'; |
| 4 | +import { useTranslation } from 'react-i18next'; |
29 | 5 |
|
30 | | -const ErrorFallback: FC<ErrorFallbackProps> = ({ |
31 | | - error, |
32 | | - componentStack, |
33 | | - eventId /* , resetError */, |
34 | | -}) => { |
35 | | - const { t } = useTranslation('translations', { |
| 6 | +const ErrorBoundary: FC<{ children?: ReactNode }> = ({ children }) => { |
| 7 | + const { t: tFallback } = useTranslation('translations', { |
36 | 8 | keyPrefix: 'ERROR_BOUNDARY.FALLBACK', |
37 | 9 | }); |
38 | | - const [name, setName] = useState(''); |
39 | | - const [email, setEmail] = useState(''); |
40 | | - const [comment, setComment] = useState(''); |
41 | | - const [feedbackGiven, setFeedbackGiven] = useState(false); |
42 | | - |
43 | | - const theme = useTheme(); |
44 | | - |
45 | | - const sendUserFeedback = (): void => { |
46 | | - const userFeedback = { |
47 | | - event_id: eventId, |
48 | | - name, |
49 | | - email, |
50 | | - comments: comment, |
51 | | - }; |
52 | | - Sentry.captureUserFeedback(userFeedback); |
53 | | - setFeedbackGiven(true); |
54 | | - }; |
55 | 10 | return ( |
56 | | - <Container> |
57 | | - <Paper variant="outlined" sx={{ p: 4 }}> |
58 | | - <Stack direction="column" spacing={1}> |
59 | | - <Stack direction="row" spacing={2}> |
60 | | - <WarningIcon sx={{ color: theme.palette.error.main }} /> |
61 | | - <Typography variant="h2" color={theme.palette.error.dark}> |
62 | | - {t('MESSAGE_TITLE')} |
63 | | - </Typography> |
64 | | - </Stack> |
65 | | - {feedbackGiven ? ( |
66 | | - <Alert severity="success">{t('THANKS_FOR_FEEDBACK')}</Alert> |
67 | | - ) : ( |
68 | | - <Box id="user-feedback"> |
69 | | - <Stack direction="column" spacing={1} maxWidth="82rem"> |
70 | | - <Typography variant="body1">{t('MESSAGE_FEEDBACK')}</Typography> |
71 | | - <TextField |
72 | | - value={name} |
73 | | - onChange={(e) => setName(e.target.value)} |
74 | | - label={t('NAME_LABEL')} |
75 | | - helperText={t('NAME_HELPER')} |
76 | | - /> |
77 | | - <TextField |
78 | | - value={email} |
79 | | - onChange={(e) => setEmail(e.target.value)} |
80 | | - label={t('EMAIL_LABEL')} |
81 | | - helperText={t('EMAIL_HELPER')} |
82 | | - /> |
83 | | - <TextField |
84 | | - multiline |
85 | | - rows={5} |
86 | | - value={comment} |
87 | | - onChange={(e) => setComment(e.target.value)} |
88 | | - label={t('COMMENT_LABEL')} |
89 | | - helperText={t('COMMENT_HELPER')} |
90 | | - /> |
91 | | - <Box> |
92 | | - <Button startIcon={<SendIcon />} onClick={sendUserFeedback}> |
93 | | - {t('SEND')} |
94 | | - </Button> |
95 | | - </Box> |
96 | | - </Stack> |
97 | | - </Box> |
98 | | - )} |
99 | | - <Accordion> |
100 | | - <AccordionSummary |
101 | | - expandIcon={<ExpandMoreIcon />} |
102 | | - aria-controls="error-details" |
103 | | - id="error-details" |
104 | | - > |
105 | | - {t('ERROR_DETAILS')} |
106 | | - </AccordionSummary> |
107 | | - <AccordionDetails> |
108 | | - <Typography variant="caption">{error.toString()}</Typography> |
109 | | - <Typography variant="caption">{componentStack}</Typography> |
110 | | - </AccordionDetails> |
111 | | - </Accordion> |
112 | | - </Stack> |
113 | | - </Paper> |
114 | | - </Container> |
| 11 | + <Sentry.ErrorBoundary |
| 12 | + // eslint-disable-next-line react/no-unstable-nested-components |
| 13 | + fallback={({ error, componentStack, eventId }) => ( |
| 14 | + <ErrorFallback |
| 15 | + error={error} |
| 16 | + componentStack={componentStack} |
| 17 | + eventId={eventId} |
| 18 | + captureUserFeedback={Sentry.captureUserFeedback} |
| 19 | + title={tFallback('MESSAGE_TITLE')} |
| 20 | + formTitle={tFallback('MESSAGE_FEEDBACK')} |
| 21 | + nameLabel={tFallback('NAME_LABEL')} |
| 22 | + nameHelper={tFallback('NAME_HELPER')} |
| 23 | + emailLabel={tFallback('EMAIL_LABEL')} |
| 24 | + emailHelper={tFallback('EMAIL_HELPER')} |
| 25 | + commentLabel={tFallback('COMMENT_LABEL')} |
| 26 | + commentHelper={tFallback('COMMENT_HELPER')} |
| 27 | + thanksMessage={tFallback('THANKS_FOR_FEEDBACK')} |
| 28 | + sendButtonLabel={tFallback('SEND')} |
| 29 | + errorDetailsLabel={tFallback('ERROR_DETAILS')} |
| 30 | + /> |
| 31 | + )} |
| 32 | + > |
| 33 | + {children} |
| 34 | + </Sentry.ErrorBoundary> |
115 | 35 | ); |
116 | 36 | }; |
117 | 37 |
|
118 | | -const ErrorBoundary: FC<{ children?: ReactNode }> = ({ children }) => ( |
119 | | - <Sentry.ErrorBoundary |
120 | | - // fallback={({ error, componentStack, eventId }) => ( |
121 | | - // <ErrorFallback |
122 | | - // error={error} |
123 | | - // componentStack={componentStack} |
124 | | - // eventId={eventId} |
125 | | - // /> |
126 | | - // )} |
127 | | - // eslint-disable-next-line react/no-unstable-nested-components |
128 | | - fallback={({ error, componentStack, eventId }) => ( |
129 | | - <ErrorFallback |
130 | | - error={error} |
131 | | - componentStack={componentStack} |
132 | | - eventId={eventId} |
133 | | - /> |
134 | | - )} |
135 | | - > |
136 | | - {children} |
137 | | - </Sentry.ErrorBoundary> |
138 | | -); |
139 | | - |
140 | 38 | export default ErrorBoundary; |
0 commit comments