import { useRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import useSWR, { mutate } from "swr"; import { SettingsRounded, PlayCircleOutlineRounded, PauseCircleOutlineRounded, BuildRounded, } from "@mui/icons-material"; import { Box, Button, Tooltip, Typography, alpha, useTheme, } from "@mui/material"; import { DialogRef, Notice, Switch } from "@/components/base"; import { TooltipIcon } from "@/components/base/base-tooltip-icon"; import { GuardState } from "@/components/setting/mods/guard-state"; import { SysproxyViewer } from "@/components/setting/mods/sysproxy-viewer"; import { TunViewer } from "@/components/setting/mods/tun-viewer"; import { useVerge } from "@/hooks/use-verge"; import { getSystemProxy, getAutotemProxy, getRunningMode, installService, } from "@/services/cmds"; import { useLockFn } from "ahooks"; import { SettingItem } from "@/components/setting/mods/setting-comp"; interface ProxySwitchProps { label?: string; onError?: (err: Error) => void; } /** * 可复用的代理控制开关组件 * 包含 Tun Mode 和 System Proxy 的开关功能 */ const ProxyControlSwitches = ({ label, onError }: ProxySwitchProps) => { const { t } = useTranslation(); const { verge, mutateVerge, patchVerge } = useVerge(); const theme = useTheme(); const { data: sysproxy } = useSWR("getSystemProxy", getSystemProxy); const { data: autoproxy } = useSWR("getAutotemProxy", getAutotemProxy); const { data: runningMode, mutate: mutateRunningMode } = useSWR( "getRunningMode", getRunningMode, ); // 是否以sidecar模式运行 const isSidecarMode = runningMode === "sidecar"; const sysproxyRef = useRef(null); const tunRef = useRef(null); const { enable_tun_mode, enable_system_proxy, proxy_auto_config } = verge ?? {}; // 确定当前显示哪个开关 const isSystemProxyMode = label === t("System Proxy") || !label; const isTunMode = label === t("Tun Mode"); const onSwitchFormat = (_e: any, value: boolean) => value; const onChangeData = (patch: Partial) => { mutateVerge({ ...verge, ...patch }, false); }; const updateProxyStatus = async () => { // 等待一小段时间让系统代理状态变化 await new Promise((resolve) => setTimeout(resolve, 100)); await mutate("getSystemProxy"); await mutate("getAutotemProxy"); }; // 安装系统服务 const onInstallService = useLockFn(async () => { try { Notice.info(t("Installing Service..."), 1000); await installService(); Notice.success(t("Service Installed Successfully"), 2000); // 重新获取运行模式 await mutateRunningMode(); } catch (err: any) { Notice.error(err.message || err.toString(), 3000); } }); return ( {label && ( {label} )} {/* 仅显示当前选中的开关 */} {isSystemProxyMode && ( {proxy_auto_config ? ( autoproxy?.enable ? ( ) : ( ) ) : sysproxy?.enable ? ( ) : ( )} {t("System Proxy")} {/* {sysproxy?.enable ? t("Proxy is active") : t("Enable this for most users") } */} sysproxyRef.current?.open()} > onChangeData({ enable_system_proxy: e })} onGuard={async (e) => { await patchVerge({ enable_system_proxy: e }); await updateProxyStatus(); }} > )} {isTunMode && ( {enable_tun_mode ? ( ) : ( )} {t("Tun Mode")} {/* {isSidecarMode ? t("TUN requires Service Mode") : t("For special applications") } */} {isSidecarMode && ( )} tunRef.current?.open()} > { // 当在sidecar模式下禁用切换 if (isSidecarMode) return; onChangeData({ enable_tun_mode: e }); }} onGuard={(e) => { // 当在sidecar模式下禁用切换 if (isSidecarMode) { Notice.error(t("TUN requires Service Mode"), 2000); return Promise.reject( new Error(t("TUN requires Service Mode")), ); } return patchVerge({ enable_tun_mode: e }); }} > )} {/* 引用对话框组件 */} ); }; export default ProxyControlSwitches;