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

147 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-09-07 01:51:43 +08:00
import useSWR from "swr";
2024-07-15 20:29:56 +08:00
import { useRef } from "react";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
import { SettingsRounded } from "@mui/icons-material";
2024-07-15 20:29:56 +08:00
import { checkService } from "@/services/cmds";
2022-11-20 20:12:58 +08:00
import { useVerge } from "@/hooks/use-verge";
import { DialogRef, Notice, Switch } from "@/components/base";
import { SettingList, SettingItem } from "./mods/setting-comp";
import { GuardState } from "./mods/guard-state";
2024-07-15 20:29:56 +08:00
import { ServiceSwitcher } from "./mods/service-switcher";
import { SysproxyViewer } from "./mods/sysproxy-viewer";
2024-02-20 23:27:03 +08:00
import { TunViewer } from "./mods/tun-viewer";
import { TooltipIcon } from "@/components/base/base-tooltip-icon";
2022-01-17 02:42:52 +08:00
interface Props {
onError?: (err: Error) => void;
}
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-04-25 16:12:04 +08:00
// service mode
const { data: serviceStatus, mutate: mutateServiceStatus } = useSWR(
"checkService",
checkService,
{
revalidateIfStale: false,
shouldRetryOnError: false,
focusThrottleInterval: 36e5, // 1 hour
}
);
2022-04-25 16:12:04 +08:00
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-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} />
2022-09-07 01:51:43 +08:00
2024-02-19 18:10:10 +08:00
<SettingItem
label={t("Tun Mode")}
extra={
<TooltipIcon
title={t("Tun Mode Info")}
2024-07-13 19:01:16 +08:00
icon={SettingsRounded}
onClick={() => tunRef.current?.open()}
/>
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 });
}}
onGuard={(e) => patchVerge({ enable_tun_mode: e })}
2022-02-25 02:09:39 +08:00
>
<Switch edge="end" />
2022-02-25 02:09:39 +08:00
</GuardState>
</SettingItem>
2024-09-30 01:04:15 +08:00
<SettingItem
label={t("Service Mode")}
extra={<TooltipIcon title={t("Service Mode Info")} />}
>
2024-07-15 20:29:56 +08:00
<ServiceSwitcher
status={serviceStatus ?? "unknown"}
mutate={mutateServiceStatus}
patchVerge={patchVerge}
onChangeData={onChangeData}
/>
</SettingItem>
2022-04-25 16:12:04 +08:00
2022-09-07 01:51:43 +08:00
<SettingItem
label={t("System Proxy")}
extra={
2024-06-07 10:32:27 +08:00
<>
<TooltipIcon
title={t("System Proxy Info")}
2024-07-13 19:01:16 +08:00
icon={SettingsRounded}
2024-06-07 10:32:27 +08:00
onClick={() => sysproxyRef.current?.open()}
/>
2024-06-07 10:32:27 +08:00
</>
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>
2024-06-26 05:33:06 +08:00
<SettingItem
label={t("Silent Start")}
extra={<TooltipIcon title={t("Silent Start Info")} />}
2024-06-26 05:33:06 +08:00
>
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;