fix(query): preserve selected new query context

This commit is contained in:
mango
2026-08-07 21:58:53 +08:00
parent 0e2a28039e
commit 4acf16893f
3 changed files with 40 additions and 7 deletions

View File

@@ -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),

View File

@@ -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);
});
});

View File

@@ -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;
};