import { useTranslation } from "react-i18next"; import { Box, Typography, Stack, Paper, Tooltip, alpha, useTheme, Fade, } from "@mui/material"; import { useState, useMemo, memo, FC, useEffect } from "react"; import ProxyControlSwitches from "@/components/shared/ProxyControlSwitches"; import { ComputerRounded, TroubleshootRounded, HelpOutlineRounded, SvgIconComponent, } from "@mui/icons-material"; import { useVerge } from "@/hooks/use-verge"; import { useSystemState } from "@/hooks/use-system-state"; import { showNotice } from "@/services/noticeService"; import { getRunningMode } from "@/services/cmds"; import { mutate } from "swr"; const LOCAL_STORAGE_TAB_KEY = "clash-verge-proxy-active-tab"; interface TabButtonProps { isActive: boolean; onClick: () => void; icon: SvgIconComponent; label: string; hasIndicator?: boolean; } // Tab组件 const TabButton: FC = memo( ({ isActive, onClick, icon: Icon, label, hasIndicator = false }) => ( {label} {hasIndicator && ( )} ), ); interface TabDescriptionProps { description: string; tooltipTitle: string; } // 描述文本组件 const TabDescription: FC = memo( ({ description, tooltipTitle }) => ( {description} ), ); export const ProxyTunCard: FC = () => { const { t } = useTranslation(); const theme = useTheme(); const [activeTab, setActiveTab] = useState( () => localStorage.getItem(LOCAL_STORAGE_TAB_KEY) || "system", ); const [localServiceOk, setLocalServiceOk] = useState(false); const { verge } = useVerge(); const { isAdminMode } = useSystemState(); const { enable_system_proxy, enable_tun_mode } = verge ?? {}; const updateLocalStatus = async () => { try { const runningMode = await getRunningMode(); const serviceStatus = runningMode === "Service"; setLocalServiceOk(serviceStatus); mutate("isServiceAvailable", serviceStatus, false); } catch (error) { console.error("更新TUN状态失败:", error); } }; useEffect(() => { updateLocalStatus(); }, []); const isTunAvailable = localServiceOk || isAdminMode; const handleError = (err: Error) => { showNotice("error", err.message || err.toString()); }; const handleTabChange = (tab: string) => { setActiveTab(tab); localStorage.setItem(LOCAL_STORAGE_TAB_KEY, tab); if (tab === "tun") { updateLocalStatus(); } }; const tabDescription = useMemo(() => { if (activeTab === "system") { return { text: enable_system_proxy ? t("System Proxy Enabled") : t("System Proxy Disabled"), tooltip: t("System Proxy Info"), }; } else { return { text: !isTunAvailable ? t("TUN Mode Service Required") : enable_tun_mode ? t("TUN Mode Enabled") : t("TUN Mode Disabled"), tooltip: t("TUN Mode Intercept Info"), }; } }, [activeTab, enable_system_proxy, enable_tun_mode, isTunAvailable, t]); return ( handleTabChange("system")} icon={ComputerRounded} label={t("System Proxy")} hasIndicator={enable_system_proxy} /> handleTabChange("tun")} icon={TroubleshootRounded} label={t("Tun Mode")} hasIndicator={enable_tun_mode && isTunAvailable} /> ); };