import { useTranslation } from "react-i18next"; import { Box, Typography, Stack, Paper, Tooltip, alpha, useTheme, Fade, } from "@mui/material"; import { useState, useMemo, memo, FC } from "react"; import ProxyControlSwitches from "@/components/shared/ProxyControlSwitches"; import { Notice } from "@/components/base"; import { ComputerRounded, TroubleshootRounded, HelpOutlineRounded, SvgIconComponent, } from "@mui/icons-material"; import { useVerge } from "@/hooks/use-verge"; import { useSystemState } from "@/hooks/use-system-state"; 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 { verge } = useVerge(); const { isSidecarMode, isAdminMode } = useSystemState(); // 从verge配置中获取开关状态 const { enable_system_proxy, enable_tun_mode } = verge ?? {}; // 判断Tun模式是否可用 - 当处于服务模式或管理员模式时可用 const isTunAvailable = !isSidecarMode || isAdminMode; // 处理错误 const handleError = (err: Error) => { Notice.error(err.message || err.toString(), 3000); }; // 处理标签切换并保存到localStorage const handleTabChange = (tab: string) => { setActiveTab(tab); localStorage.setItem(LOCAL_STORAGE_TAB_KEY, tab); }; // 用户提示文本 - 使用useMemo避免重复计算 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} /> {/* 说明文本区域 */} {/* 控制开关部分 */} ); };