2025-03-14 13:31:34 +08:00
|
|
|
|
import { useMemo } from "react";
|
2025-09-18 23:34:21 +08:00
|
|
|
|
|
2025-10-04 21:20:31 +08:00
|
|
|
|
import { useAppData } from "@/providers/app-data-context";
|
2025-03-26 13:26:32 +08:00
|
|
|
|
|
|
|
|
|
|
// 定义代理组类型
|
|
|
|
|
|
interface ProxyGroup {
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
now: string;
|
|
|
|
|
|
}
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取当前代理节点信息的自定义Hook
|
|
|
|
|
|
export const useCurrentProxy = () => {
|
2025-03-26 13:26:32 +08:00
|
|
|
|
// 从AppDataProvider获取数据
|
|
|
|
|
|
const { proxies, clashConfig, refreshProxy } = useAppData();
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取当前模式
|
|
|
|
|
|
const currentMode = clashConfig?.mode?.toLowerCase() || "rule";
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前代理节点信息
|
|
|
|
|
|
const currentProxyInfo = useMemo(() => {
|
2025-03-26 13:26:32 +08:00
|
|
|
|
if (!proxies) return { currentProxy: null, primaryGroupName: null };
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
2025-03-26 13:26:32 +08:00
|
|
|
|
const { global, groups, records } = proxies;
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 默认信息
|
|
|
|
|
|
let primaryGroupName = "GLOBAL";
|
|
|
|
|
|
let currentName = global?.now;
|
|
|
|
|
|
|
|
|
|
|
|
// 在规则模式下,寻找主要代理组(通常是第一个或者名字包含特定关键词的组)
|
|
|
|
|
|
if (currentMode === "rule" && groups.length > 0) {
|
|
|
|
|
|
// 查找主要的代理组(优先级:包含关键词 > 第一个非GLOBAL组)
|
|
|
|
|
|
const primaryKeywords = [
|
|
|
|
|
|
"auto",
|
|
|
|
|
|
"select",
|
|
|
|
|
|
"proxy",
|
|
|
|
|
|
"节点选择",
|
|
|
|
|
|
"自动选择",
|
|
|
|
|
|
];
|
|
|
|
|
|
const primaryGroup =
|
2025-03-26 13:26:32 +08:00
|
|
|
|
groups.find((group: ProxyGroup) =>
|
2025-03-14 13:31:34 +08:00
|
|
|
|
primaryKeywords.some((keyword) =>
|
|
|
|
|
|
group.name.toLowerCase().includes(keyword.toLowerCase()),
|
|
|
|
|
|
),
|
2025-03-26 13:26:32 +08:00
|
|
|
|
) || groups.filter((g: ProxyGroup) => g.name !== "GLOBAL")[0];
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
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 };
|
2025-03-26 13:26:32 +08:00
|
|
|
|
}, [proxies, currentMode]);
|
2025-03-14 13:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
currentProxy: currentProxyInfo.currentProxy,
|
|
|
|
|
|
primaryGroupName: currentProxyInfo.primaryGroupName,
|
|
|
|
|
|
mode: currentMode,
|
2025-03-26 13:26:32 +08:00
|
|
|
|
refreshProxy,
|
2025-03-14 13:31:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
};
|