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

199 lines
5.8 KiB
TypeScript
Raw Normal View History

2022-09-07 01:51:43 +08:00
import useSWR from "swr";
import { useRef } from "react";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
import { IconButton, Tooltip } from "@mui/material";
2024-02-19 18:10:10 +08:00
import { PrivacyTipRounded, Settings, InfoRounded } from "@mui/icons-material";
2022-11-20 20:12:58 +08:00
import { checkService } from "@/services/cmds";
import { useVerge } from "@/hooks/use-verge";
import { DialogRef, Switch } from "@/components/base";
import { SettingList, SettingItem } from "./mods/setting-comp";
import { GuardState } from "./mods/guard-state";
import { ServiceViewer } from "./mods/service-viewer";
import { SysproxyViewer } from "./mods/sysproxy-viewer";
2024-02-20 23:27:03 +08:00
import { TunViewer } from "./mods/tun-viewer";
2022-08-06 02:35:11 +08:00
import getSystem from "@/utils/get-system";
2022-01-17 02:42:52 +08:00
interface Props {
onError?: (err: Error) => void;
}
2024-03-31 16:16:23 +08:00
const isServiceModeAvailable =
getSystem() === "windows" || getSystem() === "linux";
2022-04-25 16:12:04 +08:00
2022-01-17 02:42:52 +08:00
const SettingSystem = ({ onError }: Props) => {
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2022-09-07 01:51:43 +08:00
2022-11-20 20:12:58 +08:00
const { verge, mutateVerge, patchVerge } = useVerge();
2022-01-17 02:42:52 +08:00
2022-04-25 16:12:04 +08:00
// service mode
const { data: serviceStatus } = useSWR(
2024-03-31 16:16:23 +08:00
isServiceModeAvailable ? "checkService" : null,
2022-04-25 16:12:04 +08:00
checkService,
2022-11-23 17:45:22 +08:00
{
revalidateIfStale: false,
shouldRetryOnError: false,
focusThrottleInterval: 36e5, // 1 hour
}
2022-04-25 16:12:04 +08:00
);
const serviceRef = useRef<DialogRef>(null);
const sysproxyRef = useRef<DialogRef>(null);
2024-02-20 23:27:03 +08:00
const tunRef = useRef<DialogRef>(null);
2022-01-17 02:42:52 +08:00
const {
2022-03-12 23:07:45 +08:00
enable_tun_mode,
enable_auto_launch,
2022-04-25 16:12:04 +08:00
enable_service_mode,
2022-03-26 18:56:16 +08:00
enable_silent_start,
2022-03-12 23:07:45 +08:00
enable_system_proxy,
2022-11-20 20:12:58 +08:00
} = verge ?? {};
2022-01-17 02:42:52 +08:00
const onSwitchFormat = (_e: any, value: boolean) => value;
2022-11-19 17:22:29 +08:00
const onChangeData = (patch: Partial<IVergeConfig>) => {
2022-11-20 20:12:58 +08:00
mutateVerge({ ...verge, ...patch }, false);
2022-01-17 02:42:52 +08:00
};
return (
2022-03-12 23:07:45 +08:00
<SettingList title={t("System Setting")}>
<SysproxyViewer ref={sysproxyRef} />
2024-02-20 23:27:03 +08:00
<TunViewer ref={tunRef} />
2024-03-31 16:16:23 +08:00
{isServiceModeAvailable && (
<ServiceViewer ref={serviceRef} enable={!!enable_service_mode} />
)}
2022-09-07 01:51:43 +08:00
2024-02-19 18:10:10 +08:00
<SettingItem
label={t("Tun Mode")}
extra={
2024-02-20 23:27:03 +08:00
<>
<Tooltip
title={
2024-03-31 16:16:23 +08:00
isServiceModeAvailable
? t("Tun Mode Info Windows")
: t("Tun Mode Info Unix")
2024-02-20 23:27:03 +08:00
}
placement="top"
>
<IconButton color="inherit" size="small">
<InfoRounded
fontSize="inherit"
style={{ cursor: "pointer", opacity: 0.75 }}
/>
</IconButton>
</Tooltip>
<IconButton
color="inherit"
size="small"
onClick={() => tunRef.current?.open()}
>
<Settings
2024-02-19 18:10:10 +08:00
fontSize="inherit"
style={{ cursor: "pointer", opacity: 0.75 }}
/>
</IconButton>
2024-02-20 23:27:03 +08:00
</>
2024-02-19 18:10:10 +08:00
}
>
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 })}
2022-11-20 20:12:58 +08:00
onGuard={(e) => patchVerge({ enable_tun_mode: e })}
2022-02-25 02:09:39 +08:00
>
<Switch edge="end" />
</GuardState>
</SettingItem>
2024-03-31 16:16:23 +08:00
{isServiceModeAvailable && (
2022-08-06 03:48:03 +08:00
<SettingItem
label={t("Service Mode")}
extra={
2022-04-25 16:12:04 +08:00
<IconButton
color="inherit"
size="small"
2022-11-21 22:33:06 +08:00
onClick={() => serviceRef.current?.open()}
2022-04-25 16:12:04 +08:00
>
2022-11-23 17:45:22 +08:00
<PrivacyTipRounded
fontSize="inherit"
style={{ cursor: "pointer", opacity: 0.75 }}
/>
2022-04-25 16:12:04 +08:00
</IconButton>
2022-11-23 17:45:22 +08:00
}
>
<GuardState
value={enable_service_mode ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_service_mode: e })}
onGuard={(e) => patchVerge({ enable_service_mode: e })}
>
<Switch
edge="end"
disabled={
serviceStatus !== "active" && serviceStatus !== "installed"
}
/>
</GuardState>
2022-04-25 16:12:04 +08:00
</SettingItem>
)}
2022-09-07 01:51:43 +08:00
<SettingItem
label={t("System Proxy")}
extra={
2022-11-23 17:45:22 +08:00
<IconButton
color="inherit"
size="small"
onClick={() => sysproxyRef.current?.open()}
2022-11-23 17:45:22 +08:00
>
<Settings
fontSize="inherit"
style={{ cursor: "pointer", opacity: 0.75 }}
/>
</IconButton>
2022-09-07 01:51:43 +08:00
}
>
2022-01-17 02:42:52 +08:00
<GuardState
2022-09-07 01:51:43 +08:00
value={enable_system_proxy ?? false}
2022-01-17 02:42:52 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
2022-09-07 01:51:43 +08:00
onChange={(e) => onChangeData({ enable_system_proxy: e })}
2022-11-20 20:12:58 +08:00
onGuard={(e) => patchVerge({ enable_system_proxy: e })}
2022-01-17 02:42:52 +08:00
>
<Switch edge="end" />
</GuardState>
</SettingItem>
2022-09-07 01:51:43 +08:00
<SettingItem label={t("Auto Launch")}>
2022-03-26 18:56:16 +08:00
<GuardState
2022-09-07 01:51:43 +08:00
value={enable_auto_launch ?? false}
2022-03-26 18:56:16 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
2022-09-07 01:51:43 +08:00
onChange={(e) => onChangeData({ enable_auto_launch: e })}
2022-11-20 20:12:58 +08:00
onGuard={(e) => patchVerge({ enable_auto_launch: e })}
2022-03-26 18:56:16 +08:00
>
<Switch edge="end" />
</GuardState>
</SettingItem>
2022-09-07 01:51:43 +08:00
<SettingItem label={t("Silent Start")}>
2022-01-17 02:42:52 +08:00
<GuardState
2022-09-07 01:51:43 +08:00
value={enable_silent_start ?? false}
2022-01-17 02:42:52 +08:00
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
2022-09-07 01:51:43 +08:00
onChange={(e) => onChangeData({ enable_silent_start: e })}
2022-11-20 20:12:58 +08:00
onGuard={(e) => patchVerge({ enable_silent_start: e })}
2022-01-17 02:42:52 +08:00
>
<Switch edge="end" />
</GuardState>
</SettingItem>
</SettingList>
);
};
export default SettingSystem;