import { mutate } from "swr"; import { useEffect, useState } from "react"; import { useLockFn, useSetState } from "ahooks"; import { Button, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, TextField, } from "@mui/material"; import { Settings } from "@mui/icons-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 // remote / local file / merge / script const ProfileEdit = (props: Props) => { const { open, itemData, onClose } = props; const [form, setForm] = useSetState({ ...itemData }); const [option, setOption] = useSetState(itemData.option ?? {}); const [showOpt, setShowOpt] = useState(!!itemData.option); useEffect(() => { if (itemData) { setForm({ ...itemData }); setOption(itemData.option ?? {}); setShowOpt(!!itemData.option?.user_agent); } }, [itemData]); const onUpdate = useLockFn(async () => { try { const { uid } = itemData; const { name, desc, url } = form; const option_ = itemData.type === "remote" ? option : undefined; if (itemData.type === "remote" && !url) { throw new Error("Remote URL should not be null"); } await patchProfile(uid, { uid, name, desc, url, option: option_ }); 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; const type = form.type || (form.url ? "remote" : form.file?.endsWith("js") ? "script" : "local"); return ( Edit Profile setForm({ name: e.target.value })} /> setForm({ desc: e.target.value })} /> {type === "remote" && ( setForm({ url: e.target.value })} /> )} {showOpt && ( setOption({ user_agent: e.target.value })} /> )} {form.type === "remote" && ( setShowOpt((o) => !o)} > )} ); }; export default ProfileEdit;