Skip to content

Commit

Permalink
feat: use enhanced guard-state
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Feb 24, 2022
1 parent 8606af3 commit 9ccc66c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
50 changes: 20 additions & 30 deletions src/components/setting/setting-clash.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useEffect, useState } from "react";
import { useDebounceFn } from "ahooks";
import { useSetRecoilState } from "recoil";
import useSWR, { useSWRConfig } from "swr";
import { useSetRecoilState } from "recoil";
import {
ListItemText,
TextField,
Expand Down Expand Up @@ -31,13 +29,10 @@ const SettingClash = ({ onError }: Props) => {
ipv6 = false,
"allow-lan": allowLan = false,
"log-level": logLevel = "silent",
"mixed-port": thePort = 0,
"mixed-port": mixedPort = 0,
} = clashConfig ?? {};

const setPort = useSetRecoilState(atomClashPort);
const [mixedPort, setMixedPort] = useState(thePort);

useEffect(() => setMixedPort(thePort), [thePort]);
const setGlobalClashPort = useSetRecoilState(atomClashPort);

const onSwitchFormat = (_e: any, value: boolean) => value;
const onChangeData = (patch: Partial<ApiType.ConfigData>) => {
Expand All @@ -48,27 +43,20 @@ const SettingClash = ({ onError }: Props) => {
await patchClashConfig(patch);
};

// restart clash when port is changed
const { run: onUpdatePort } = useDebounceFn(
async (port: number) => {
try {
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!");
} catch (err: any) {
setMixedPort(thePort); // back to old port value
Notice.error(err.message ?? err.toString());
}
},
{ wait: 1000 }
);
const onUpdatePort = async (port: number) => {
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 });
setGlobalClashPort(port);
Notice.success("Change Clash port successfully!");

// update the config
mutate("getClashConfig");
};

// get clash core version
const clashVer = versionData?.premium
Expand Down Expand Up @@ -128,9 +116,11 @@ const SettingClash = ({ onError }: Props) => {
<ListItemText primary="Mixed Port" />
<GuardState
value={mixedPort!}
onCatch={onError}
onFormat={(e: any) => +e.target.value?.replace(/\D+/, "")}
onChange={setMixedPort}
onChange={(e) => onChangeData({ "mixed-port": e })}
onGuard={onUpdatePort}
waitTime={800}
>
<TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState>
Expand Down
1 change: 1 addition & 0 deletions src/components/setting/setting-system.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const SettingSystem = ({ onError }: Props) => {
onFormat={(e: any) => e.target.value}
onChange={(e) => onChangeData({ system_proxy_bypass: e })}
onGuard={(e) => patchVergeConfig({ system_proxy_bypass: e })}
waitTime={1000}
>
<TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState>
Expand Down

0 comments on commit 9ccc66c

Please sign in to comment.