From 4acf16893f4ae542b1d02f9209349df6cc693f14 Mon Sep 17 00:00:00 2001 From: mango <1711456624@qq.com> Date: Fri, 7 Aug 2026 21:58:53 +0800 Subject: [PATCH] fix(query): preserve selected new query context --- frontend/src/App.tsx | 12 ++++++------ frontend/src/utils/newQueryContext.test.ts | 18 +++++++++++++++++- frontend/src/utils/newQueryContext.ts | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 9a234300..1d3ba3b3 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -238,7 +238,7 @@ import { import { useAppUpdateManager } from './hooks/useAppUpdateManager'; import { useAppLogPanelResize } from './hooks/useAppLogPanelResize'; import { useAppSidebarResize } from './hooks/useAppSidebarResize'; -import { resolveNewQueryContext } from './utils/newQueryContext'; +import { canInheritNewQueryTableContext, resolveNewQueryContext } from './utils/newQueryContext'; import { useAppUtilityStyles } from './hooks/useAppUtilityStyles'; import { useWorkbenchTabs } from './hooks/useWorkbenchTabs'; import { @@ -2731,11 +2731,11 @@ function App() { validConnectionIds: new Set(connections.map(connection => connection.id)), }); const connection = connections.find(c => c.id === targetContext.connectionId); - const inheritsTableContext = currentTab - && (currentTab.type === 'table' || currentTab.type === 'design') - && String(currentTab.connectionId || '').trim() === targetContext.connectionId - && String(currentTab.dbName || '').trim() === targetContext.dbName; - const tableName = inheritsTableContext ? String(currentTab.tableName || '').trim() : ''; + const inheritsTableContext = canInheritNewQueryTableContext({ + activeTab: currentTab, + targetContext, + }); + const tableName = inheritsTableContext ? String(currentTab?.tableName || '').trim() : ''; const contextualQuery = tableName && connection ? buildContextualNewQueryTemplate({ dbType: resolveDataSourceType(connection.config), diff --git a/frontend/src/utils/newQueryContext.test.ts b/frontend/src/utils/newQueryContext.test.ts index a1a188bd..bb0c8d34 100644 --- a/frontend/src/utils/newQueryContext.test.ts +++ b/frontend/src/utils/newQueryContext.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { resolveNewQueryContext } from './newQueryContext'; +import { canInheritNewQueryTableContext, resolveNewQueryContext } from './newQueryContext'; describe('resolveNewQueryContext', () => { const validConnectionIds = new Set(['conn-a', 'conn-b']); @@ -44,4 +44,20 @@ describe('resolveNewQueryContext', () => { validConnectionIds, })).toEqual({ connectionId: '', dbName: '' }); }); + + it('only inherits a table tab when it belongs to the resolved target context', () => { + const tableTab = { type: 'table', connectionId: 'conn-a', dbName: 'database_a', tableName: 'users' }; + expect(canInheritNewQueryTableContext({ + activeTab: tableTab, + targetContext: { connectionId: 'conn-a', dbName: 'database_a' }, + })).toBe(true); + expect(canInheritNewQueryTableContext({ + activeTab: tableTab, + targetContext: { connectionId: 'conn-b', dbName: 'database_b' }, + })).toBe(false); + expect(canInheritNewQueryTableContext({ + activeTab: { ...tableTab, type: 'query' }, + targetContext: { connectionId: 'conn-a', dbName: 'database_a' }, + })).toBe(false); + }); }); diff --git a/frontend/src/utils/newQueryContext.ts b/frontend/src/utils/newQueryContext.ts index 7fc7c553..05c3bf66 100644 --- a/frontend/src/utils/newQueryContext.ts +++ b/frontend/src/utils/newQueryContext.ts @@ -35,3 +35,20 @@ export const resolveNewQueryContext = ({ || normalizeValidContext(activeTab, validConnectionIds) || { connectionId: '', dbName: '' } ); +export interface NewQueryTableContextLike extends NewQueryContextLike { + type?: unknown; + tableName?: unknown; +} + +export const canInheritNewQueryTableContext = ({ + activeTab, + targetContext, +}: { + activeTab?: NewQueryTableContextLike | null; + targetContext: NewQueryContext; +}): boolean => { + const tabType = String(activeTab?.type || ''); + return (tabType === 'table' || tabType === 'design') + && String(activeTab?.connectionId || '').trim() === targetContext.connectionId + && String(activeTab?.dbName || '').trim() === targetContext.dbName; +};