[#121] added error handler and snackbar
This commit is contained in:
committed by
Benoit TELLIER
parent
f498309f44
commit
a00f179dbd
@@ -0,0 +1,38 @@
|
||||
import Snackbar from "@mui/material/Snackbar";
|
||||
import Alert from "@mui/material/Alert";
|
||||
import Button from "@mui/material/Button";
|
||||
|
||||
interface Props {
|
||||
messages: string[];
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
export function EventErrorSnackbar({ messages, onClose }: Props) {
|
||||
const open = messages.length > 0;
|
||||
const summary =
|
||||
messages.length === 1
|
||||
? messages[0]
|
||||
: `${messages.length} events with errors`;
|
||||
|
||||
return (
|
||||
<Snackbar
|
||||
open={open}
|
||||
autoHideDuration={6000}
|
||||
onClose={onClose}
|
||||
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
|
||||
>
|
||||
<Alert
|
||||
severity="error"
|
||||
onClose={onClose}
|
||||
sx={{ width: "100%" }}
|
||||
action={
|
||||
<Button color="inherit" size="small" onClick={onClose}>
|
||||
OK
|
||||
</Button>
|
||||
}
|
||||
>
|
||||
{summary}
|
||||
</Alert>
|
||||
</Snackbar>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
export class EventErrorHandler {
|
||||
private errors = new Map<string, string>();
|
||||
private onErrorCallback?: (messages: string[]) => void;
|
||||
|
||||
setErrorCallback(callback: (messages: string[]) => void) {
|
||||
this.onErrorCallback = callback;
|
||||
}
|
||||
|
||||
reportError(eventId: string, message: string) {
|
||||
if (!this.errors.has(eventId)) {
|
||||
this.errors.set(eventId, message);
|
||||
console.warn(`[EventErrorHandler] ${eventId}: ${message}`);
|
||||
this.emit();
|
||||
}
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
this.errors.clear();
|
||||
this.emit();
|
||||
}
|
||||
|
||||
private emit() {
|
||||
this.onErrorCallback?.(Array.from(this.errors.values()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user