import { mutate } from "swr"; import { useEffect, useState } from "react"; import { useLockFn, useSetState } from "ahooks"; import { useTranslation } from "react-i18next"; import { Button, Collapse, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, IconButton, Switch, TextField, } from "@mui/material"; import { Settings } from "@mui/icons-material"; import { patchProfile } from "@/services/cmds"; import { version } from "@root/package.json"; import { Notice } from "@/components/base"; interface Props { open: boolean; itemData: IProfileItem; onClose: () => void; } // edit the profile item // remote / local file / merge / script const InfoEditor = (props: Props) => { const { open, itemData, onClose } = props; const { t } = useTranslation(); const [form, setForm] = useSetState({ ...itemData }); const [option, setOption] = useSetState(itemData.option ?? {}); const [showOpt, setShowOpt] = useState(!!itemData.option); useEffect(() => { if (itemData) { const { option } = itemData; setForm({ ...itemData }); setOption(option ?? {}); setShowOpt( itemData.type === "remote" && (!!option?.user_agent || !!option?.update_interval || !!option?.self_proxy || !!option?.with_proxy) ); } }, [itemData]); const onUpdate = useLockFn(async () => { try { const { uid } = itemData; const { name, desc, url } = form; const option_ = itemData.type === "remote" || itemData.type === "local" ? 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_ }); 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 ( {t("Edit Info")} setForm({ name: e.target.value })} onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> setForm({ desc: e.target.value })} onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> {type === "remote" && ( setForm({ url: e.target.value })} onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> )} {(type === "remote" || type === "local") && ( { const str = e.target.value?.replace(/\D/, ""); setOption({ update_interval: !!str ? +str : undefined }); }} onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> )} setOption({ user_agent: e.target.value })} onKeyDown={(e) => e.key === "Enter" && onUpdate()} /> setOption((o) => ({ self_proxy: c ? false : o.self_proxy ?? false, with_proxy: c, })) } /> } /> setOption((o) => ({ with_proxy: c ? false : o.with_proxy ?? false, self_proxy: c, })) } /> } /> {form.type === "remote" && ( setShowOpt((o) => !o)} > )} ); }; export default InfoEditor;