Fix getDelayFix hook issue

This commit is contained in:
Ahao
2025-05-20 18:15:05 +08:00
parent b70e202201
commit 5983ac5449
2 changed files with 50 additions and 38 deletions

View File

@@ -24,6 +24,7 @@
- 同步更新多语言翻译 - 同步更新多语言翻译
- 修复 .window-state.json 无法删除的问题 - 修复 .window-state.json 无法删除的问题
- 无法修改配置更新 HTTP 请求超时 - 无法修改配置更新 HTTP 请求超时
- 修复 getDelayFix 钩子问题
#### 新增了: #### 新增了:
- Mihomo(Meta)内核升级至 1.19.8 - Mihomo(Meta)内核升级至 1.19.8

View File

@@ -128,7 +128,7 @@ export const CurrentProxyCard = () => {
groups: [], groups: [],
records: {}, records: {},
globalProxy: "", globalProxy: "",
directProxy: null, directProxy: { name: "DIRECT" }, // 默认值避免 undefined
}, },
selection: { selection: {
group: "", group: "",
@@ -213,7 +213,7 @@ export const CurrentProxyCard = () => {
if (isDirectMode) { if (isDirectMode) {
newGroup = "DIRECT"; newGroup = "DIRECT";
newProxy = "DIRECT"; newProxy = "DIRECT";
newDisplayProxy = proxies.records?.DIRECT || null; newDisplayProxy = proxies.records?.DIRECT || { name: "DIRECT" }; // 确保非空
} else if (isGlobalMode && proxies.global) { } else if (isGlobalMode && proxies.global) {
newGroup = "GLOBAL"; newGroup = "GLOBAL";
newProxy = proxies.global.now || ""; newProxy = proxies.global.now || "";
@@ -248,7 +248,7 @@ export const CurrentProxyCard = () => {
groups: filteredGroups, groups: filteredGroups,
records: proxies.records || {}, records: proxies.records || {},
globalProxy: proxies.global?.now || "", globalProxy: proxies.global?.now || "",
directProxy: proxies.records?.DIRECT || null, directProxy: proxies.records?.DIRECT || { name: "DIRECT" },
}, },
selection: { selection: {
group: newGroup, group: newGroup,
@@ -364,12 +364,15 @@ export const CurrentProxyCard = () => {
return state.displayProxy; return state.displayProxy;
}, [state.displayProxy]); }, [state.displayProxy]);
// 获取当前节点的延迟 // 获取当前节点的延迟(增加非空校验)
const currentDelay = currentProxy const currentDelay = currentProxy && state.selection.group
? delayManager.getDelayFix(currentProxy, state.selection.group) ? delayManager.getDelayFix(currentProxy, state.selection.group)
: -1; : -1;
const signalInfo = getSignalIcon(currentDelay); // 信号图标(增加非空校验)
const signalInfo = currentProxy && state.selection.group
? getSignalIcon(currentDelay)
: { icon: <SignalNone />, text: "未初始化", color: "text.secondary" };
// 自定义渲染选择框中的值 // 自定义渲染选择框中的值
const renderProxyValue = useCallback( const renderProxyValue = useCallback(
@@ -378,7 +381,7 @@ export const CurrentProxyCard = () => {
const delayValue = delayManager.getDelayFix( const delayValue = delayManager.getDelayFix(
state.proxyData.records[selected], state.proxyData.records[selected],
state.selection.group, state.selection.group
); );
return ( return (
@@ -402,23 +405,27 @@ export const CurrentProxyCard = () => {
localStorage.setItem(STORAGE_KEY_SORT_TYPE, newSortType.toString()); localStorage.setItem(STORAGE_KEY_SORT_TYPE, newSortType.toString());
}, [sortType]); }, [sortType]);
// 排序代理函数 // 排序代理函数(增加非空校验)
const sortProxies = useCallback( const sortProxies = useCallback(
(proxies: ProxyOption[]) => { (proxies: ProxyOption[]) => {
if (!proxies || sortType === 0) return proxies; if (!proxies || sortType === 0) return proxies;
// 确保数据存在
if (!state.proxyData.records || !state.selection.group) return proxies;
const list = [...proxies]; const list = [...proxies];
if (sortType === 1) { if (sortType === 1) {
list.sort((a, b) => { list.sort((a, b) => {
const ad = delayManager.getDelayFix( const recordA = state.proxyData.records[a.name];
state.proxyData.records[a.name], const recordB = state.proxyData.records[b.name];
state.selection.group
); // 处理 record 不存在的情况
const bd = delayManager.getDelayFix( if (!recordA) return 1;
state.proxyData.records[b.name], if (!recordB) return -1;
state.selection.group
); const ad = delayManager.getDelayFix(recordA, state.selection.group);
const bd = delayManager.getDelayFix(recordB, state.selection.group);
if (ad === -1 || ad === -2) return 1; if (ad === -1 || ad === -2) return 1;
if (bd === -1 || bd === -2) return -1; if (bd === -1 || bd === -2) return -1;
@@ -431,10 +438,10 @@ export const CurrentProxyCard = () => {
return list; return list;
}, },
[sortType, state?.proxyData?.records, state?.selection?.group] [sortType, state.proxyData.records, state.selection.group]
); );
// 计算要显示的代理选项 // 计算要显示的代理选项(增加非空校验)
const proxyOptions = useMemo(() => { const proxyOptions = useMemo(() => {
if (isDirectMode) { if (isDirectMode) {
return [{ name: "DIRECT" }]; return [{ name: "DIRECT" }];
@@ -453,13 +460,15 @@ export const CurrentProxyCard = () => {
} }
// 规则模式 // 规则模式
const group = state.proxyData.groups.find( const group = state.selection.group
(g: { name: string }) => g.name === state.selection.group, ? state.proxyData.groups.find(g => g.name === state.selection.group)
); : null;
if (group) { if (group) {
const options = group.all.map((name) => ({ name })); const options = group.all.map((name) => ({ name }));
return sortProxies(options); return sortProxies(options);
} }
return []; return [];
}, [isDirectMode, isGlobalMode, proxies, state.proxyData, state.selection.group, sortProxies]); }, [isDirectMode, isGlobalMode, proxies, state.proxyData, state.selection.group, sortProxies]);
@@ -648,10 +657,12 @@ export const CurrentProxyCard = () => {
{isDirectMode {isDirectMode
? null ? null
: proxyOptions.map((proxy, index) => { : proxyOptions.map((proxy, index) => {
const delayValue = delayManager.getDelayFix( const delayValue = state.proxyData.records[proxy.name] && state.selection.group
? delayManager.getDelayFix(
state.proxyData.records[proxy.name], state.proxyData.records[proxy.name],
state.selection.group, state.selection.group,
); )
: -1;
return ( return (
<MenuItem <MenuItem
key={`${proxy.name}-${index}`} key={`${proxy.name}-${index}`}