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

186 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-11-20 21:48:39 +08:00
import { useRef } from "react";
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";
import { ArrowForward, Settings } from "@mui/icons-material";
2022-11-20 21:48:39 +08:00
import { DialogRef } from "@/components/base";
2022-11-23 17:44:40 +08:00
import { useClash } from "@/hooks/use-clash";
2022-11-20 21:48:39 +08:00
import { GuardState } from "./mods/guard-state";
import { WebUIViewer } from "./mods/web-ui-viewer";
import { ClashFieldViewer } from "./mods/clash-field-viewer";
import { ClashPortViewer } from "./mods/clash-port-viewer";
import { ControllerViewer } from "./mods/controller-viewer";
import { SettingList, SettingItem } from "./mods/setting-comp";
import { ClashCoreViewer } from "./mods/clash-core-viewer";
2023-11-22 00:15:41 -08:00
import { invoke_uwp_tool } from "@/services/cmds";
import getSystem from "@/utils/get-system";
const isWIN = getSystem() === "windows";
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
2022-11-23 17:44:40 +08:00
const { clash, version, mutateClash, patchClash } = useClash();
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,
2022-11-23 17:44:40 +08:00
} = clash ?? {};
2021-12-20 00:01:32 +08:00
2022-11-20 21:48:39 +08:00
const webRef = useRef<DialogRef>(null);
const fieldRef = useRef<DialogRef>(null);
const portRef = useRef<DialogRef>(null);
const ctrlRef = useRef<DialogRef>(null);
const coreRef = useRef<DialogRef>(null);
2022-08-06 21:56:54 +08:00
2021-12-20 00:01:32 +08:00
const onSwitchFormat = (_e: any, value: boolean) => value;
2022-11-19 17:22:29 +08:00
const onChangeData = (patch: Partial<IConfigData>) => {
2022-08-08 22:14:03 +08:00
mutateClash((old) => ({ ...(old! || {}), ...patch }), false);
2021-12-20 00:01:32 +08:00
};
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-11-20 21:48:39 +08:00
<WebUIViewer ref={webRef} />
<ClashFieldViewer ref={fieldRef} />
<ClashPortViewer ref={portRef} />
<ControllerViewer ref={ctrlRef} />
<ClashCoreViewer ref={coreRef} />
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 })}
2022-11-23 17:44:40 +08:00
onGuard={(e) => patchClash({ "allow-lan": e })}
2021-12-20 00:01:32 +08:00
>
<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 })}
2022-11-23 17:44:40 +08:00
onGuard={(e) => patchClash({ ipv6: e })}
2021-12-20 00:01:32 +08:00
>
<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 })}
2022-11-23 17:44:40 +08:00
onGuard={(e) => patchClash({ "log-level": e })}
2021-12-20 00:01:32 +08:00
>
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-11-06 23:23:26 +08:00
sx={{ width: 100, input: { py: "7.5px", cursor: "pointer" } }}
2022-09-05 02:12:25 +08:00
onClick={(e) => {
2022-11-20 21:48:39 +08:00
portRef.current?.open();
2022-09-05 02:12:25 +08:00
(e.target as any).blur();
}}
/>
2021-12-20 00:01:32 +08:00
</SettingItem>
2022-02-10 01:35:55 +08:00
<SettingItem label={t("External")}>
2022-11-06 23:23:26 +08:00
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
2022-11-20 21:48:39 +08:00
onClick={() => ctrlRef.current?.open()}
2022-11-06 23:23:26 +08:00
>
<ArrowForward />
</IconButton>
</SettingItem>
2022-08-08 01:51:30 +08:00
<SettingItem label={t("Web UI")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
2022-11-20 21:48:39 +08:00
onClick={() => webRef.current?.open()}
2022-08-08 01:51:30 +08:00
>
<ArrowForward />
</IconButton>
</SettingItem>
<SettingItem label={t("Clash Field")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
2022-11-20 21:48:39 +08:00
onClick={() => fieldRef.current?.open()}
2022-08-08 01:51:30 +08:00
>
<ArrowForward />
</IconButton>
</SettingItem>
<SettingItem
label={t("Clash Core")}
extra={
<IconButton
color="inherit"
size="small"
onClick={() => coreRef.current?.open()}
>
<Settings
fontSize="inherit"
style={{ cursor: "pointer", opacity: 0.75 }}
/>
</IconButton>
}
>
2022-11-23 17:44:40 +08:00
<Typography sx={{ py: "7px", pr: 1 }}>{version}</Typography>
2022-02-10 01:35:55 +08:00
</SettingItem>
2023-11-22 00:15:41 -08:00
{isWIN && (
<SettingItem label={t("Open UWP tool")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={invoke_uwp_tool}
>
<ArrowForward />
</IconButton>
</SettingItem>
)}
2022-01-16 03:22:37 +08:00
</SettingList>
2021-12-20 00:01:32 +08:00
);
};
export default SettingClash;