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

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