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

@@ -101,13 +101,13 @@ export const CurrentProxyCard = () => {
const mode = clashConfig?.mode?.toLowerCase() || "rule"; const mode = clashConfig?.mode?.toLowerCase() || "rule";
const isGlobalMode = mode === "global"; const isGlobalMode = mode === "global";
const isDirectMode = mode === "direct"; const isDirectMode = mode === "direct";
// 添加排序类型状态 // 添加排序类型状态
const [sortType, setSortType] = useState<ProxySortType>(() => { const [sortType, setSortType] = useState<ProxySortType>(() => {
const savedSortType = localStorage.getItem(STORAGE_KEY_SORT_TYPE); const savedSortType = localStorage.getItem(STORAGE_KEY_SORT_TYPE);
return savedSortType ? Number(savedSortType) as ProxySortType : 0; return savedSortType ? Number(savedSortType) as ProxySortType : 0;
}); });
// 定义状态类型 // 定义状态类型
type ProxyState = { type ProxyState = {
proxyData: { proxyData: {
@@ -128,7 +128,7 @@ export const CurrentProxyCard = () => {
groups: [], groups: [],
records: {}, records: {},
globalProxy: "", globalProxy: "",
directProxy: null, directProxy: { name: "DIRECT" }, // 默认值避免 undefined
}, },
selection: { selection: {
group: "", group: "",
@@ -160,9 +160,9 @@ export const CurrentProxyCard = () => {
return primaryGroup?.name || ""; return primaryGroup?.name || "";
}; };
const primaryGroupName = getPrimaryGroupName(); const primaryGroupName = getPrimaryGroupName();
// 根据模式确定初始组 // 根据模式确定初始组
if (isGlobalMode) { if (isGlobalMode) {
setState((prev) => ({ setState((prev) => ({
@@ -204,7 +204,7 @@ export const CurrentProxyCard = () => {
now: g.now || "", now: g.now || "",
all: g.all.map((p: { name: string }) => p.name), all: g.all.map((p: { name: string }) => p.name),
})); }));
let newProxy = ""; let newProxy = "";
let newDisplayProxy = null; let newDisplayProxy = null;
let newGroup = prev.selection.group; let newGroup = prev.selection.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;
@@ -428,13 +435,13 @@ export const CurrentProxyCard = () => {
} else { } else {
list.sort((a, b) => a.name.localeCompare(b.name)); list.sort((a, b) => a.name.localeCompare(b.name));
} }
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" }];
@@ -445,21 +452,23 @@ export const CurrentProxyCard = () => {
const name = typeof p === 'string' ? p : p.name; const name = typeof p === 'string' ? p : p.name;
return name !== "DIRECT" && name !== "REJECT"; return name !== "DIRECT" && name !== "REJECT";
}) })
.map((p: any) => ({ .map((p: any) => ({
name: typeof p === 'string' ? p : p.name name: typeof p === 'string' ? p : p.name
})); }));
return sortProxies(options); return sortProxies(options);
} }
// 规则模式 // 规则模式
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]);
@@ -509,8 +518,8 @@ export const CurrentProxyCard = () => {
action={ action={
<Box sx={{ display: "flex", alignItems: "center" }}> <Box sx={{ display: "flex", alignItems: "center" }}>
<Tooltip title={getSortTooltip()}> <Tooltip title={getSortTooltip()}>
<IconButton <IconButton
size="small" size="small"
color="inherit" color="inherit"
onClick={handleSortTypeChange} onClick={handleSortTypeChange}
sx={{ mr: 1 }} sx={{ mr: 1 }}
@@ -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
state.proxyData.records[proxy.name], ? delayManager.getDelayFix(
state.selection.group, state.proxyData.records[proxy.name],
); state.selection.group,
)
: -1;
return ( return (
<MenuItem <MenuItem
key={`${proxy.name}-${index}`} key={`${proxy.name}-${index}`}
@@ -692,4 +703,4 @@ export const CurrentProxyCard = () => {
)} )}
</EnhancedCard> </EnhancedCard>
); );
}; };