From 7ede91599c5cc1857cd2403b328e445681edda85 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Wed, 26 Mar 2025 22:04:16 +0800 Subject: [PATCH] fix: standardize RunningMode handling between frontend and backend This commit improves the type consistency between the Rust backend and TypeScript frontend by: 1. Modifying the Rust `get_running_mode()` command to return a String instead of RunningMode enum directly 2. Removing the RunningMode enum and IRunningMode interface from TypeScript types 3. Using string literals for mode comparison in frontend components 4. Standardizing on capitalized mode names (e.g., "Sidecar" instead of "sidecar") These changes ensure proper serialization/deserialization between backend and frontend, making the code more maintainable and reducing potential inconsistencies. --- src-tauri/src/module/sysinfo.rs | 7 +- src/components/home/proxy-tun-card.tsx | 197 +++++++++--------- src/components/home/system-info-card.tsx | 55 +++-- src/components/setting/setting-system.tsx | 2 +- .../shared/ProxyControlSwitches.tsx | 4 +- src/services/cmds.ts | 4 +- src/services/types.d.ts | 10 - 7 files changed, 134 insertions(+), 145 deletions(-) diff --git a/src-tauri/src/module/sysinfo.rs b/src-tauri/src/module/sysinfo.rs index 58f21097..15cd8480 100644 --- a/src-tauri/src/module/sysinfo.rs +++ b/src-tauri/src/module/sysinfo.rs @@ -35,11 +35,8 @@ impl PlatformSpecification { // Get running mode asynchronously let running_mode = tokio::task::block_in_place(|| { tokio::runtime::Handle::current().block_on(async { - match CoreManager::global().get_running_mode().await { - crate::core::RunningMode::Service => "Service".to_string(), - crate::core::RunningMode::Sidecar => "Standalone".to_string(), - crate::core::RunningMode::NotRunning => "Not Running".to_string(), - } + let running_mode = CoreManager::global().get_running_mode().await; + running_mode.to_string() }) }); diff --git a/src/components/home/proxy-tun-card.tsx b/src/components/home/proxy-tun-card.tsx index 038ab5a9..89300106 100644 --- a/src/components/home/proxy-tun-card.tsx +++ b/src/components/home/proxy-tun-card.tsx @@ -32,71 +32,64 @@ interface TabButtonProps { } // 抽取Tab组件以减少重复代码 -const TabButton: FC = memo(({ - isActive, - onClick, - icon: Icon, - label, - hasIndicator = false -}) => ( - - - = memo( + ({ isActive, onClick, icon: Icon, label, hasIndicator = false }) => ( + - {label} - - {hasIndicator && ( - - )} - -)); + + + {label} + + {hasIndicator && ( + + )} + + ), +); interface TabDescriptionProps { description: string; @@ -104,44 +97,46 @@ interface TabDescriptionProps { } // 抽取描述文本组件 -const TabDescription: FC = memo(({ description, tooltipTitle }) => ( - - - {description} - - - - - -)); +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 [activeTab, setActiveTab] = useState( + () => localStorage.getItem(LOCAL_STORAGE_TAB_KEY) || "system", ); // 获取代理状态信息 @@ -152,7 +147,7 @@ export const ProxyTunCard: FC = () => { const { enable_system_proxy, enable_tun_mode } = verge ?? {}; // 是否以sidecar模式运行 - const isSidecarMode = runningMode === "sidecar"; + const isSidecarMode = runningMode === "Sidecar"; // 处理错误 const handleError = (err: Error) => { @@ -172,7 +167,7 @@ export const ProxyTunCard: FC = () => { text: enable_system_proxy ? t("System Proxy Enabled") : t("System Proxy Disabled"), - tooltip: t("System Proxy Info") + tooltip: t("System Proxy Info"), }; } else { return { @@ -181,7 +176,7 @@ export const ProxyTunCard: FC = () => { : enable_tun_mode ? t("TUN Mode Enabled") : t("TUN Mode Disabled"), - tooltip: t("TUN Mode Intercept Info") + tooltip: t("TUN Mode Intercept Info"), }; } }, [activeTab, enable_system_proxy, enable_tun_mode, isSidecarMode, t]); diff --git a/src/components/home/system-info-card.tsx b/src/components/home/system-info-card.tsx index 404c546b..e5aecf94 100644 --- a/src/components/home/system-info-card.tsx +++ b/src/components/home/system-info-card.tsx @@ -24,14 +24,14 @@ export const SystemInfoCard = () => { }); // 获取运行模式 - const { data: runningMode = "sidecar", mutate: mutateRunningMode } = useSWR( + const { data: runningMode = "Sidecar", mutate: mutateRunningMode } = useSWR( "getRunningMode", getRunningMode, - { suspense: false, revalidateOnFocus: false } + { suspense: false, revalidateOnFocus: false }, ); // 是否以sidecar模式运行 - const isSidecarMode = runningMode === "sidecar"; + const isSidecarMode = runningMode === "Sidecar"; // 初始化系统信息 useEffect(() => { @@ -42,7 +42,10 @@ export const SystemInfoCard = () => { if (lines.length > 0) { const sysName = lines[0].split(": ")[1] || ""; const sysVersion = lines[1].split(": ")[1] || ""; - setSystemState(prev => ({ ...prev, osInfo: `${sysName} ${sysVersion}` })); + setSystemState((prev) => ({ + ...prev, + osInfo: `${sysName} ${sysVersion}`, + })); } }) .catch(console.error); @@ -53,9 +56,9 @@ export const SystemInfoCard = () => { try { const timestamp = parseInt(lastCheck, 10); if (!isNaN(timestamp)) { - setSystemState(prev => ({ - ...prev, - lastCheckUpdate: new Date(timestamp).toLocaleString() + setSystemState((prev) => ({ + ...prev, + lastCheckUpdate: new Date(timestamp).toLocaleString(), })); } } catch (e) { @@ -65,11 +68,11 @@ export const SystemInfoCard = () => { // 如果启用了自动检查更新但没有记录,设置当前时间并延迟检查 const now = Date.now(); localStorage.setItem("last_check_update", now.toString()); - setSystemState(prev => ({ - ...prev, - lastCheckUpdate: new Date(now).toLocaleString() + setSystemState((prev) => ({ + ...prev, + lastCheckUpdate: new Date(now).toLocaleString(), })); - + setTimeout(() => { if (verge?.auto_check_update) { checkUpdate().catch(console.error); @@ -84,9 +87,9 @@ export const SystemInfoCard = () => { async () => { const now = Date.now(); localStorage.setItem("last_check_update", now.toString()); - setSystemState(prev => ({ - ...prev, - lastCheckUpdate: new Date(now).toLocaleString() + setSystemState((prev) => ({ + ...prev, + lastCheckUpdate: new Date(now).toLocaleString(), })); return await checkUpdate(); }, @@ -94,7 +97,7 @@ export const SystemInfoCard = () => { revalidateOnFocus: false, refreshInterval: 24 * 60 * 60 * 1000, // 每天检查一次 dedupingInterval: 60 * 60 * 1000, // 1小时内不重复检查 - } + }, ); // 导航到设置页面 @@ -147,16 +150,22 @@ export const SystemInfoCard = () => { }); // 是否启用自启动 - const autoLaunchEnabled = useMemo(() => verge?.enable_auto_launch || false, [verge]); + const autoLaunchEnabled = useMemo( + () => verge?.enable_auto_launch || false, + [verge], + ); // 运行模式样式 - const runningModeStyle = useMemo(() => ({ - cursor: isSidecarMode ? "pointer" : "default", - textDecoration: isSidecarMode ? "underline" : "none", - "&:hover": { - opacity: isSidecarMode ? 0.7 : 1, - }, - }), [isSidecarMode]); + const runningModeStyle = useMemo( + () => ({ + cursor: isSidecarMode ? "pointer" : "default", + textDecoration: isSidecarMode ? "underline" : "none", + "&:hover": { + opacity: isSidecarMode ? 0.7 : 1, + }, + }), + [isSidecarMode], + ); // 只有当verge存在时才渲染内容 if (!verge) return null; diff --git a/src/components/setting/setting-system.tsx b/src/components/setting/setting-system.tsx index bdd47393..7731c3d7 100644 --- a/src/components/setting/setting-system.tsx +++ b/src/components/setting/setting-system.tsx @@ -58,7 +58,7 @@ const SettingSystem = ({ onError }: Props) => { }, [autoLaunchEnabled]); // 是否以sidecar模式运行 - const isSidecarMode = runningMode === "sidecar"; + const isSidecarMode = runningMode === "Sidecar"; const sysproxyRef = useRef(null); const tunRef = useRef(null); diff --git a/src/components/shared/ProxyControlSwitches.tsx b/src/components/shared/ProxyControlSwitches.tsx index db7c66ba..8586d0d3 100644 --- a/src/components/shared/ProxyControlSwitches.tsx +++ b/src/components/shared/ProxyControlSwitches.tsx @@ -52,7 +52,7 @@ const ProxyControlSwitches = ({ label, onError }: ProxySwitchProps) => { ); // 是否以sidecar模式运行 - const isSidecarMode = runningMode === "sidecar"; + const isSidecarMode = runningMode === "Sidecar"; const sysproxyRef = useRef(null); const tunRef = useRef(null); @@ -138,7 +138,7 @@ const ProxyControlSwitches = ({ label, onError }: ProxySwitchProps) => { {t("System Proxy")} {/* - {sysproxy?.enable + {sysproxy?.enable ? t("Proxy is active") : t("Enable this for most users") } diff --git a/src/services/cmds.ts b/src/services/cmds.ts index 9ab2c21d..2fdb0b0c 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -1,7 +1,5 @@ -import dayjs from "dayjs"; import { invoke } from "@tauri-apps/api/core"; import { Notice } from "@/components/base"; -import { IRunningMode } from "./types"; export async function copyClashEnv() { return invoke("copy_clash_env"); @@ -313,7 +311,7 @@ export async function validateScriptFile(filePath: string) { // 获取当前运行模式 export const getRunningMode = async () => { - return invoke("get_running_mode"); + return invoke("get_running_mode"); }; // 获取应用运行时间 diff --git a/src/services/types.d.ts b/src/services/types.d.ts index e40eda10..09ae045d 100644 --- a/src/services/types.d.ts +++ b/src/services/types.d.ts @@ -806,13 +806,3 @@ interface IWebDavConfig { username: string; password: string; } - -export enum RunningMode { - Service = "Service", - Sidecar = "Sidecar", - NotRunning = "NotRunning", -} - -export interface IRunningMode { - mode: RunningMode; -}