Files
clash-verge-rev/src/components/setting/setting-clash.tsx
T

149 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-01-21 02:32:23 +08:00
import { useEffect, useState } from "react";
import { useDebounceFn } from "ahooks";
import { useSetRecoilState } from "recoil";
2021-12-20 00:01:32 +08:00
import useSWR, { useSWRConfig } from "swr";
import {
ListItemText,
TextField,
Switch,
Select,
MenuItem,
2022-02-10 01:35:55 +08:00
Typography,
2021-12-20 00:01:32 +08:00
} from "@mui/material";
2022-01-16 03:22:37 +08:00
import { ApiType } from "../../services/types";
2022-02-10 01:35:55 +08:00
import { atomClashPort } from "../../states/setting";
import { patchClashConfig } from "../../services/cmds";
import { SettingList, SettingItem } from "./setting";
import { getClashConfig, getVersion, updateConfigs } from "../../services/api";
2022-02-13 19:27:06 +08:00
import Notice from "../base/base-notice";
2022-01-16 18:30:25 +08:00
import GuardState from "./guard-state";
2021-12-20 00:01:32 +08:00
interface Props {
2022-01-21 02:32:23 +08:00
onError: (err: Error) => void;
2021-12-20 00:01:32 +08:00
}
const SettingClash = ({ onError }: Props) => {
const { mutate } = useSWRConfig();
const { data: clashConfig } = useSWR("getClashConfig", getClashConfig);
const {
ipv6 = false,
"allow-lan": allowLan = false,
"log-level": logLevel = "silent",
2022-01-21 02:32:23 +08:00
"mixed-port": thePort = 0,
2021-12-20 00:01:32 +08:00
} = clashConfig ?? {};
2022-01-21 02:32:23 +08:00
const setPort = useSetRecoilState(atomClashPort);
const [mixedPort, setMixedPort] = useState(thePort);
useEffect(() => setMixedPort(thePort), [thePort]);
2021-12-20 00:01:32 +08:00
const onSwitchFormat = (_e: any, value: boolean) => value;
2021-12-25 22:33:29 +08:00
const onChangeData = (patch: Partial<ApiType.ConfigData>) => {
2021-12-20 00:01:32 +08:00
mutate("getClashConfig", { ...clashConfig, ...patch }, false);
};
2021-12-25 22:33:29 +08:00
const onUpdateData = async (patch: Partial<ApiType.ConfigData>) => {
2021-12-20 00:01:32 +08:00
await updateConfigs(patch);
await patchClashConfig(patch);
};
2022-01-21 02:32:23 +08:00
// restart clash when port is changed
const { run: onUpdatePort } = useDebounceFn(
async (port: number) => {
2022-02-12 21:12:39 +08:00
try {
2022-01-21 02:32:23 +08:00
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
await patchClashConfig({ "mixed-port": port });
onChangeData({ "mixed-port": port });
setPort(port);
Notice.success("Change Clash port successfully!");
2022-02-12 21:12:39 +08:00
} catch (err: any) {
2022-01-21 02:32:23 +08:00
setMixedPort(thePort); // back to old port value
Notice.error(err.message ?? err.toString());
2022-02-12 21:12:39 +08:00
}
2022-01-21 02:32:23 +08:00
},
{ wait: 1000 }
);
2022-02-10 01:35:55 +08:00
// get clash core version
const [clashVer, setClashVer] = useState("");
useEffect(() => {
2022-02-12 21:12:39 +08:00
getVersion().then(({ premium, version }) => {
2022-02-10 01:35:55 +08:00
setClashVer(premium ? `${version} Premium` : version);
});
}, []);
2021-12-20 00:01:32 +08:00
return (
2022-01-16 03:22:37 +08:00
<SettingList title="Clash Setting">
2021-12-20 00:01:32 +08:00
<SettingItem>
2022-01-08 02:54:37 +08:00
<ListItemText primary="Allow Lan" />
2021-12-20 00:01:32 +08:00
<GuardState
value={allowLan}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ "allow-lan": e })}
onGuard={(e) => onUpdateData({ "allow-lan": e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
<ListItemText primary="IPv6" />
<GuardState
value={ipv6}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ ipv6: e })}
onGuard={(e) => onUpdateData({ ipv6: e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
2022-01-08 02:54:37 +08:00
<ListItemText primary="Log Level" />
2021-12-20 00:01:32 +08:00
<GuardState
value={logLevel}
onCatch={onError}
onFormat={(e: any) => e.target.value}
onChange={(e) => onChangeData({ "log-level": e })}
onGuard={(e) => onUpdateData({ "log-level": e })}
>
<Select size="small" sx={{ width: 120 }}>
2021-12-20 00:39:07 +08:00
<MenuItem value="debug">Debug</MenuItem>
2021-12-20 00:01:32 +08:00
<MenuItem value="info">Info</MenuItem>
<MenuItem value="warning">Warning</MenuItem>
<MenuItem value="error">Error</MenuItem>
<MenuItem value="silent">Silent</MenuItem>
</Select>
</GuardState>
</SettingItem>
<SettingItem>
2022-01-08 02:54:37 +08:00
<ListItemText primary="Mixed Port" />
2022-01-21 02:32:23 +08:00
<GuardState
2021-12-20 00:36:50 +08:00
value={mixedPort!}
2022-01-21 02:32:23 +08:00
onFormat={(e: any) => +e.target.value?.replace(/\D+/, "")}
onChange={setMixedPort}
onGuard={onUpdatePort}
>
<TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState>
2021-12-20 00:01:32 +08:00
</SettingItem>
2022-02-10 01:35:55 +08:00
<SettingItem>
<ListItemText primary="Clash core" />
<Typography sx={{ py: 1 }}>{clashVer}</Typography>
</SettingItem>
2022-01-16 03:22:37 +08:00
</SettingList>
2021-12-20 00:01:32 +08:00
);
};
export default SettingClash;