fix: unify homepage node selection

This commit is contained in:
wonfen
2025-09-01 11:30:27 +08:00
parent 1b54c9fc1b
commit 0ea875f7f7
4 changed files with 179 additions and 122 deletions

View File

@@ -29,14 +29,9 @@ import {
} from "@mui/icons-material";
import { useNavigate } from "react-router-dom";
import { EnhancedCard } from "@/components/home/enhanced-card";
import {
updateProxy,
deleteConnection,
syncTrayProxySelection,
} from "@/services/cmds";
import delayManager from "@/services/delay";
import { useVerge } from "@/hooks/use-verge";
import { useAppData } from "@/providers/app-data-provider";
import { useProxySelection } from "@/hooks/use-proxy-selection";
// 本地存储的键名
const STORAGE_KEY_GROUP = "clash-verge-selected-proxy-group";
@@ -98,8 +93,18 @@ export const CurrentProxyCard = () => {
const { t } = useTranslation();
const navigate = useNavigate();
const theme = useTheme();
const { verge } = useVerge();
const { proxies, connections, clashConfig, refreshProxy } = useAppData();
const { proxies, clashConfig, refreshProxy } = useAppData();
// 统一代理选择器
const { handleSelectChange } = useProxySelection({
onSuccess: () => {
refreshProxy();
},
onError: (error) => {
console.error("代理切换失败", error);
refreshProxy();
},
});
// 判断模式
const mode = clashConfig?.mode?.toLowerCase() || "rule";
@@ -117,8 +122,6 @@ export const CurrentProxyCard = () => {
proxyData: {
groups: { name: string; now: string; all: string[] }[];
records: Record<string, any>;
globalProxy: string;
directProxy: any;
};
selection: {
group: string;
@@ -131,8 +134,6 @@ export const CurrentProxyCard = () => {
proxyData: {
groups: [],
records: {},
globalProxy: "",
directProxy: { name: "DIRECT" }, // 默认值避免 undefined
},
selection: {
group: "",
@@ -257,8 +258,6 @@ export const CurrentProxyCard = () => {
proxyData: {
groups: filteredGroups,
records: proxies.records || {},
globalProxy: proxies.global?.now || "",
directProxy: proxies.records?.DIRECT || { name: "DIRECT" },
},
selection: {
group: newGroup,
@@ -314,7 +313,7 @@ export const CurrentProxyCard = () => {
// 处理代理节点变更
const handleProxyChange = useCallback(
async (event: SelectChangeEvent) => {
(event: SelectChangeEvent) => {
if (isDirectMode) return;
const newProxy = event.target.value;
@@ -334,42 +333,15 @@ export const CurrentProxyCard = () => {
localStorage.setItem(STORAGE_KEY_PROXY, newProxy);
}
try {
await updateProxy(currentGroup, newProxy);
// 自动关闭连接设置
if (verge?.auto_close_connection && previousProxy) {
connections.data.forEach((conn: any) => {
if (conn.chains.includes(previousProxy)) {
deleteConnection(conn.id);
}
});
}
// 同步托盘菜单状态
try {
await syncTrayProxySelection();
} catch (syncError) {
console.warn("Failed to sync tray proxy selection:", syncError);
}
// 延长刷新延迟时间
setTimeout(() => {
refreshProxy();
}, 500);
} catch (error) {
console.error("更新代理失败", error);
}
const skipConfigSave = isGlobalMode || isDirectMode;
handleSelectChange(currentGroup, previousProxy, skipConfigSave)(event);
},
[
isDirectMode,
isGlobalMode,
state.proxyData.records,
state.selection,
verge?.auto_close_connection,
refreshProxy,
debouncedSetState,
connections.data,
handleSelectChange,
],
);