import { useState } from "react"; import { useSWRConfig } from "swr"; import { useLockFn, useSetState } from "ahooks"; import { Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControl, IconButton, InputLabel, MenuItem, Select, TextField, } from "@mui/material"; import { Settings } from "@mui/icons-material"; import { createProfile } from "../../services/cmds"; import Notice from "../base/base-notice"; interface Props { open: boolean; onClose: () => void; } // create a new profile // remote / local file / merge / script const ProfileNew = (props: Props) => { const { open, onClose } = props; const { mutate } = useSWRConfig(); const [form, setForm] = useSetState({ type: "remote", name: "", desc: "", url: "", }); const [showOpt, setShowOpt] = useState(false); const [option, setOption] = useSetState({ user_agent: "", }); // able to add more option const onCreate = useLockFn(async () => { if (!form.type) { Notice.error("`Type` should not be null"); return; } try { const name = form.name || `${form.type} file`; if (form.type === "remote" && !form.url) { throw new Error("The URL should not be null"); } const option_ = form.type === "remote" ? option : undefined; await createProfile({ ...form, name, option: option_ }); setForm({ type: "remote", name: "", desc: "", url: "" }); setOption({ user_agent: "" }); setShowOpt(false); mutate("getProfiles"); onClose(); } catch (err: any) { Notice.error(err.message || err.toString()); } }); const textFieldProps = { fullWidth: true, size: "small", margin: "normal", variant: "outlined", } as const; return ( Create Profile Type setForm({ name: e.target.value })} /> setForm({ desc: e.target.value })} /> {form.type === "remote" && ( setForm({ url: e.target.value })} /> )} {showOpt && ( setOption({ user_agent: e.target.value })} /> )} {form.type === "remote" && ( setShowOpt((o) => !o)} > )} ); }; export default ProfileNew;