mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-27 16:00:09 +08:00
refactor: reorganize component and hook structure; remove unused modules
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
/**
|
||||
* 资源清理 Hook
|
||||
* 用于在组件卸载或窗口关闭时统一清理资源
|
||||
*/
|
||||
export const useCleanup = () => {
|
||||
const cleanupFnsRef = useRef<Set<() => void>>(new Set());
|
||||
|
||||
const registerCleanup = (fn: () => void) => {
|
||||
cleanupFnsRef.current.add(fn);
|
||||
return () => {
|
||||
cleanupFnsRef.current.delete(fn);
|
||||
};
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
cleanupFnsRef.current.forEach((fn) => {
|
||||
try {
|
||||
fn();
|
||||
} catch (error) {
|
||||
console.error("[资源清理] 清理失败:", error);
|
||||
}
|
||||
});
|
||||
cleanupFnsRef.current.clear();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
cleanup();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { registerCleanup, cleanup };
|
||||
};
|
||||
@@ -1,74 +0,0 @@
|
||||
import { useMemo } from "react";
|
||||
|
||||
import { useClashConfig, useProxiesData } from "@/hooks/app-data";
|
||||
|
||||
// 获取当前代理节点信息的自定义Hook
|
||||
export const useCurrentProxy = () => {
|
||||
const { proxies, refreshProxy } = useProxiesData();
|
||||
const { clashConfig } = useClashConfig();
|
||||
|
||||
// 获取当前模式
|
||||
const currentMode = clashConfig?.mode?.toLowerCase() || "rule";
|
||||
|
||||
// 获取当前代理节点信息
|
||||
const currentProxyInfo = useMemo(() => {
|
||||
if (!proxies) return { currentProxy: null, primaryGroupName: null };
|
||||
|
||||
const globalGroup = proxies.global as IProxyGroupItem | undefined;
|
||||
const groups: IProxyGroupItem[] = Array.isArray(proxies.groups)
|
||||
? (proxies.groups as IProxyGroupItem[])
|
||||
: [];
|
||||
const records = (proxies.records || {}) as Record<string, IProxyItem>;
|
||||
|
||||
// 默认信息
|
||||
let primaryGroupName = "GLOBAL";
|
||||
let currentName = globalGroup?.now;
|
||||
|
||||
// 在规则模式下,寻找主要代理组(通常是第一个或者名字包含特定关键词的组)
|
||||
if (currentMode === "rule" && groups.length > 0) {
|
||||
// 查找主要的代理组(优先级:包含关键词 > 第一个非GLOBAL组)
|
||||
const primaryKeywords = [
|
||||
"auto",
|
||||
"select",
|
||||
"proxy",
|
||||
"节点选择",
|
||||
"自动选择",
|
||||
];
|
||||
const primaryGroup =
|
||||
groups.find((group) =>
|
||||
primaryKeywords.some((keyword) =>
|
||||
group.name.toLowerCase().includes(keyword.toLowerCase()),
|
||||
),
|
||||
) || groups.filter((g) => g.name !== "GLOBAL")[0];
|
||||
|
||||
if (primaryGroup) {
|
||||
primaryGroupName = primaryGroup.name;
|
||||
currentName = primaryGroup.now;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果找不到当前节点,返回null
|
||||
if (!currentName) return { currentProxy: null, primaryGroupName };
|
||||
|
||||
// 获取完整的节点信息
|
||||
const currentProxy = records[currentName] || {
|
||||
name: currentName,
|
||||
type: "Unknown",
|
||||
udp: false,
|
||||
xudp: false,
|
||||
tfo: false,
|
||||
mptcp: false,
|
||||
smux: false,
|
||||
history: [],
|
||||
};
|
||||
|
||||
return { currentProxy, primaryGroupName };
|
||||
}, [proxies, currentMode]);
|
||||
|
||||
return {
|
||||
currentProxy: currentProxyInfo.currentProxy,
|
||||
primaryGroupName: currentProxyInfo.primaryGroupName,
|
||||
mode: currentMode,
|
||||
refreshProxy,
|
||||
};
|
||||
};
|
||||
@@ -2,7 +2,7 @@ import { useLockFn } from "ahooks";
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { closeAllConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useSystemProxyData } from "@/hooks/app-data";
|
||||
import { useSystemProxyData } from "@/hooks/use-clash-data";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { getAutotemProxy } from "@/services/cmds";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user