fix: node selection for remembered profiles during switch (#6865)

* fix: node selection for remembered profiles during switch

* remove redundant comments
This commit is contained in:
StarsSail
2026-05-26 06:20:02 +00:00
committed by GitHub
parent 28139706d2
commit 512efe7d41
+21 -42
View File
@@ -37,7 +37,10 @@ import {
} from 'react'
import { useTranslation } from 'react-i18next'
import { useLocation } from 'react-router'
import { closeAllConnections } from 'tauri-plugin-mihomo-api'
import {
closeAllConnections,
selectNodeForGroup,
} from 'tauri-plugin-mihomo-api'
import { BasePage, BaseStyledTextField, DialogRef } from '@/components/base'
import { ProfileItem } from '@/components/profile/profile-item'
@@ -50,6 +53,7 @@ import { ConfigViewer } from '@/components/setting/mods/config-viewer'
import { useListen } from '@/hooks/use-listen'
import { useProfiles } from '@/hooks/use-profiles'
import {
calcuProxies,
createProfile,
deleteProfile,
enhanceProfiles,
@@ -178,7 +182,6 @@ const ProfilePage = () => {
const {
profiles = {},
activateSelected,
patchProfiles,
mutateProfiles,
error,
@@ -389,34 +392,6 @@ const ProfilePage = () => {
}
}
const executeBackgroundTasks = useCallback(
async (
profile: string,
sequence: number,
abortController: AbortController,
) => {
try {
if (
sequence === requestSequenceRef.current &&
switchingProfileRef.current === profile &&
!abortController.signal.aborted
) {
await activateSelected(profiles)
debugLog(`[Profile] 后台处理完成,序列号: ${sequence}`)
} else {
debugProfileSwitch(
'BACKGROUND_TASK_SKIPPED',
profile,
`序列号过期或被中断: ${sequence} vs ${requestSequenceRef.current}`,
)
}
} catch (err: any) {
console.warn('Failed to activate selected proxies:', err)
}
},
[activateSelected, profiles],
)
const activateProfile = useCallback(
async (profile: string, notifySuccess: boolean) => {
if (profiles.current === profile && !notifySuccess) {
@@ -486,6 +461,22 @@ const ProfilePage = () => {
return
}
// 选择所记忆的节点
const current = profiles.items?.find((e) => e.uid === profile)
for (const item of current?.selected ?? []) {
if (item.name && item.now) {
try {
await selectNodeForGroup(item.name, item.now)
} catch (err) {
debugLog(
`[Profile] 选择节点失败: ${item.name} -> ${item.now}`,
err,
)
}
}
}
queryClient.setQueryData(['getProxies'], await calcuProxies())
// 完成切换
await mutateLogs()
closeAllConnections()
@@ -500,17 +491,6 @@ const ProfilePage = () => {
debugLog(
`[Profile] 切换到 ${profile} 完成,序列号: ${currentSequence},开始后台处理`,
)
// 延迟执行后台任务
setTimeout(
() =>
executeBackgroundTasks(
profile,
currentSequence,
currentAbortController,
),
50,
)
} catch (err: any) {
if (pendingRequestRef.current) {
pendingRequestRef.current = null
@@ -546,7 +526,6 @@ const ProfilePage = () => {
profiles,
patchProfiles,
mutateLogs,
executeBackgroundTasks,
handleProfileInterrupt,
cleanupSwitchState,
],