Files
clash-verge-rev/src/components/profile/profile-edit.tsx
T

109 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-02 01:58:16 +08:00
import { mutate } from "swr";
2022-03-05 19:04:20 +08:00
import { useEffect } from "react";
import { useLockFn, useSetState } from "ahooks";
2022-03-02 01:58:16 +08:00
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TextField,
} from "@mui/material";
import { CmdType } from "../../services/types";
import { patchProfile } from "../../services/cmds";
import Notice from "../base/base-notice";
interface Props {
open: boolean;
itemData: CmdType.ProfileItem;
onClose: () => void;
}
// edit the profile item
const ProfileEdit = (props: Props) => {
const { open, itemData, onClose } = props;
2022-03-05 19:04:20 +08:00
const [form, setForm] = useSetState({ ...itemData });
2022-03-02 01:58:16 +08:00
useEffect(() => {
if (itemData) {
2022-03-05 19:04:20 +08:00
setForm({ ...itemData });
2022-03-02 01:58:16 +08:00
}
}, [itemData]);
const onUpdate = useLockFn(async () => {
try {
const { uid } = itemData;
2022-03-05 19:04:20 +08:00
const { name, desc, url } = form;
2022-03-07 01:30:32 +08:00
if (itemData.type === "remote" && !url) {
throw new Error("Remote URL should not be null");
}
2022-03-02 01:58:16 +08:00
await patchProfile(uid, { uid, name, desc, url });
mutate("getProfiles");
onClose();
} catch (err: any) {
2022-03-05 19:04:20 +08:00
Notice.error(err?.message || err.toString());
2022-03-02 01:58:16 +08:00
}
});
2022-03-05 19:04:20 +08:00
const textFieldProps = {
fullWidth: true,
size: "small",
margin: "normal",
variant: "outlined",
} as const;
const type =
2022-03-05 22:54:39 +08:00
form.type ||
(form.url ? "remote" : form.file?.endsWith("js") ? "script" : "local");
2022-03-05 19:04:20 +08:00
2022-03-02 01:58:16 +08:00
return (
<Dialog open={open} onClose={onClose}>
2022-03-05 19:04:20 +08:00
<DialogTitle sx={{ pb: 0.5 }}>Edit Profile</DialogTitle>
<DialogContent sx={{ width: 336, pb: 1 }}>
2022-03-02 01:58:16 +08:00
<TextField
2022-03-05 19:04:20 +08:00
{...textFieldProps}
disabled
label="Type"
value={type}
sx={{ input: { textTransform: "capitalize" } }}
/>
<TextField
{...textFieldProps}
2022-03-02 01:58:16 +08:00
autoFocus
label="Name"
2022-03-05 19:04:20 +08:00
value={form.name}
onChange={(e) => setForm({ name: e.target.value })}
2022-03-02 01:58:16 +08:00
/>
<TextField
2022-03-05 19:04:20 +08:00
{...textFieldProps}
2022-03-02 01:58:16 +08:00
label="Descriptions"
2022-03-05 19:04:20 +08:00
value={form.desc}
onChange={(e) => setForm({ desc: e.target.value })}
2022-03-02 01:58:16 +08:00
/>
2022-03-05 19:04:20 +08:00
{type === "remote" && (
<TextField
{...textFieldProps}
label="Subscription Url"
value={form.url}
onChange={(e) => setForm({ url: e.target.value })}
/>
)}
2022-03-02 01:58:16 +08:00
</DialogContent>
2022-03-05 19:04:20 +08:00
2022-03-02 01:58:16 +08:00
<DialogActions sx={{ px: 2, pb: 2 }}>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={onUpdate} variant="contained">
Update
</Button>
</DialogActions>
</Dialog>
);
};
export default ProfileEdit;