Files
clash-proxy/src/components/setting/setting-clash.tsx

154 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-08-08 22:14:03 +08:00
import useSWR from "swr";
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 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-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";
2022-08-08 01:51:30 +08:00
import ClashFieldViewer from "./mods/clash-field-viewer";
2022-09-05 02:12:25 +08:00
import ClashPortViewer from "./mods/clash-port-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();
2022-08-08 22:14:03 +08:00
const { data: clashConfig, mutate: mutateClash } = 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-08-06 21:56:54 +08:00
const webUIHandler = useModalHandler();
2022-08-08 01:51:30 +08:00
const fieldHandler = useModalHandler();
2022-09-05 02:12:25 +08:00
const portHandler = useModalHandler();
2022-08-06 21:56:54 +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>) => {
2022-08-08 22:14:03 +08:00
mutateClash((old) => ({ ...(old! || {}), ...patch }), false);
2021-12-20 00:01:32 +08:00
};
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-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-08 22:14:03 +08:00
<ClashFieldViewer handler={fieldHandler} />
2022-09-05 02:12:25 +08:00
<ClashPortViewer handler={portHandler} />
2022-08-06 21:56:54 +08:00
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 03:48:03 +08:00
<SettingItem label={t("Log Level")}>
2021-12-20 00:01:32 +08:00
<GuardState
2022-09-06 14:42:58 +08:00
// clash premium 2022.08.26 值为warn
value={logLevel === "warn" ? "warning" : 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-09-05 02:12:25 +08:00
<Select size="small" sx={{ width: 100, "> 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>
2022-09-06 14:42:58 +08:00
<MenuItem value="warning">Warn</MenuItem>
2021-12-20 00:01:32 +08:00
<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-09-05 02:12:25 +08:00
<TextField
autoComplete="off"
size="small"
2022-03-12 23:07:45 +08:00
value={mixedPort ?? 0}
2022-09-05 02:12:25 +08:00
sx={{ width: 100, input: { py: "7.5px" } }}
onClick={(e) => {
portHandler.current.open();
(e.target as any).blur();
}}
/>
2021-12-20 00:01:32 +08:00
</SettingItem>
2022-02-10 01:35:55 +08:00
2022-08-08 01:51:30 +08:00
<SettingItem label={t("Web UI")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={() => webUIHandler.current.open()}
>
<ArrowForward />
</IconButton>
</SettingItem>
<SettingItem label={t("Clash Field")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={() => fieldHandler.current.open()}
>
<ArrowForward />
</IconButton>
</SettingItem>
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;