feat: support more options for remote profile

This commit is contained in:
GyDi
2022-03-10 02:03:55 +08:00
parent 04c754c0ac
commit fe1fea671c
7 changed files with 135 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
import { mutate } from "swr";
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useLockFn, useSetState } from "ahooks";
import {
Button,
@@ -7,8 +7,10 @@ import {
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";
@@ -20,13 +22,18 @@ interface Props {
}
// 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]);
@@ -34,12 +41,14 @@ const ProfileEdit = (props: Props) => {
try {
const { uid } = itemData;
const { name, desc, url } = form;
const option_ = showOpt ? option : undefined;
if (itemData.type === "remote" && !url) {
throw new Error("Remote URL should not be null");
}
await patchProfile(uid, { uid, name, desc, url });
await patchProfile(uid, { uid, name, desc, url, option: option_ });
setShowOpt(false);
mutate("getProfiles");
onClose();
} catch (err: any) {
@@ -94,9 +103,28 @@ const ProfileEdit = (props: Props) => {
onChange={(e) => setForm({ url: e.target.value })}
/>
)}
{showOpt && (
<TextField
{...textFieldProps}
label="User Agent"
value={option.user_agent}
onChange={(e) => setOption({ user_agent: e.target.value })}
/>
)}
</DialogContent>
<DialogActions sx={{ px: 2, pb: 2 }}>
<DialogActions sx={{ px: 2, pb: 2, position: "relative" }}>
{form.type === "remote" && (
<IconButton
size="small"
sx={{ position: "absolute", left: 18 }}
onClick={() => setShowOpt((o) => !o)}
>
<Settings />
</IconButton>
)}
<Button onClick={onClose}>Cancel</Button>
<Button onClick={onUpdate} variant="contained">
Update

View File

@@ -98,7 +98,7 @@ const ProfileItem = (props: Props) => {
if (loading) return;
setLoading(true);
try {
await updateProfile(itemData.uid, withProxy);
await updateProfile(itemData.uid, { with_proxy: withProxy });
setLoading(false);
mutate("getProfiles");
} catch (err: any) {

View File

@@ -1,3 +1,4 @@
import { useState } from "react";
import { useSWRConfig } from "swr";
import { useLockFn, useSetState } from "ahooks";
import {
@@ -7,11 +8,13 @@ import {
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";
@@ -27,12 +30,17 @@ const ProfileNew = (props: Props) => {
const { mutate } = useSWRConfig();
const [form, setForm] = useSetState({
type: "remote",
name: "",
desc: "",
type: "remote",
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");
@@ -43,11 +51,15 @@ const ProfileNew = (props: Props) => {
const name = form.name || `${form.type} file`;
if (form.type === "remote" && !form.url) {
throw new Error("Remote URL should not be null");
throw new Error("The URL should not be null");
}
await createProfile({ ...form, name });
setForm({ name: "", desc: "", type: "remote", url: "" });
const option_ = showOpt ? 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) {
@@ -67,17 +79,10 @@ const ProfileNew = (props: Props) => {
<DialogTitle sx={{ pb: 0.5 }}>Create Profile</DialogTitle>
<DialogContent sx={{ width: 336, pb: 1 }}>
<TextField
{...textFieldProps}
autoFocus
label="Name"
value={form.name}
onChange={(e) => setForm({ name: e.target.value })}
/>
<FormControl size="small" fullWidth sx={{ mt: 2, mb: 1 }}>
<InputLabel>Type</InputLabel>
<Select
autoFocus
label="Type"
value={form.type}
onChange={(e) => setForm({ type: e.target.value })}
@@ -89,6 +94,13 @@ const ProfileNew = (props: Props) => {
</Select>
</FormControl>
<TextField
{...textFieldProps}
label="Name"
value={form.name}
onChange={(e) => setForm({ name: e.target.value })}
/>
<TextField
{...textFieldProps}
label="Descriptions"
@@ -104,9 +116,28 @@ const ProfileNew = (props: Props) => {
onChange={(e) => setForm({ url: e.target.value })}
/>
)}
{showOpt && (
<TextField
{...textFieldProps}
label="User Agent"
value={option.user_agent}
onChange={(e) => setOption({ user_agent: e.target.value })}
/>
)}
</DialogContent>
<DialogActions sx={{ px: 2, pb: 2 }}>
<DialogActions sx={{ px: 2, pb: 2, position: "relative" }}>
{form.type === "remote" && (
<IconButton
size="small"
sx={{ position: "absolute", left: 18 }}
onClick={() => setShowOpt((o) => !o)}
>
<Settings />
</IconButton>
)}
<Button onClick={onClose}>Cancel</Button>
<Button onClick={onCreate} variant="contained">
Create