clash-verge/src/components/setting-verge.tsx

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-12-19 19:01:32 +03:00
import useSWR, { useSWRConfig } from "swr";
import { List, ListItemText, ListSubheader, Switch } from "@mui/material";
import {
getVergeConfig,
patchVergeConfig,
setSysProxy,
2021-12-25 17:33:29 +03:00
} from "../services/cmds";
import { CmdType } from "../services/types";
2021-12-19 19:01:32 +03:00
import GuardState from "./guard-state";
import SettingItem from "./setting-item";
import PaletteSwitch from "./palette-switch";
interface Props {
onError?: (err: Error) => void;
}
const SettingVerge = ({ onError }: Props) => {
const { mutate } = useSWRConfig();
const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
const {
theme_mode: mode = "light",
enable_self_startup: startup = false,
enable_system_proxy: proxy = false,
} = vergeConfig ?? {};
const onSwitchFormat = (_e: any, value: boolean) => value;
2021-12-25 17:33:29 +03:00
const onChangeData = (patch: Partial<CmdType.VergeConfig>) => {
2021-12-19 19:01:32 +03:00
mutate("getVergeConfig", { ...vergeConfig, ...patch }, false);
};
return (
2021-12-21 21:11:31 +03:00
<List>
2021-12-19 19:01:32 +03:00
<ListSubheader></ListSubheader>
<SettingItem>
<ListItemText primary="外观主题" />
<GuardState
value={mode === "dark"}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ theme_mode: e ? "dark" : "light" })}
onGuard={async (c) => {
await patchVergeConfig({ theme_mode: c ? "dark" : "light" });
}}
>
<PaletteSwitch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
<ListItemText primary="开机自启" />
<GuardState
value={startup}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_self_startup: e })}
onGuard={async (e) => {
await patchVergeConfig({ enable_self_startup: e });
}}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
<SettingItem>
<ListItemText primary="设置系统代理" />
<GuardState
value={proxy}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_system_proxy: e })}
onGuard={async (e) => {
await setSysProxy(e);
await patchVergeConfig({ enable_system_proxy: e });
}}
>
<Switch edge="end" />
</GuardState>
</SettingItem>
</List>
);
};
export default SettingVerge;