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

139 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-12-20 00:01:32 +08:00
import useSWR, { useSWRConfig } from "swr";
2022-02-25 01:22:33 +08:00
import { useSetRecoilState } from "recoil";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
2021-12-20 00:01:32 +08:00
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-23 02:00:45 +08:00
import { atomClashPort } from "../../services/states";
2022-02-10 01:35:55 +08:00
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) => {
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2021-12-20 00:01:32 +08:00
const { mutate } = useSWRConfig();
const { data: clashConfig } = useSWR("getClashConfig", getClashConfig);
2022-02-24 23:04:18 +08:00
const { data: versionData } = useSWR("getVersion", getVersion);
2021-12-20 00:01:32 +08:00
const {
2022-03-12 23:07:45 +08:00
ipv6,
"allow-lan": allowLan,
"log-level": logLevel,
"mixed-port": mixedPort,
2021-12-20 00:01:32 +08:00
} = clashConfig ?? {};
2022-02-25 01:22:33 +08:00
const setGlobalClashPort = useSetRecoilState(atomClashPort);
2022-01-21 02:32:23 +08:00
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-02-25 01:22:33 +08:00
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");
};
2022-01-21 02:32:23 +08:00
2022-02-10 01:35:55 +08:00
// get clash core version
2022-02-24 23:04:18 +08:00
const clashVer = versionData?.premium
? `${versionData.version} Premium`
: versionData?.version || "-";
2022-02-10 01:35:55 +08:00
2021-12-20 00:01:32 +08:00
return (
2022-03-12 23:07:45 +08:00
<SettingList title={t("Clash Setting")}>
2021-12-20 00:01:32 +08:00
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Allow Lan")} />
2021-12-20 00:01:32 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={allowLan ?? false}
2021-12-20 00:01:32 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ "allow-lan": e })}
onGuard={(e) => onUpdateData({ "allow-lan": e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("IPv6")} />
2021-12-20 00:01:32 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={ipv6 ?? false}
2021-12-20 00:01:32 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ ipv6: e })}
onGuard={(e) => onUpdateData({ ipv6: e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Log Level")} />
2021-12-20 00:01:32 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={logLevel ?? "info"}
2021-12-20 00:01:32 +08:00
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-03-12 23:07:45 +08:00
<ListItemText primary={t("Mixed Port")} />
2022-01-21 02:32:23 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={mixedPort ?? 0}
2022-02-25 01:22:33 +08:00
onCatch={onError}
2022-01-21 02:32:23 +08:00
onFormat={(e: any) => +e.target.value?.replace(/\D+/, "")}
2022-02-25 01:22:33 +08:00
onChange={(e) => onChangeData({ "mixed-port": e })}
2022-01-21 02:32:23 +08:00
onGuard={onUpdatePort}
2022-02-25 01:22:33 +08:00
waitTime={800}
2022-01-21 02:32:23 +08:00
>
<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>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Clash core")} />
2022-02-10 01:35:55 +08:00
<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;