Skip to content

Commit

Permalink
Handle multiline error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Jan 16, 2025
1 parent 69f8cf9 commit 3d5ae0a
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/common/components/ErrorHandler.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
import { Snackbar, Alert } from '@mui/material';
import React from 'react';
import { Snackbar, Alert, Button, Link, Dialog, DialogContent, DialogContentText, DialogActions, Typography } from '@mui/material';

Check warning on line 1 in src/common/components/ErrorHandler.jsx

View workflow job for this annotation

GitHub Actions / build

Expected a line break after this opening brace

Check warning on line 1 in src/common/components/ErrorHandler.jsx

View workflow job for this annotation

GitHub Actions / build

Expected a line break before this closing brace
import React, { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { usePrevious } from '../../reactHelper';
import { errorsActions } from '../../store';
import { useTranslation } from './LocalizationProvider';

const ErrorHandler = () => {
const dispatch = useDispatch();
const t = useTranslation();

const error = useSelector((state) => state.errors.errors.find(() => true));
const previousError = usePrevious(error);
const cachedError = usePrevious(error);

const message = error || cachedError;
const multiline = message?.includes('\n');
const displayMessage = multiline ? message.split('\n')[0] : message;

const [expanded, setExpanded] = useState(false);

return (
<Snackbar open={!!error}>
<Alert
elevation={6}
onClose={() => dispatch(errorsActions.pop())}
severity="error"
variant="filled"
<>
<Snackbar open={Boolean(error) && !expanded}>
<Alert
elevation={6}
onClose={() => dispatch(errorsActions.pop())}
severity="error"
variant="filled"
>
{displayMessage} | <Link color="inherit" href="#" onClick={() => setExpanded(true)}>{t('sharedShowDetails')}</Link>

Check failure on line 30 in src/common/components/ErrorHandler.jsx

View workflow job for this annotation

GitHub Actions / build

` | ` must be placed on a new line

Check failure on line 30 in src/common/components/ErrorHandler.jsx

View workflow job for this annotation

GitHub Actions / build

`Link` must be placed on a new line
</Alert>
</Snackbar>
<Dialog
open={expanded}
onClose={() => setExpanded(false)}
maxWidth={false}
>
{error || previousError}
</Alert>
</Snackbar>
<DialogContent>
<DialogContentText>
<Typography variant="caption">
<pre>{message}</pre>
</Typography>
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setExpanded(false)} autoFocus>{t('sharedHide')}</Button>
</DialogActions>
</Dialog>
</>
);
};

Expand Down

0 comments on commit 3d5ae0a

Please sign in to comment.