feat: use enhanced guard-state

This commit is contained in:
GyDi 2022-02-25 01:22:33 +08:00
parent 8606af3616
commit 9ccc66ca1e
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
2 changed files with 21 additions and 30 deletions

View File

@ -1,7 +1,5 @@
import { useEffect, useState } from "react";
import { useDebounceFn } from "ahooks";
import { useSetRecoilState } from "recoil";
import useSWR, { useSWRConfig } from "swr"; import useSWR, { useSWRConfig } from "swr";
import { useSetRecoilState } from "recoil";
import { import {
ListItemText, ListItemText,
TextField, TextField,
@ -31,13 +29,10 @@ const SettingClash = ({ onError }: Props) => {
ipv6 = false, ipv6 = false,
"allow-lan": allowLan = false, "allow-lan": allowLan = false,
"log-level": logLevel = "silent", "log-level": logLevel = "silent",
"mixed-port": thePort = 0, "mixed-port": mixedPort = 0,
} = clashConfig ?? {}; } = clashConfig ?? {};
const setPort = useSetRecoilState(atomClashPort); const setGlobalClashPort = useSetRecoilState(atomClashPort);
const [mixedPort, setMixedPort] = useState(thePort);
useEffect(() => setMixedPort(thePort), [thePort]);
const onSwitchFormat = (_e: any, value: boolean) => value; const onSwitchFormat = (_e: any, value: boolean) => value;
const onChangeData = (patch: Partial<ApiType.ConfigData>) => { const onChangeData = (patch: Partial<ApiType.ConfigData>) => {
@ -48,10 +43,7 @@ const SettingClash = ({ onError }: Props) => {
await patchClashConfig(patch); await patchClashConfig(patch);
}; };
// restart clash when port is changed const onUpdatePort = async (port: number) => {
const { run: onUpdatePort } = useDebounceFn(
async (port: number) => {
try {
if (port < 1000) { if (port < 1000) {
throw new Error("The port should not < 1000"); throw new Error("The port should not < 1000");
} }
@ -59,16 +51,12 @@ const SettingClash = ({ onError }: Props) => {
throw new Error("The port should not > 65536"); throw new Error("The port should not > 65536");
} }
await patchClashConfig({ "mixed-port": port }); await patchClashConfig({ "mixed-port": port });
onChangeData({ "mixed-port": port }); setGlobalClashPort(port);
setPort(port);
Notice.success("Change Clash port successfully!"); Notice.success("Change Clash port successfully!");
} catch (err: any) {
setMixedPort(thePort); // back to old port value // update the config
Notice.error(err.message ?? err.toString()); mutate("getClashConfig");
} };
},
{ wait: 1000 }
);
// get clash core version // get clash core version
const clashVer = versionData?.premium const clashVer = versionData?.premium
@ -128,9 +116,11 @@ const SettingClash = ({ onError }: Props) => {
<ListItemText primary="Mixed Port" /> <ListItemText primary="Mixed Port" />
<GuardState <GuardState
value={mixedPort!} value={mixedPort!}
onCatch={onError}
onFormat={(e: any) => +e.target.value?.replace(/\D+/, "")} onFormat={(e: any) => +e.target.value?.replace(/\D+/, "")}
onChange={setMixedPort} onChange={(e) => onChangeData({ "mixed-port": e })}
onGuard={onUpdatePort} onGuard={onUpdatePort}
waitTime={800}
> >
<TextField autoComplete="off" size="small" sx={{ width: 120 }} /> <TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState> </GuardState>

View File

@ -91,6 +91,7 @@ const SettingSystem = ({ onError }: Props) => {
onFormat={(e: any) => e.target.value} onFormat={(e: any) => e.target.value}
onChange={(e) => onChangeData({ system_proxy_bypass: e })} onChange={(e) => onChangeData({ system_proxy_bypass: e })}
onGuard={(e) => patchVergeConfig({ system_proxy_bypass: e })} onGuard={(e) => patchVergeConfig({ system_proxy_bypass: e })}
waitTime={1000}
> >
<TextField autoComplete="off" size="small" sx={{ width: 120 }} /> <TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState> </GuardState>