mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2024-11-16 11:42:21 +08:00
feat: add use-notice hook
This commit is contained in:
parent
48f1b27d93
commit
97f2bc8761
|
@ -1,71 +1,45 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import {
|
import { Box, Button, TextField, Typography } from "@mui/material";
|
||||||
Box,
|
|
||||||
Button,
|
|
||||||
Grid,
|
|
||||||
Slide,
|
|
||||||
Snackbar,
|
|
||||||
TextField,
|
|
||||||
Typography,
|
|
||||||
} from "@mui/material";
|
|
||||||
import { importProfile } from "../services/command";
|
import { importProfile } from "../services/command";
|
||||||
|
import useNotice from "../utils/use-notice";
|
||||||
|
|
||||||
const RulesPage = () => {
|
const RulesPage = () => {
|
||||||
const [url, setUrl] = useState("");
|
const [url, setUrl] = useState("");
|
||||||
const [message, setMessage] = useState("");
|
|
||||||
const [disabled, setDisabled] = useState(false);
|
const [disabled, setDisabled] = useState(false);
|
||||||
|
|
||||||
|
const [notice, noticeElement] = useNotice();
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
setUrl("");
|
setUrl("");
|
||||||
setDisabled(true);
|
setDisabled(true);
|
||||||
importProfile(url)
|
importProfile(url)
|
||||||
.then(() => setMessage("Successfully import profile."))
|
.then(() => notice.success("Successfully import profile."))
|
||||||
.catch(() => setMessage("Failed to import profile."))
|
.catch(() => notice.error("Failed to import profile."))
|
||||||
.finally(() => setDisabled(false));
|
.finally(() => setDisabled(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>
|
<Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>
|
||||||
<Typography variant="h4" component="h1" sx={{ py: 2 }}>
|
<Typography variant="h4" component="h1" sx={{ py: 2, mb: 1 }}>
|
||||||
Rules
|
Rules
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|
||||||
<Grid
|
<Box sx={{ display: "flex" }}>
|
||||||
container
|
<TextField
|
||||||
spacing={2}
|
label="Profile URL"
|
||||||
justifyContent="space-between"
|
size="small"
|
||||||
alignItems="center"
|
fullWidth
|
||||||
>
|
value={url}
|
||||||
<Grid item xs={9}>
|
onChange={(e) => setUrl(e.target.value)}
|
||||||
<TextField
|
sx={{ mr: 4 }}
|
||||||
label="Profile Url"
|
/>
|
||||||
size="medium"
|
<Button disabled={disabled} variant="contained" onClick={onClick}>
|
||||||
fullWidth
|
Import
|
||||||
value={url}
|
</Button>
|
||||||
onChange={(e) => setUrl(e.target.value)}
|
</Box>
|
||||||
/>
|
|
||||||
</Grid>
|
|
||||||
<Grid item>
|
|
||||||
<Button
|
|
||||||
disabled={disabled}
|
|
||||||
size="large"
|
|
||||||
variant="contained"
|
|
||||||
onClick={onClick}
|
|
||||||
>
|
|
||||||
Import
|
|
||||||
</Button>
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<Snackbar
|
{noticeElement}
|
||||||
open={!!message}
|
|
||||||
anchorOrigin={{ vertical: "top", horizontal: "right" }}
|
|
||||||
autoHideDuration={3000}
|
|
||||||
onClose={() => setMessage("")}
|
|
||||||
message={message}
|
|
||||||
TransitionComponent={(p) => <Slide {...p} direction="left" />}
|
|
||||||
/>
|
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
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;
|
Loading…
Reference in New Issue
Block a user