Revert "refactor: profile switch (#5197)"
This reverts commit c2dcd86722.
This commit is contained in:
@@ -2,6 +2,12 @@ import { useMemo } from "react";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
|
||||
// 定义代理组类型
|
||||
interface ProxyGroup {
|
||||
name: string;
|
||||
now: string;
|
||||
}
|
||||
|
||||
// 获取当前代理节点信息的自定义Hook
|
||||
export const useCurrentProxy = () => {
|
||||
// 从AppDataProvider获取数据
|
||||
@@ -31,15 +37,15 @@ export const useCurrentProxy = () => {
|
||||
"自动选择",
|
||||
];
|
||||
const primaryGroup =
|
||||
groups.find((group) =>
|
||||
groups.find((group: ProxyGroup) =>
|
||||
primaryKeywords.some((keyword) =>
|
||||
group.name.toLowerCase().includes(keyword.toLowerCase()),
|
||||
),
|
||||
) || groups.find((group) => group.name !== "GLOBAL");
|
||||
) || groups.filter((g: ProxyGroup) => g.name !== "GLOBAL")[0];
|
||||
|
||||
if (primaryGroup) {
|
||||
primaryGroupName = primaryGroup.name;
|
||||
currentName = primaryGroup.now ?? currentName;
|
||||
currentName = primaryGroup.now;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,54 +5,33 @@ import {
|
||||
getProfiles,
|
||||
patchProfile,
|
||||
patchProfilesConfig,
|
||||
calcuProxies,
|
||||
} from "@/services/cmds";
|
||||
import {
|
||||
useProfileStore,
|
||||
selectEffectiveProfiles,
|
||||
selectIsHydrating,
|
||||
selectLastResult,
|
||||
} from "@/stores/profile-store";
|
||||
import { calcuProxies } from "@/services/cmds";
|
||||
|
||||
export const useProfiles = () => {
|
||||
const profilesFromStore = useProfileStore(selectEffectiveProfiles);
|
||||
const storeHydrating = useProfileStore(selectIsHydrating);
|
||||
const lastResult = useProfileStore(selectLastResult);
|
||||
const commitProfileSnapshot = useProfileStore(
|
||||
(state) => state.commitHydrated,
|
||||
);
|
||||
|
||||
const {
|
||||
data: swrProfiles,
|
||||
data: profiles,
|
||||
mutate: mutateProfiles,
|
||||
error,
|
||||
isValidating,
|
||||
} = useSWR("getProfiles", getProfiles, {
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
dedupingInterval: 500,
|
||||
dedupingInterval: 500, // 减少去重时间,提高响应性
|
||||
errorRetryCount: 3,
|
||||
errorRetryInterval: 1000,
|
||||
refreshInterval: 0,
|
||||
onError: (err) => {
|
||||
console.error("[useProfiles] SWR错误:", err);
|
||||
refreshInterval: 0, // 完全由手动控制
|
||||
onError: (error) => {
|
||||
console.error("[useProfiles] SWR错误:", error);
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
commitProfileSnapshot(data);
|
||||
console.log(
|
||||
"[useProfiles] 配置数据更新成功,配置数量",
|
||||
"[useProfiles] 配置数据更新成功,配置数量:",
|
||||
data?.items?.length || 0,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const rawProfiles = profilesFromStore ?? swrProfiles;
|
||||
const profiles = (rawProfiles ?? {
|
||||
current: null,
|
||||
items: [],
|
||||
}) as IProfilesConfig;
|
||||
const hasProfiles = rawProfiles != null;
|
||||
|
||||
const patchProfiles = async (
|
||||
value: Partial<IProfilesConfig>,
|
||||
signal?: AbortSignal,
|
||||
@@ -70,30 +49,32 @@ export const useProfiles = () => {
|
||||
await mutateProfiles();
|
||||
|
||||
return success;
|
||||
} catch (err) {
|
||||
if (err instanceof DOMException && err.name === "AbortError") {
|
||||
throw err;
|
||||
} catch (error) {
|
||||
if (error instanceof DOMException && error.name === "AbortError") {
|
||||
throw error;
|
||||
}
|
||||
|
||||
await mutateProfiles();
|
||||
throw err;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const patchCurrent = async (value: Partial<IProfileItem>) => {
|
||||
if (!hasProfiles || !profiles.current) {
|
||||
return;
|
||||
if (profiles?.current) {
|
||||
await patchProfile(profiles.current, value);
|
||||
mutateProfiles();
|
||||
}
|
||||
await patchProfile(profiles.current, value);
|
||||
mutateProfiles();
|
||||
};
|
||||
|
||||
// 根据selected的节点选择
|
||||
const activateSelected = async () => {
|
||||
try {
|
||||
console.log("[ActivateSelected] 开始处理代理选择");
|
||||
|
||||
const proxiesData = await calcuProxies();
|
||||
const profileData = hasProfiles ? profiles : null;
|
||||
const [proxiesData, profileData] = await Promise.all([
|
||||
calcuProxies(),
|
||||
getProfiles(),
|
||||
]);
|
||||
|
||||
if (!profileData || !proxiesData) {
|
||||
console.log("[ActivateSelected] 代理或配置数据不可用,跳过处理");
|
||||
@@ -109,6 +90,7 @@ export const useProfiles = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否有saved的代理选择
|
||||
const { selected = [] } = current;
|
||||
if (selected.length === 0) {
|
||||
console.log("[ActivateSelected] 当前profile无保存的代理选择,跳过");
|
||||
@@ -116,7 +98,7 @@ export const useProfiles = () => {
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[ActivateSelected] 当前profile有${selected.length} 个代理选择配置`,
|
||||
`[ActivateSelected] 当前profile有 ${selected.length} 个代理选择配置`,
|
||||
);
|
||||
|
||||
const selectedMap = Object.fromEntries(
|
||||
@@ -133,6 +115,7 @@ export const useProfiles = () => {
|
||||
"LoadBalance",
|
||||
]);
|
||||
|
||||
// 处理所有代理组
|
||||
[global, ...groups].forEach((group) => {
|
||||
if (!group) {
|
||||
return;
|
||||
@@ -167,7 +150,7 @@ export const useProfiles = () => {
|
||||
|
||||
if (!existsInGroup) {
|
||||
console.warn(
|
||||
`[ActivateSelected] 保存的代理${savedProxy} 不存在于代理组${name}`,
|
||||
`[ActivateSelected] 保存的代理 ${savedProxy} 不存在于代理组 ${name}`,
|
||||
);
|
||||
hasChange = true;
|
||||
newSelected.push({ name, now: now ?? savedProxy });
|
||||
@@ -190,7 +173,7 @@ export const useProfiles = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("[ActivateSelected] 完成代理切换,保存新的选择配置");
|
||||
console.log(`[ActivateSelected] 完成代理切换,保存新的选择配置`);
|
||||
|
||||
try {
|
||||
await patchProfile(profileData.current!, { selected: newSelected });
|
||||
@@ -212,18 +195,14 @@ export const useProfiles = () => {
|
||||
|
||||
return {
|
||||
profiles,
|
||||
hasProfiles,
|
||||
current: hasProfiles
|
||||
? (profiles.items?.find((p) => p && p.uid === profiles.current) ?? null)
|
||||
: null,
|
||||
current: profiles?.items?.find((p) => p && p.uid === profiles.current),
|
||||
activateSelected,
|
||||
patchProfiles,
|
||||
patchCurrent,
|
||||
mutateProfiles,
|
||||
isLoading: isValidating || storeHydrating,
|
||||
isHydrating: storeHydrating,
|
||||
lastResult,
|
||||
// 新增故障检测状态
|
||||
isLoading: isValidating,
|
||||
error,
|
||||
isStale: !hasProfiles && !error && !isValidating,
|
||||
isStale: !profiles && !error && !isValidating, // 检测是否处于异常状态
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user