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

122 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-01-17 02:42:52 +08:00
import useSWR, { useSWRConfig } from "swr";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
2022-02-14 01:26:24 +08:00
import { Box, ListItemText, Switch, TextField } from "@mui/material";
2022-01-17 02:42:52 +08:00
import { getVergeConfig, patchVergeConfig } from "../../services/cmds";
import { SettingList, SettingItem } from "./setting";
import { CmdType } from "../../services/types";
import GuardState from "./guard-state";
import SysproxyTooltip from "./sysproxy-tooltip";
interface Props {
onError?: (err: Error) => void;
}
const SettingSystem = ({ onError }: Props) => {
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2022-01-17 02:42:52 +08:00
const { mutate } = useSWRConfig();
const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
const {
2022-03-12 23:07:45 +08:00
enable_tun_mode,
enable_auto_launch,
enable_system_proxy,
system_proxy_bypass,
enable_proxy_guard,
2022-01-17 02:42:52 +08:00
} = vergeConfig ?? {};
const onSwitchFormat = (_e: any, value: boolean) => value;
const onChangeData = (patch: Partial<CmdType.VergeConfig>) => {
mutate("getVergeConfig", { ...vergeConfig, ...patch }, false);
};
return (
2022-03-12 23:07:45 +08:00
<SettingList title={t("System Setting")}>
2022-02-25 02:09:39 +08:00
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Tun Mode")} />
2022-02-25 02:09:39 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={enable_tun_mode ?? false}
2022-02-25 02:09:39 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_tun_mode: e })}
onGuard={(e) => patchVergeConfig({ enable_tun_mode: e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
2022-01-17 02:42:52 +08:00
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Auto Launch")} />
2022-01-17 02:42:52 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={enable_auto_launch ?? false}
2022-01-17 02:42:52 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_auto_launch: e })}
onGuard={(e) => patchVergeConfig({ enable_auto_launch: e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
<ListItemText
primary={
<Box sx={{ display: "flex", alignItems: "center" }}>
2022-03-12 23:07:45 +08:00
{t("System Proxy")}
2022-01-17 02:42:52 +08:00
<SysproxyTooltip />
</Box>
}
/>
<GuardState
2022-03-12 23:07:45 +08:00
value={enable_system_proxy ?? false}
2022-01-17 02:42:52 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_system_proxy: e })}
2022-02-14 01:26:24 +08:00
onGuard={async (e) => {
await patchVergeConfig({ enable_system_proxy: e });
mutate("getVergeConfig"); // update bypass value
}}
2022-01-17 02:42:52 +08:00
>
<Switch edge="end" />
</GuardState>
</SettingItem>
2022-02-14 01:26:24 +08:00
2022-02-20 18:53:21 +08:00
{enable_system_proxy && (
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Proxy Guard")} />
2022-02-20 18:53:21 +08:00
<GuardState
2022-03-12 23:07:45 +08:00
value={enable_proxy_guard ?? false}
2022-02-20 18:53:21 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_proxy_guard: e })}
onGuard={(e) => patchVergeConfig({ enable_proxy_guard: e })}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
)}
{enable_system_proxy && (
2022-02-14 01:26:24 +08:00
<SettingItem>
2022-03-12 23:07:45 +08:00
<ListItemText primary={t("Proxy Bypass")} />
2022-02-14 01:26:24 +08:00
<GuardState
2022-02-20 18:53:21 +08:00
value={system_proxy_bypass ?? ""}
2022-02-14 01:26:24 +08:00
onCatch={onError}
onFormat={(e: any) => e.target.value}
onChange={(e) => onChangeData({ system_proxy_bypass: e })}
onGuard={(e) => patchVergeConfig({ system_proxy_bypass: e })}
2022-02-25 01:22:33 +08:00
waitTime={1000}
2022-02-14 01:26:24 +08:00
>
<TextField autoComplete="off" size="small" sx={{ width: 120 }} />
</GuardState>
</SettingItem>
)}
2022-01-17 02:42:52 +08:00
</SettingList>
);
};
export default SettingSystem;