bitburner-src/src/ui/React/Snackbar.tsx

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-10-13 23:25:58 +02:00
import React, { useEffect } from "react";
import { useSnackbar, SnackbarProvider as SB } from "notistack";
2022-01-08 15:59:31 +01:00
import makeStyles from "@mui/styles/makeStyles";
import { EventEmitter } from "../../utils/EventEmitter";
2021-10-13 23:25:58 +02:00
import Alert from "@mui/material/Alert";
import Paper from "@mui/material/Paper";
2022-01-08 15:59:31 +01:00
import { logBoxBaseZIndex } from "./LogBoxManager";
2021-10-13 23:25:58 +02:00
interface IProps {
children: React.ReactNode | React.ReactNode[];
}
export enum ToastVariant {
2022-04-09 05:42:07 +02:00
SUCCESS = "success",
WARNING = "warning",
ERROR = "error",
INFO = "info",
}
2022-01-08 15:59:31 +01:00
const useStyles = makeStyles(() => ({
snackbar: {
// Log popup z-index increments, so let's add a padding to be well above them.
zIndex: `${logBoxBaseZIndex + 1000} !important` as any,
"& .MuiAlert-icon": {
2022-04-07 01:30:08 +02:00
alignSelf: "center",
},
2022-04-07 01:30:08 +02:00
},
2022-01-08 15:59:31 +01:00
}));
2021-10-13 23:25:58 +02:00
export function SnackbarProvider(props: IProps): React.ReactElement {
2022-01-08 15:59:31 +01:00
const classes = useStyles();
2021-10-13 23:25:58 +02:00
return (
2022-04-07 01:30:08 +02:00
<SB
dense
maxSnack={9}
anchorOrigin={{ horizontal: "right", vertical: "bottom" }}
autoHideDuration={2000}
classes={{ containerRoot: classes.snackbar }}
>
2021-10-13 23:25:58 +02:00
{props.children}
</SB>
);
}
export const SnackbarEvents = new EventEmitter<[string | React.ReactNode, ToastVariant, number | null]>();
export function Snackbar(): React.ReactElement {
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
2021-10-13 23:25:58 +02:00
useEffect(() =>
SnackbarEvents.subscribe((s, variant, duration) => {
const id = enqueueSnackbar(<Alert severity={variant}>{s}</Alert>, {
2021-10-13 23:25:58 +02:00
content: (k, m) => <Paper key={k}>{m}</Paper>,
variant: variant,
2021-12-20 19:29:04 +01:00
autoHideDuration: duration,
onClick: () => closeSnackbar(id),
2022-04-07 01:30:08 +02:00
});
}),
);
2021-10-13 23:25:58 +02:00
return <></>;
}