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

156 lines
4.7 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 {
TextField,
Switch,
Select,
MenuItem,
2022-02-10 01:35:55 +08:00
Typography,
2022-08-06 03:48:03 +08:00
IconButton,
2021-12-20 00:01:32 +08:00
} from "@mui/material";
2022-08-06 02:35:11 +08:00
import { atomClashPort } from "@/services/states";
2022-08-06 03:48:03 +08:00
import { ArrowForward } from "@mui/icons-material";
2022-08-06 21:56:54 +08:00
import { patchClashConfig } from "@/services/cmds";
2022-02-10 01:35:55 +08:00
import { SettingList, SettingItem } from "./setting";
2022-08-06 02:35:11 +08:00
import { getClashConfig, getVersion, updateConfigs } from "@/services/api";
2022-08-06 21:56:54 +08:00
import useModalHandler from "@/hooks/use-modal-handler";
2022-02-13 19:27:06 +08:00
import Notice from "../base/base-notice";
2022-08-06 02:35:11 +08:00
import GuardState from "./mods/guard-state";
import CoreSwitch from "./mods/core-switch";
2022-08-06 21:56:54 +08:00
import WebUIViewer from "./mods/web-ui-viewer";
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
2022-08-06 21:56:54 +08:00
const webUIHandler = useModalHandler();
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);
2022-05-17 01:59:49 +08:00
Notice.success("Change Clash port successfully!", 1000);
2022-02-25 01:22:33 +08:00
// 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")}>
2022-08-06 21:56:54 +08:00
<WebUIViewer handler={webUIHandler} onError={onError} />
2022-08-06 03:48:03 +08:00
<SettingItem label={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>
2022-08-06 03:48:03 +08:00
<SettingItem label={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>
2022-08-06 21:56:54 +08:00
<SettingItem label={t("Web UI")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={() => webUIHandler.current.open()}
>
<ArrowForward />
</IconButton>
</SettingItem>
2022-08-06 03:48:03 +08:00
<SettingItem label={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 })}
>
2022-08-06 03:48:03 +08:00
<Select size="small" sx={{ width: 120, "> div": { py: "7.5px" } }}>
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>
2022-08-06 03:48:03 +08:00
<SettingItem label={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-04-25 19:39:21 +08:00
waitTime={1000}
2022-01-21 02:32:23 +08:00
>
2022-08-06 03:48:03 +08:00
<TextField
autoComplete="off"
size="small"
sx={{ width: 120, input: { py: "7.5px" } }}
/>
2022-01-21 02:32:23 +08:00
</GuardState>
2021-12-20 00:01:32 +08:00
</SettingItem>
2022-02-10 01:35:55 +08:00
2022-08-06 03:48:03 +08:00
<SettingItem label={t("Clash Core")} extra={<CoreSwitch />}>
<Typography sx={{ py: "7px" }}>{clashVer}</Typography>
2022-02-10 01:35:55 +08:00
</SettingItem>
2022-01-16 03:22:37 +08:00
</SettingList>
2021-12-20 00:01:32 +08:00
);
};
export default SettingClash;