feat(search): limit sidebar search to opened databases

This commit is contained in:
jefferyjob authored and GitHub committed 2026-09-29 13:59:42 +08:00
1 parent e4ab63d9f3
commit e7883a6466
17 files changed
+169 -2

No files matched your search

@@ -0,0 +1,62 @@
import { describe, expect, it } from "vitest";
import type { TreeNode } from "@/types/database";
import { isSidebarSearchPrunedDatabaseNode, resolveSidebarSearchDatabaseScope } from "@/lib/sidebar/sidebarSearchDatabaseScope";
const OPTIONS = {
enabled: true,
isChildrenLoaded: (nodeId: string) => nodeId.endsWith(":loaded"),
openDatabaseKeys: new Set(["conn-1\x00tabbed"]),
};
function databaseNode(id: string, database: string, overrides: Partial<TreeNode> = {}): TreeNode {
return { id, label: database, type: "database", connectionId: "conn-1", database, isExpanded: false, children: [], ...overrides };
}
function connectionNode(children: TreeNode[]): TreeNode {
return { id: "conn-1", label: "prod", type: "connection", connectionId: "conn-1", isExpanded: true, children };
}
describe("resolveSidebarSearchDatabaseScope", () => {
it.each([
["disabled setting keeps the search unrestricted", { enabled: false }, null],
["no database opened falls back to every database", {}, null],
] as const)("returns null when %s", (_name, overrides, expected) => {
const node = connectionNode([databaseNode("conn-1:a", "a"), databaseNode("conn-1:b", "b")]);
expect(resolveSidebarSearchDatabaseScope(node, { ...OPTIONS, ...overrides })).toEqual(expected);
});
it("keeps databases opened in the tree", () => {
const node = connectionNode([databaseNode("conn-1:a:loaded", "a"), databaseNode("conn-1:b", "b")]);
expect(resolveSidebarSearchDatabaseScope(node, OPTIONS)).toEqual(new Set(["conn-1:a:loaded"]));
});
it("keeps databases referenced by an open editor tab", () => {
const node = connectionNode([databaseNode("conn-1:tabbed", "tabbed"), databaseNode("conn-1:b", "b")]);
expect(resolveSidebarSearchDatabaseScope(node, OPTIONS)).toEqual(new Set(["conn-1:tabbed"]));
});
it("discovers nested database nodes below catalogs and skips their subtrees", () => {
const nested = databaseNode("conn-1:cat:nested:loaded", "nested");
const node = connectionNode([
{ id: "conn-1:cat", label: "cat", type: "doris-catalog", connectionId: "conn-1", catalog: "cat", isExpanded: true, children: [nested] },
{ id: "conn-1:other", label: "other", type: "doris-catalog", connectionId: "conn-1", catalog: "other", isExpanded: false, children: [databaseNode("conn-1:other:deep", "deep")] },
]);
expect(resolveSidebarSearchDatabaseScope(node, OPTIONS)).toEqual(new Set(["conn-1:cat:nested:loaded"]));
});
it("treats schema-mode trees without database nodes as unrestricted", () => {
const schemaNode: TreeNode = { id: "conn-1:public", label: "public", type: "schema", connectionId: "conn-1", database: "main", isExpanded: false, children: [] };
expect(resolveSidebarSearchDatabaseScope(connectionNode([schemaNode]), OPTIONS)).toEqual(null);
});
});
describe("isSidebarSearchPrunedDatabaseNode", () => {
it.each([
["database outside the scope", databaseNode("conn-1:b", "b"), new Set(["conn-1:a"]), true],
["database inside the scope", databaseNode("conn-1:a", "a"), new Set(["conn-1:a"]), false],
["unrestricted scope", databaseNode("conn-1:b", "b"), null, false],
["non-database container", { id: "conn-1", label: "prod", type: "connection", connectionId: "conn-1", isExpanded: true, children: [] } as TreeNode, new Set(["conn-1:a"]), false],
] as const)("returns %expected when the node is a %s", (_name, node, scope, expected) => {
expect(isSidebarSearchPrunedDatabaseNode(node, scope)).toBe(expected);
});
});
@@ -89,6 +89,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"sidebarObjectDisplay",
"routineSourceOpenMode",
"sidebarTableSearchEnabled",
"sidebarSearchOpenedDatabasesOnly",
"autoSelectActiveSidebarNode",
"sidebarBrowseObjectsOnDatabaseActivation",
"openTabsRestoreMode",
@@ -201,6 +201,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
{ id: "navigation-object-display", category: "navigation", titleKey: "settings.sidebarObjectDisplay", targetId: "navigation" },
{ id: "navigation-object-info", category: "navigation", titleKey: "settings.sidebarObjectInfoMode", descriptionKey: "settings.sidebarObjectInfoModeDescription", targetId: "navigation" },
{ id: "navigation-table-search", category: "navigation", titleKey: "settings.sidebarTableSearchEnabled", descriptionKey: "settings.sidebarTableSearchEnabledDescription", targetId: "navigation" },
{ id: "navigation-search-opened-databases-only", category: "navigation", titleKey: "settings.sidebarSearchOpenedDatabasesOnly", descriptionKey: "settings.sidebarSearchOpenedDatabasesOnlyDescription", targetId: "navigation" },
{ id: "navigation-active-node", category: "navigation", titleKey: "settings.autoSelectActiveSidebarNode", descriptionKey: "settings.autoSelectActiveSidebarNodeDescription", targetId: "navigation" },
{ id: "navigation-browse-objects-on-database-activation", category: "navigation", titleKey: "settings.sidebarBrowseObjectsOnDatabaseActivation", descriptionKey: "settings.sidebarBrowseObjectsOnDatabaseActivationDescription", targetId: "navigation" },
{ id: "navigation-tabs-restore", category: "navigation", titleKey: "settings.openTabsRestoreMode", descriptionKey: "settings.openTabsRestoreModeDescription", targetId: "navigation" },
@@ -122,6 +122,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
"sidebarObjectDisplay",
"routineSourceOpenMode",
"sidebarTableSearchEnabled",
"sidebarSearchOpenedDatabasesOnly",
"autoSelectActiveSidebarNode",
"sidebarBrowseObjectsOnDatabaseActivation",
"openTabsRestoreMode",
@@ -0,0 +1,56 @@
import type { TreeNode, TreeNodeType } from "@/types/database";
import { sidebarDatabaseOpenKey } from "@/lib/sidebar/sidebarDatabaseOpenState";
// Database-level containers whose objects the sidebar search walker may load.
// Mirrors the sidebar "database opened" indicator (sidebarDatabaseOpenState):
// a database participates in the automatic search only when the user actually
// opened it — children loaded in the tree or referenced by an open editor tab.
const searchScopeDatabaseTypes = new Set<TreeNodeType>(["database", "mongo-db", "vector-database"]);
export interface SidebarSearchDatabaseScopeOptions {
enabled: boolean;
isChildrenLoaded: (nodeId: string) => boolean;
openDatabaseKeys: ReadonlySet<string>;
}
function collectOpenedDatabaseNodeIds(node: TreeNode, opened: Set<string>, options: SidebarSearchDatabaseScopeOptions): void {
for (const child of node.children ?? []) {
if (searchScopeDatabaseTypes.has(child.type)) {
const isOpened = !!child.connectionId && child.database != null && (options.isChildrenLoaded(child.id) || options.openDatabaseKeys.has(sidebarDatabaseOpenKey(child.connectionId, child.database)));
if (isOpened) opened.add(child.id);
// Nothing below a database-level node is itself database-level, so the
// subtree never contributes another scope entry.
continue;
}
// Pass-through containers (connection groups, Doris catalogs, linked
// servers) keep their nested database nodes scannable.
collectOpenedDatabaseNodeIds(child, opened, options);
}
}
/**
* Node ids of database-level nodes the sidebar search may load for one
* connection, or null when the search must stay unrestricted.
*
* null means either the setting is off, the connection exposes no
* database-level nodes (schema-mode trees such as Oracle, Redis, etc.), or
* none of them is opened — in that case falling back to searching every
* database keeps the box useful right after connecting, before the user
* opens anything.
*/
export function resolveSidebarSearchDatabaseScope(connectionNode: TreeNode, options: SidebarSearchDatabaseScopeOptions): ReadonlySet<string> | null {
if (!options.enabled) return null;
const opened = new Set<string>();
collectOpenedDatabaseNodeIds(connectionNode, opened, options);
return opened.size > 0 ? opened : null;
}
/**
* Whether the search walker must skip a node: a database-level node outside
* the opened scope. Containers and non-database nodes are never pruned, and a
* null scope (unrestricted) prunes nothing.
*/
export function isSidebarSearchPrunedDatabaseNode(node: TreeNode, scope: ReadonlySet<string> | null): boolean {
if (!scope) return false;
return searchScopeDatabaseTypes.has(node.type) && !scope.has(node.id);
}