mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
fix: use selected proxy after profile changed
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import useSWR from "swr";
|
||||
import useSWR, { mutate } from "swr";
|
||||
import {
|
||||
getProfiles,
|
||||
patchProfile,
|
||||
patchProfilesConfig,
|
||||
} from "@/services/cmds";
|
||||
import { getProxies, updateProxy } from "@/services/api";
|
||||
|
||||
export const useProfiles = () => {
|
||||
const { data: profiles, mutate: mutateProfiles } = useSWR(
|
||||
@@ -23,9 +24,54 @@ export const useProfiles = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// 根据selected的节点选择
|
||||
const activateSelected = async () => {
|
||||
const proxiesData = await getProxies();
|
||||
const profileData = await getProfiles();
|
||||
|
||||
if (!profileData || !proxiesData) return;
|
||||
|
||||
const current = profileData.items?.find(
|
||||
(e) => e.uid === profileData.current
|
||||
);
|
||||
|
||||
if (!current) return;
|
||||
|
||||
// init selected array
|
||||
const { selected = [] } = current;
|
||||
const selectedMap = Object.fromEntries(
|
||||
selected.map((each) => [each.name!, each.now!])
|
||||
);
|
||||
|
||||
let hasChange = false;
|
||||
|
||||
const newSelected: typeof selected = [];
|
||||
const { global, groups } = proxiesData;
|
||||
|
||||
[global, ...groups].forEach(({ type, name, now }) => {
|
||||
if (!now || (type !== "Selector" && type !== "Fallback")) return;
|
||||
if (selectedMap[name] != null && selectedMap[name] !== now) {
|
||||
hasChange = true;
|
||||
updateProxy(name, selectedMap[name]);
|
||||
console.log({
|
||||
name,
|
||||
now,
|
||||
select: selectedMap[name],
|
||||
});
|
||||
}
|
||||
newSelected.push({ name, now: selectedMap[name] });
|
||||
});
|
||||
|
||||
if (hasChange) {
|
||||
patchProfile(profileData.current!, { selected: newSelected });
|
||||
mutate("getProxies", getProxies());
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
profiles,
|
||||
current: profiles?.items?.find((p) => p.uid === profiles.current),
|
||||
activateSelected,
|
||||
patchProfiles,
|
||||
patchCurrent,
|
||||
mutateProfiles,
|
||||
|
||||
Reference in New Issue
Block a user