31 lines
923 B
React
31 lines
923 B
React
import React, { useState } from 'react';
|
|
import Snackbar from '@mui/material/Snackbar';
|
|
import Alert from '@mui/material/Alert';
|
|
|
|
// 模块级单例:admin 任意处调用 showSnack,由 AdminLayout 内的 SnackHost 消费
|
|
let showFn = null;
|
|
|
|
export function showSnack(msg, severity = 'success') {
|
|
if (showFn) showFn(msg, severity);
|
|
else window.alert(msg);
|
|
}
|
|
|
|
export default function SnackHost() {
|
|
const [open, setOpen] = useState(false);
|
|
const [msg, setMsg] = useState('');
|
|
const [severity, setSeverity] = useState('success');
|
|
|
|
showFn = (m, s) => { setMsg(m); setSeverity(s || 'success'); setOpen(true); };
|
|
|
|
return (
|
|
<Snackbar
|
|
open={open}
|
|
autoHideDuration={2500}
|
|
onClose={() => setOpen(false)}
|
|
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
|
>
|
|
<Alert severity={severity} variant="filled" onClose={() => setOpen(false)}>{msg}</Alert>
|
|
</Snackbar>
|
|
);
|
|
}
|