2021-12-20 00:01:32 +08:00
|
|
|
import useSWR, { useSWRConfig } from "swr";
|
2021-12-28 01:35:58 +08:00
|
|
|
import {
|
|
|
|
List,
|
|
|
|
ListItemText,
|
|
|
|
ListSubheader,
|
|
|
|
Switch,
|
|
|
|
Typography,
|
|
|
|
} from "@mui/material";
|
2021-12-20 00:01:32 +08:00
|
|
|
import {
|
2022-01-10 02:05:35 +08:00
|
|
|
setSysProxy,
|
2021-12-20 00:01:32 +08:00
|
|
|
getVergeConfig,
|
|
|
|
patchVergeConfig,
|
2021-12-25 22:33:29 +08:00
|
|
|
} from "../services/cmds";
|
|
|
|
import { CmdType } from "../services/types";
|
2022-01-10 02:05:35 +08:00
|
|
|
import { version } from "../../package.json";
|
2021-12-20 00:01:32 +08: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 22:33:29 +08:00
|
|
|
const onChangeData = (patch: Partial<CmdType.VergeConfig>) => {
|
2021-12-20 00:01:32 +08:00
|
|
|
mutate("getVergeConfig", { ...vergeConfig, ...patch }, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-12-22 02:11:31 +08:00
|
|
|
<List>
|
2022-01-08 02:54:37 +08:00
|
|
|
<ListSubheader sx={{ background: "transparent" }}>
|
|
|
|
Common Setting
|
|
|
|
</ListSubheader>
|
2021-12-20 00:01:32 +08:00
|
|
|
|
|
|
|
<SettingItem>
|
2022-01-08 02:54:37 +08:00
|
|
|
<ListItemText primary="Theme Mode" />
|
2021-12-20 00:01:32 +08:00
|
|
|
<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>
|
2022-01-10 22:57:21 +08:00
|
|
|
<ListItemText primary="Self Startup" />
|
2021-12-20 00:01:32 +08:00
|
|
|
<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>
|
2022-01-08 02:54:37 +08:00
|
|
|
<ListItemText primary="System Proxy" />
|
2021-12-20 00:01:32 +08:00
|
|
|
<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>
|
2021-12-28 01:35:58 +08:00
|
|
|
|
|
|
|
<SettingItem>
|
|
|
|
<ListItemText primary="Version" />
|
|
|
|
<Typography sx={{ py: "6px" }}>v{version}</Typography>
|
|
|
|
</SettingItem>
|
2021-12-20 00:01:32 +08:00
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SettingVerge;
|