perf(proxy-chain): avoid duplicate proxy refresh in chain panel #5855

This commit is contained in:
Slinetrac 2025-12-31 16:42:56 +08:00
parent 609008f087
commit cb5a2e7ce3
No known key found for this signature in database

View File

@ -33,14 +33,13 @@ import {
} from "@mui/material"; } from "@mui/material";
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import useSWR from "swr";
import { import {
closeAllConnections, closeAllConnections,
selectNodeForGroup, selectNodeForGroup,
} from "tauri-plugin-mihomo-api"; } from "tauri-plugin-mihomo-api";
import { useProxiesData } from "@/hooks/use-clash-data"; import { useProxiesData } from "@/hooks/use-clash-data";
import { calcuProxies, updateProxyChainConfigInRuntime } from "@/services/cmds"; import { updateProxyChainConfigInRuntime } from "@/services/cmds";
import { debugLog } from "@/utils/debug"; import { debugLog } from "@/utils/debug";
interface ProxyChainItem { interface ProxyChainItem {
@ -200,44 +199,33 @@ export const ProxyChain = ({
}: ProxyChainProps) => { }: ProxyChainProps) => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
const { proxies } = useProxiesData(); const { proxies, refreshProxy } = useProxiesData();
const [isConnecting, setIsConnecting] = useState(false); const [isConnecting, setIsConnecting] = useState(false);
const markUnsavedChanges = useCallback(() => { const markUnsavedChanges = useCallback(() => {
onMarkUnsavedChanges?.(); onMarkUnsavedChanges?.();
}, [onMarkUnsavedChanges]); }, [onMarkUnsavedChanges]);
// 获取当前代理信息以检查连接状态
const { data: currentProxies, mutate: mutateProxies } = useSWR(
"getProxies",
calcuProxies,
{
revalidateOnFocus: true,
revalidateIfStale: true,
refreshInterval: 5000, // 每5秒刷新一次
},
);
const isConnected = useMemo(() => { const isConnected = useMemo(() => {
if (!currentProxies || proxyChain.length < 2) { if (!proxies || proxyChain.length < 2) {
return false; return false;
} }
const lastNode = proxyChain[proxyChain.length - 1]; const lastNode = proxyChain[proxyChain.length - 1];
if (mode === "global") { if (mode === "global") {
return currentProxies.global?.now === lastNode.name; return proxies.global?.now === lastNode.name;
} }
if (!selectedGroup || !Array.isArray(currentProxies.groups)) { if (!selectedGroup || !Array.isArray(proxies.groups)) {
return false; return false;
} }
const proxyChainGroup = currentProxies.groups.find( const proxyChainGroup = proxies.groups.find(
(group) => group.name === selectedGroup, (group) => group.name === selectedGroup,
); );
return proxyChainGroup?.now === lastNode.name; return proxyChainGroup?.now === lastNode.name;
}, [currentProxies, proxyChain, mode, selectedGroup]); }, [proxies, proxyChain, mode, selectedGroup]);
// 监听链的变化,但排除从配置加载的情况 // 监听链的变化,但排除从配置加载的情况
const chainLengthRef = useRef(proxyChain.length); const chainLengthRef = useRef(proxyChain.length);
@ -313,7 +301,7 @@ export const ProxyChain = ({
localStorage.removeItem("proxy-chain-items"); localStorage.removeItem("proxy-chain-items");
await closeAllConnections(); await closeAllConnections();
await mutateProxies(); await refreshProxy();
onUpdateChain([]); onUpdateChain([]);
} catch (error) { } catch (error) {
@ -354,7 +342,7 @@ export const ProxyChain = ({
localStorage.setItem("proxy-chain-exit-node", lastNode.name); localStorage.setItem("proxy-chain-exit-node", lastNode.name);
// 刷新代理信息以更新连接状态 // 刷新代理信息以更新连接状态
mutateProxies(); refreshProxy();
debugLog("Successfully connected to proxy chain"); debugLog("Successfully connected to proxy chain");
} catch (error) { } catch (error) {
console.error("Failed to connect to proxy chain:", error); console.error("Failed to connect to proxy chain:", error);
@ -366,7 +354,7 @@ export const ProxyChain = ({
proxyChain, proxyChain,
isConnected, isConnected,
t, t,
mutateProxies, refreshProxy,
mode, mode,
selectedGroup, selectedGroup,
onUpdateChain, onUpdateChain,