mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
feat: add use-notice hook
This commit is contained in:
55
src/utils/use-notice.tsx
Normal file
55
src/utils/use-notice.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { useMemo, useState } from "react";
|
||||
import { IconButton, Slide, Snackbar } from "@mui/material";
|
||||
import { Close } from "@mui/icons-material";
|
||||
|
||||
interface NoticeInstance {
|
||||
info: (msg: string) => void;
|
||||
error: (msg: string) => void;
|
||||
success: (msg: string) => void;
|
||||
}
|
||||
|
||||
const useNotice = () => {
|
||||
const [message, setMessage] = useState("");
|
||||
const [level, setLevel] = useState<"info" | "error" | "success">("info");
|
||||
|
||||
const handleClose = (_e: any, reason: string) => {
|
||||
if (reason !== "clickaway") setMessage("");
|
||||
};
|
||||
|
||||
const element = useMemo(
|
||||
() => (
|
||||
<Snackbar
|
||||
open={!!message}
|
||||
anchorOrigin={{ vertical: "top", horizontal: "right" }}
|
||||
autoHideDuration={3000}
|
||||
onClose={handleClose}
|
||||
message={message}
|
||||
TransitionComponent={(p) => <Slide {...p} direction="left" />}
|
||||
action={
|
||||
<IconButton
|
||||
size="small"
|
||||
color="inherit"
|
||||
onClick={() => setMessage("")}
|
||||
>
|
||||
<Close fontSize="small" />
|
||||
</IconButton>
|
||||
}
|
||||
/>
|
||||
),
|
||||
[message]
|
||||
);
|
||||
|
||||
const instance = (Object.fromEntries(
|
||||
(["info", "error", "success"] as const).map((item) => [
|
||||
item,
|
||||
(msg: string) => {
|
||||
setLevel(item);
|
||||
setMessage(msg);
|
||||
},
|
||||
])
|
||||
) as unknown) as NoticeInstance;
|
||||
|
||||
return [instance, element] as const;
|
||||
};
|
||||
|
||||
export default useNotice;
|
||||
Reference in New Issue
Block a user