From 512efe7d411bcf2ad274417125d3c42a284b96a7 Mon Sep 17 00:00:00 2001 From: StarsSail <152023695+wysha-object@users.noreply.github.com> Date: Tue, 26 May 2026 14:20:02 +0800 Subject: [PATCH] fix: node selection for remembered profiles during switch (#6865) * fix: node selection for remembered profiles during switch * remove redundant comments --- src/pages/profiles.tsx | 63 ++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/src/pages/profiles.tsx b/src/pages/profiles.tsx index 922b305bb..b7dc18406 100644 --- a/src/pages/profiles.tsx +++ b/src/pages/profiles.tsx @@ -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, ],