2022-11-20 19:46:16 +08:00
|
|
|
|
import useSWR from "swr";
|
2025-02-17 16:07:46 +08:00
|
|
|
|
import { useEffect, useMemo, useRef, useState, useCallback } from "react";
|
2022-12-13 17:34:39 +08:00
|
|
|
|
import { getProxies } from "@/services/api";
|
|
|
|
|
|
import { useVerge } from "@/hooks/use-verge";
|
2022-11-20 19:46:16 +08:00
|
|
|
|
import { filterSort } from "./use-filter-sort";
|
2022-12-14 15:07:51 +08:00
|
|
|
|
import { useWindowWidth } from "./use-window-width";
|
2022-11-21 22:10:24 +08:00
|
|
|
|
import {
|
|
|
|
|
|
useHeadStateNew,
|
|
|
|
|
|
DEFAULT_STATE,
|
|
|
|
|
|
type HeadState,
|
|
|
|
|
|
} from "./use-head-state";
|
2022-11-20 19:46:16 +08:00
|
|
|
|
|
|
|
|
|
|
export interface IRenderItem {
|
2022-12-13 17:34:39 +08:00
|
|
|
|
// 组 | head | item | empty | item col
|
|
|
|
|
|
type: 0 | 1 | 2 | 3 | 4;
|
2022-11-20 19:46:16 +08:00
|
|
|
|
key: string;
|
|
|
|
|
|
group: IProxyGroupItem;
|
|
|
|
|
|
proxy?: IProxyItem;
|
2022-12-13 17:34:39 +08:00
|
|
|
|
col?: number;
|
|
|
|
|
|
proxyCol?: IProxyItem[];
|
2022-11-20 19:46:16 +08:00
|
|
|
|
headState?: HeadState;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
interface ProxiesData {
|
|
|
|
|
|
groups: IProxyGroupItem[];
|
|
|
|
|
|
global?: IProxyGroupItem;
|
|
|
|
|
|
proxies: any[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 缓存计算结果
|
|
|
|
|
|
const groupCache = new WeakMap<ProxiesData, Map<string, IProxyGroupItem>>();
|
|
|
|
|
|
// 用于追踪缓存的key
|
|
|
|
|
|
const cacheKeys = new Set<ProxiesData>();
|
|
|
|
|
|
|
2022-11-20 19:46:16 +08:00
|
|
|
|
export const useRenderList = (mode: string) => {
|
2025-02-17 16:07:46 +08:00
|
|
|
|
// 添加用户交互标记
|
|
|
|
|
|
const isUserInteracting = useRef(false);
|
|
|
|
|
|
const interactionTimer = useRef<number | null>(null);
|
|
|
|
|
|
// 添加上一次有效的数据缓存
|
|
|
|
|
|
const [lastValidData, setLastValidData] = useState<ProxiesData | null>(null);
|
|
|
|
|
|
// 添加刷新锁
|
|
|
|
|
|
const refreshLock = useRef(false);
|
|
|
|
|
|
const lastRenderList = useRef<IRenderItem[]>([]);
|
|
|
|
|
|
|
|
|
|
|
|
// 组件卸载时清理
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
if (interactionTimer.current) {
|
|
|
|
|
|
clearTimeout(interactionTimer.current);
|
|
|
|
|
|
}
|
|
|
|
|
|
refreshLock.current = false;
|
|
|
|
|
|
isUserInteracting.current = false;
|
|
|
|
|
|
// 清理 WeakMap 缓存
|
|
|
|
|
|
cacheKeys.forEach((key) => {
|
|
|
|
|
|
groupCache.delete(key);
|
|
|
|
|
|
});
|
|
|
|
|
|
cacheKeys.clear();
|
|
|
|
|
|
};
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
// 优化数据获取函数
|
|
|
|
|
|
const fetchProxies = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (isUserInteracting.current || refreshLock.current) {
|
|
|
|
|
|
return lastValidData;
|
|
|
|
|
|
}
|
|
|
|
|
|
const data = await getProxies();
|
|
|
|
|
|
|
|
|
|
|
|
// 预处理和缓存组数据
|
|
|
|
|
|
if (data && !groupCache.has(data)) {
|
|
|
|
|
|
const groupMap = new Map();
|
|
|
|
|
|
data.groups.forEach((group) => {
|
|
|
|
|
|
groupMap.set(group.name, group);
|
|
|
|
|
|
});
|
|
|
|
|
|
groupCache.set(data, groupMap);
|
|
|
|
|
|
cacheKeys.add(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setLastValidData(data);
|
|
|
|
|
|
return data;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (lastValidData) return lastValidData;
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [lastValidData]);
|
|
|
|
|
|
|
2022-11-20 19:46:16 +08:00
|
|
|
|
const { data: proxiesData, mutate: mutateProxies } = useSWR(
|
|
|
|
|
|
"getProxies",
|
2025-02-17 16:07:46 +08:00
|
|
|
|
fetchProxies,
|
2025-02-06 07:58:42 +08:00
|
|
|
|
{
|
2025-02-17 16:07:46 +08:00
|
|
|
|
refreshInterval: isUserInteracting.current ? 0 : 2000,
|
2025-02-06 07:58:42 +08:00
|
|
|
|
dedupingInterval: 1000,
|
|
|
|
|
|
revalidateOnFocus: false,
|
2025-02-17 16:07:46 +08:00
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
|
if (!data || isUserInteracting.current) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (proxiesData) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const groupMap = groupCache.get(proxiesData);
|
|
|
|
|
|
if (!groupMap) return;
|
|
|
|
|
|
|
|
|
|
|
|
const needUpdate = data.groups.some((group: IProxyGroupItem) => {
|
|
|
|
|
|
const oldGroup = groupMap.get(group.name);
|
|
|
|
|
|
return !oldGroup || oldGroup.now !== group.now;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!needUpdate) return;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("Data comparison error:", e);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-02-06 07:58:42 +08:00
|
|
|
|
},
|
2022-11-20 19:46:16 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
// 优化mutateProxies包装函数
|
|
|
|
|
|
const wrappedMutateProxies = useCallback(async () => {
|
|
|
|
|
|
if (interactionTimer.current) {
|
|
|
|
|
|
clearTimeout(interactionTimer.current);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-02-18 00:37:55 +08:00
|
|
|
|
// 立即更新本地状态以响应UI
|
|
|
|
|
|
if (proxiesData) {
|
|
|
|
|
|
const currentGroup = proxiesData.groups.find(
|
|
|
|
|
|
(g) => g.now !== undefined,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (currentGroup) {
|
|
|
|
|
|
const optimisticData = { ...proxiesData };
|
|
|
|
|
|
setLastValidData(optimisticData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 执行实际的更新并等待结果
|
|
|
|
|
|
const result = await mutateProxies();
|
|
|
|
|
|
|
|
|
|
|
|
// 更新最新数据
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
setLastValidData(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("Failed to update proxies:", error);
|
|
|
|
|
|
// 发生错误时恢复到之前的状态
|
|
|
|
|
|
if (proxiesData) {
|
2025-02-17 16:07:46 +08:00
|
|
|
|
setLastValidData(proxiesData);
|
|
|
|
|
|
}
|
2025-02-18 00:37:55 +08:00
|
|
|
|
throw error;
|
2025-02-17 16:07:46 +08:00
|
|
|
|
} finally {
|
2025-02-18 00:37:55 +08:00
|
|
|
|
// 重置状态
|
|
|
|
|
|
isUserInteracting.current = false;
|
|
|
|
|
|
refreshLock.current = false;
|
|
|
|
|
|
if (interactionTimer.current) {
|
|
|
|
|
|
clearTimeout(interactionTimer.current);
|
2025-02-17 16:07:46 +08:00
|
|
|
|
interactionTimer.current = null;
|
2025-02-18 00:37:55 +08:00
|
|
|
|
}
|
2025-02-17 16:07:46 +08:00
|
|
|
|
}
|
2025-02-18 00:37:55 +08:00
|
|
|
|
}, [proxiesData, mutateProxies]);
|
2025-02-17 16:07:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 确保初始数据加载后更新lastValidData
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (proxiesData && !lastValidData) {
|
|
|
|
|
|
setLastValidData(proxiesData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [proxiesData]);
|
|
|
|
|
|
|
2022-12-13 17:34:39 +08:00
|
|
|
|
const { verge } = useVerge();
|
2022-12-14 15:07:51 +08:00
|
|
|
|
const { width } = useWindowWidth();
|
|
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
const col = useMemo(() => {
|
|
|
|
|
|
const baseCol = Math.floor(verge?.proxy_layout_column || 6);
|
|
|
|
|
|
if (baseCol >= 6 || baseCol <= 0) {
|
|
|
|
|
|
if (width > 1450) return 4;
|
|
|
|
|
|
if (width > 1024) return 3;
|
|
|
|
|
|
if (width > 900) return 2;
|
|
|
|
|
|
if (width >= 600) return 2;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return baseCol;
|
|
|
|
|
|
}, [verge?.proxy_layout_column, width]);
|
2022-12-13 17:34:39 +08:00
|
|
|
|
|
2022-11-20 19:46:16 +08:00
|
|
|
|
const [headStates, setHeadState] = useHeadStateNew();
|
|
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
// 优化初始数据加载
|
2022-11-20 19:46:16 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!proxiesData) return;
|
|
|
|
|
|
const { groups, proxies } = proxiesData;
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
(mode === "rule" && !groups.length) ||
|
|
|
|
|
|
(mode === "global" && proxies.length < 2)
|
|
|
|
|
|
) {
|
2025-02-17 16:07:46 +08:00
|
|
|
|
const timer = setTimeout(() => mutateProxies(), 500);
|
|
|
|
|
|
return () => clearTimeout(timer);
|
2022-11-20 19:46:16 +08:00
|
|
|
|
}
|
2025-02-17 16:07:46 +08:00
|
|
|
|
}, [proxiesData, mode, mutateProxies]);
|
2022-11-20 19:46:16 +08:00
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
// 优化渲染列表计算
|
|
|
|
|
|
const renderList = useMemo(() => {
|
|
|
|
|
|
const currentData = proxiesData || lastValidData;
|
|
|
|
|
|
if (!currentData) return lastRenderList.current;
|
2022-11-21 22:10:24 +08:00
|
|
|
|
|
2022-11-20 19:46:16 +08:00
|
|
|
|
const useRule = mode === "rule" || mode === "script";
|
|
|
|
|
|
const renderGroups =
|
2025-02-17 16:07:46 +08:00
|
|
|
|
(useRule && currentData.groups.length
|
|
|
|
|
|
? currentData.groups
|
|
|
|
|
|
: [currentData.global!]) || [];
|
2022-11-20 19:46:16 +08:00
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
const newList = renderGroups.flatMap((group: IProxyGroupItem) => {
|
2022-11-21 22:10:24 +08:00
|
|
|
|
const headState = headStates[group.name] || DEFAULT_STATE;
|
2022-11-20 19:46:16 +08:00
|
|
|
|
const ret: IRenderItem[] = [
|
|
|
|
|
|
{ type: 0, key: group.name, group, headState },
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2022-11-21 22:10:24 +08:00
|
|
|
|
if (headState?.open || !useRule) {
|
2022-11-20 19:46:16 +08:00
|
|
|
|
const proxies = filterSort(
|
|
|
|
|
|
group.all,
|
|
|
|
|
|
group.name,
|
|
|
|
|
|
headState.filterText,
|
2024-11-24 00:14:46 +08:00
|
|
|
|
headState.sortType,
|
2022-11-20 19:46:16 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2022-12-13 17:34:39 +08:00
|
|
|
|
ret.push({ type: 1, key: `head-${group.name}`, group, headState });
|
2022-11-20 19:46:16 +08:00
|
|
|
|
|
|
|
|
|
|
if (!proxies.length) {
|
2022-12-13 17:34:39 +08:00
|
|
|
|
ret.push({ type: 3, key: `empty-${group.name}`, group, headState });
|
2025-02-17 16:07:46 +08:00
|
|
|
|
return ret;
|
2022-12-13 17:34:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (col > 1) {
|
|
|
|
|
|
return ret.concat(
|
|
|
|
|
|
groupList(proxies, col).map((proxyCol) => ({
|
|
|
|
|
|
type: 4,
|
2022-12-14 15:07:51 +08:00
|
|
|
|
key: `col-${group.name}-${proxyCol[0].name}`,
|
2022-12-13 17:34:39 +08:00
|
|
|
|
group,
|
|
|
|
|
|
headState,
|
|
|
|
|
|
col,
|
|
|
|
|
|
proxyCol,
|
2024-11-24 00:14:46 +08:00
|
|
|
|
})),
|
2022-12-13 17:34:39 +08:00
|
|
|
|
);
|
2022-11-20 19:46:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ret.concat(
|
|
|
|
|
|
proxies.map((proxy) => ({
|
|
|
|
|
|
type: 2,
|
|
|
|
|
|
key: `${group.name}-${proxy!.name}`,
|
|
|
|
|
|
group,
|
|
|
|
|
|
proxy,
|
|
|
|
|
|
headState,
|
2024-11-24 00:14:46 +08:00
|
|
|
|
})),
|
2022-11-20 19:46:16 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-17 16:07:46 +08:00
|
|
|
|
const filteredList = !useRule
|
|
|
|
|
|
? newList.slice(1)
|
|
|
|
|
|
: newList.filter((item) => !item.group.hidden);
|
|
|
|
|
|
|
|
|
|
|
|
lastRenderList.current = filteredList;
|
|
|
|
|
|
return filteredList;
|
|
|
|
|
|
}, [headStates, proxiesData, lastValidData, mode, col]);
|
2022-11-20 19:46:16 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
renderList,
|
2025-02-17 16:07:46 +08:00
|
|
|
|
onProxies: wrappedMutateProxies,
|
2022-11-20 19:46:16 +08:00
|
|
|
|
onHeadState: setHeadState,
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
2022-12-13 17:34:39 +08:00
|
|
|
|
|
|
|
|
|
|
function groupList<T = any>(list: T[], size: number): T[][] {
|
|
|
|
|
|
return list.reduce((p, n) => {
|
|
|
|
|
|
if (!p.length) return [[n]];
|
|
|
|
|
|
|
|
|
|
|
|
const i = p.length - 1;
|
|
|
|
|
|
if (p[i].length < size) {
|
|
|
|
|
|
p[i].push(n);
|
|
|
|
|
|
return p;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.push([n]);
|
|
|
|
|
|
return p;
|
|
|
|
|
|
}, [] as T[][]);
|
|
|
|
|
|
}
|