🐛 fix(sidebar): 顶部新建查询绑定选中数据库

Fixes #665
This commit is contained in:
Syngnat
2026-07-17 22:40:36 +08:00
parent e7857bb2e4
commit e24942083a
4 changed files with 80 additions and 5 deletions

View File

@@ -1314,7 +1314,7 @@ describe('Sidebar locate toolbar', () => {
expect(css).not.toContain('.gn-v2-active-connection-trigger:hover');
});
it('shows a prominent v2 new query action before connection creation without reusing the plus icon', () => {
it('opens the v2 header new-query action in the selected database before connection creation', () => {
mocks.state.connections = [{
id: 'conn-local',
name: '开发240',
@@ -1349,8 +1349,11 @@ describe('Sidebar locate toolbar', () => {
expect(markup.indexOf('data-gonavi-new-query-action="true"')).toBeLessThan(markup.indexOf('data-gonavi-create-connection-action="true"'));
expect(newQueryActionSource).toContain('icon={<FileTextOutlined />}');
expect(newQueryActionSource).not.toContain('icon={<PlusOutlined />}');
expect(newQueryActionSource).toContain("handleV2ConnectionContextMenuAction(getConnectionNodeForAction(activeConnection), 'new-query')");
expect(newQueryActionSource).not.toContain("handleV2ConnectionContextMenuAction(activeConnection, 'new-query')");
expect(newQueryActionSource).toContain('const selectedDatabase = resolveV2SelectedDatabaseName({');
expect(newQueryActionSource).toContain('activeConnectionId: activeConnection.id,');
expect(newQueryActionSource).toContain('activeContextConnectionId: activeContext?.connectionId,');
expect(newQueryActionSource).toContain("handleV2DatabaseContextMenuAction(getDatabaseNodeRef(activeConnection, selectedDatabase), 'new-query');");
expect(newQueryActionSource).toContain("handleV2ConnectionContextMenuAction(getConnectionNodeForAction(activeConnection), 'new-query');");
expect(newQueryActionSource).toContain('disabled={!activeConnection}');
});

View File

@@ -189,6 +189,7 @@ import {
resolveSidebarDatabaseTreePruneKeys,
resolveSidebarNodeConnectionId,
resolveV2ActiveConnectionId,
resolveV2SelectedDatabaseName,
resolveV2CommandSearchPersistentFilter,
shouldClearSidebarNodeChildrenOnCollapse,
shouldSkipSidebarLoadOnExpandWhileDragging,
@@ -2031,6 +2032,7 @@ const Sidebar: React.FC<{
const getDatabaseNodeRef = (connRef: any, dbName: string) => {
const latestConn = connections.find(c => c.id === connRef.id);
return {
title: dbName,
key: `${connRef.id}-${dbName}`,
dataRef: { ...(latestConn || connRef), dbName }
};
@@ -3004,9 +3006,19 @@ const Sidebar: React.FC<{
data-gonavi-new-query-action="true"
disabled={!activeConnection}
onClick={() => {
if (activeConnection) {
handleV2ConnectionContextMenuAction(getConnectionNodeForAction(activeConnection), 'new-query');
if (!activeConnection) {
return;
}
const selectedDatabase = resolveV2SelectedDatabaseName({
activeConnectionId: activeConnection.id,
activeContextConnectionId: activeContext?.connectionId,
activeContextDbName: activeContext?.dbName,
});
if (selectedDatabase) {
handleV2DatabaseContextMenuAction(getDatabaseNodeRef(activeConnection, selectedDatabase), 'new-query');
return;
}
handleV2ConnectionContextMenuAction(getConnectionNodeForAction(activeConnection), 'new-query');
}}
>
{t('sidebar.menu.new_query')}

View File

@@ -0,0 +1,43 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { resolveV2SelectedDatabaseName } from './sidebarV2Utils';
describe('resolveV2SelectedDatabaseName', () => {
it('keeps a selected database only when it belongs to the active connection', () => {
expect(resolveV2SelectedDatabaseName({
activeConnectionId: 'conn-local',
activeContextConnectionId: 'conn-local',
activeContextDbName: 'reporting',
})).toBe('reporting');
expect(resolveV2SelectedDatabaseName({
activeConnectionId: 'conn-local',
activeContextConnectionId: 'conn-other',
activeContextDbName: 'analytics',
})).toBe('');
});
it('does not bind a connection-level query to an empty selected database', () => {
expect(resolveV2SelectedDatabaseName({
activeConnectionId: 'conn-local',
activeContextConnectionId: 'conn-local',
activeContextDbName: ' ',
})).toBe('');
});
it('uses the selected-database resolver before falling back to the connection action', () => {
const sidebarSource = readFileSync(new URL('./Sidebar.tsx', import.meta.url), 'utf8');
const databaseNodeStart = sidebarSource.indexOf('const getDatabaseNodeRef = (connRef: any, dbName: string) => {');
const databaseNodeEnd = sidebarSource.indexOf('const extractObjectName =', databaseNodeStart);
const headerStart = sidebarSource.indexOf('<div className="gn-v2-active-connection-actions">');
const headerEnd = sidebarSource.indexOf('<Tooltip title={v2ConnectionActionsLabel}>', headerStart);
const databaseNodeSource = sidebarSource.slice(databaseNodeStart, databaseNodeEnd);
const headerSource = sidebarSource.slice(headerStart, headerEnd);
expect(databaseNodeSource).toContain('title: dbName,');
expect(headerSource).toContain('const selectedDatabase = resolveV2SelectedDatabaseName({');
expect(headerSource).toContain("handleV2DatabaseContextMenuAction(getDatabaseNodeRef(activeConnection, selectedDatabase), 'new-query');");
expect(headerSource).toContain("handleV2ConnectionContextMenuAction(getConnectionNodeForAction(activeConnection), 'new-query');");
});
});

View File

@@ -1056,6 +1056,23 @@ export const resolveV2ActiveConnectionId = ({
|| '';
};
export const resolveV2SelectedDatabaseName = ({
activeConnectionId,
activeContextConnectionId,
activeContextDbName,
}: {
activeConnectionId?: unknown;
activeContextConnectionId?: unknown;
activeContextDbName?: unknown;
}): string => {
const connectionId = String(activeConnectionId || '').trim();
const contextConnectionId = String(activeContextConnectionId || '').trim();
if (!connectionId || connectionId !== contextConnectionId) {
return '';
}
return String(activeContextDbName || '').trim();
};
export const resolveSidebarDatabaseTreePruneKeys = ({
treeData,
expandedKeys,