From 77d6a1ddc9f3257d7e04ccde745fa6f13d9bb6f6 Mon Sep 17 00:00:00 2001 From: Baobhan Sith <80159437+Heavrnl@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:35:05 +0800 Subject: [PATCH] update --- .../components/WorkspaceConnectionList.vue | 9 +-- packages/frontend/src/stores/session.store.ts | 65 +++++++------------ 2 files changed, 29 insertions(+), 45 deletions(-) diff --git a/packages/frontend/src/components/WorkspaceConnectionList.vue b/packages/frontend/src/components/WorkspaceConnectionList.vue index b528d8e..8b4db72 100644 --- a/packages/frontend/src/components/WorkspaceConnectionList.vue +++ b/packages/frontend/src/components/WorkspaceConnectionList.vue @@ -5,6 +5,7 @@ import { storeToRefs } from 'pinia'; import { useI18n } from 'vue-i18n'; import { useConnectionsStore, ConnectionInfo } from '../stores/connections.store'; import { useTagsStore, TagInfo } from '../stores/tags.store'; +import { useSessionStore } from '../stores/session.store'; // 导入 session store // 定义事件 const emit = defineEmits([ @@ -18,6 +19,7 @@ const { t } = useI18n(); // const router = useRouter(); // 不再需要 const connectionsStore = useConnectionsStore(); const tagsStore = useTagsStore(); +const sessionStore = useSessionStore(); // 获取 session store 实例 const { connections, isLoading: connectionsLoading, error: connectionsError } = storeToRefs(connectionsStore); const { tags, isLoading: tagsLoading, error: tagsError } = storeToRefs(tagsStore); @@ -108,11 +110,10 @@ const toggleGroup = (groupName: string) => { expandedGroups.value[groupName] = !expandedGroups.value[groupName]; }; -// 处理单击连接 (左键) +// 处理单击连接 (左键) - 使用 session store 处理连接请求 const handleConnect = (connectionId: number) => { - console.log(`[WkspConnList] handleConnect (左键) called for ID: ${connectionId}`); - emit('connect-request', connectionId); - console.log(`[WkspConnList] Emitted 'connect-request' for ID: ${connectionId}`); + console.log(`[WkspConnList] handleConnect (左键) called for ID: ${connectionId}. Delegating to sessionStore.`); + sessionStore.handleConnectRequest(connectionId); // 调用 session store 的 action closeContextMenu(); // 点击连接后关闭菜单 }; diff --git a/packages/frontend/src/stores/session.store.ts b/packages/frontend/src/stores/session.store.ts index 54d6b0c..16a8378 100644 --- a/packages/frontend/src/stores/session.store.ts +++ b/packages/frontend/src/stores/session.store.ts @@ -322,55 +322,38 @@ export const useSessionStore = defineStore('session', () => { }; /** - * 处理连接列表的左键点击(如果点击的是当前活动标签且断开则重连,否则总是新建标签) + * 处理连接列表的左键点击 + * 优先级 1: 如果点击的是当前活动标签且断开,则重连该标签。 + * 优先级 2: 其他所有情况(非活动标签、活动且已连接标签、新连接),总是打开新标签。 */ const handleConnectRequest = (connectionId: number | string) => { const connIdStr = String(connectionId); console.log(`[SessionStore] handleConnectRequest called for ID: ${connIdStr}`); - let existingSession: SessionState | null = null; - let existingSessionId: string | null = null; - // 查找是否存在对应 connectionId 的会话 - for (const [sessionId, session] of sessions.value.entries()) { - if (session.connectionId === connIdStr) { - existingSession = session; - existingSessionId = sessionId; - break; + let activeAndDisconnected = false; // 标记是否满足最高优先级条件 + + // 检查是否点击了当前活动且断开的会话 + if (activeSessionId.value) { + const currentActiveSession = sessions.value.get(activeSessionId.value); + if (currentActiveSession && currentActiveSession.connectionId === connIdStr) { + const currentStatus = currentActiveSession.wsManager.connectionStatus.value; + console.log(`[SessionStore] 点击的是当前活动会话 ${activeSessionId.value},状态: ${currentStatus}`); + if (currentStatus === 'disconnected' || currentStatus === 'error') { + activeAndDisconnected = true; + // 满足最高优先级:重连当前活动会话 + console.log(`[SessionStore] 活动会话 ${activeSessionId.value} 已断开或出错,尝试重连...`); + const wsUrl = `ws://${window.location.hostname}:3001`; // TODO: 从配置获取 URL + currentActiveSession.wsManager.connect(wsUrl); + } } } - // 检查点击的连接是否是当前活动的标签页 - if (existingSession && existingSessionId && existingSessionId === activeSessionId.value) { - // 是当前活动标签页 - const currentStatus = existingSession.wsManager.connectionStatus.value; - console.log(`[SessionStore] 点击的是当前活动会话 ${existingSessionId},状态: ${currentStatus}`); - if (currentStatus === 'disconnected' || currentStatus === 'error') { - // 如果已断开或出错,则尝试重连 - console.log(`[SessionStore] 活动会话 ${existingSessionId} 已断开或出错,尝试重连...`); - const wsUrl = `ws://${window.location.hostname}:3001`; // TODO: 从配置获取 URL - existingSession.wsManager.connect(wsUrl); - // 不需要再调用 activateSession,因为它已经是活动的 - } else { - // 如果状态正常,则无需操作 - console.log(`[SessionStore] 活动会话 ${existingSessionId} 状态正常,无需操作。`); - } - } else if (existingSessionId && existingSession) { - // 点击的是一个已存在但非活动的会话 - console.log(`[SessionStore] 点击的连接 ${connIdStr} 存在于非活动会话 ${existingSessionId} 中,将激活它。`); - activateSession(existingSessionId); - // 激活后检查状态并尝试重连 (如果需要) - const currentStatus = existingSession.wsManager.connectionStatus.value; - if (currentStatus === 'disconnected' || currentStatus === 'error') { - console.log(`[SessionStore] 激活的会话 ${existingSessionId} 已断开或出错,尝试重连...`); - const wsUrl = `ws://${window.location.hostname}:3001`; // TODO: 从配置获取 URL - existingSession.wsManager.connect(wsUrl); - } - } else { - // 点击的连接没有对应的会话,创建新会话 - console.log(`[SessionStore] 未找到 ID 为 ${connIdStr} 的现有会话,将打开新会话。`); - openNewSession(connIdStr); - } - }; + // 如果不满足最高优先级条件,则总是打开新会话 + if (!activeAndDisconnected) { + console.log(`[SessionStore] 不满足重连条件或点击了其他连接,将打开新会话 for ID: ${connIdStr}`); + openNewSession(connIdStr); + } + }; /** * 处理连接列表的中键点击(总是打开新会话)