fix(sidebar): search connections by host and username

This commit is contained in:
大熊 authored and GitHub committed 2026-08-13 15:24:30 +08:00
1 parent 18d581137c
commit b5a8864220
6 files changed
+46 -7

No files matched your search

@@ -121,11 +121,16 @@ export function remapSidebarLayoutConnectionIds(layout: SidebarLayout, connectio
}; };
} }
export function connectionSidebarSearchAliases(config: Pick<ConnectionConfig, "host" | "username">): string[] {
return [config.host, config.username].filter((value) => value.trim().length > 0);
}
function makeConnectionNode(config: ConnectionConfig, pinned: boolean): TreeNode { function makeConnectionNode(config: ConnectionConfig, pinned: boolean): TreeNode {
return { return {
id: config.id, id: config.id,
label: config.name, label: config.name,
type: "connection", type: "connection",
searchAliases: connectionSidebarSearchAliases(config),
connectionId: config.id, connectionId: config.id,
isExpanded: false, isExpanded: false,
children: [], children: [],
@@ -4,12 +4,14 @@ import { createSidebarLabelMatcher, type SidebarLabelMatcher } from "@/lib/sideb
const preserveMatchedSubtreeTypes = new Set(["connection", "database", "schema", "table", "view", "mongo-db", "mongo-collection"]); const preserveMatchedSubtreeTypes = new Set(["connection", "database", "schema", "table", "view", "mongo-db", "mongo-collection"]);
const hiddenSearchNodeTypes = new Set<TreeNodeType>(["user-admin", "dameng-job-admin"]); const hiddenSearchNodeTypes = new Set<TreeNodeType>(["user-admin", "dameng-job-admin"]);
function bestMatch(matchLabel: SidebarLabelMatcher, label: string, comment?: string | null) { function bestMatch(matchLabel: SidebarLabelMatcher, label: string, comment?: string | null, aliases?: readonly string[]) {
const lm = matchLabel(label); let best = matchLabel(label);
if (!comment) return lm; for (const candidate of [comment, ...(aliases ?? [])]) {
const cm = matchLabel(comment); if (!candidate) continue;
if (lm && cm) return lm.score >= cm.score ? lm : cm; const match = matchLabel(candidate);
return lm ?? cm; if (match && (!best || match.score > best.score)) best = match;
}
return best;
} }
function normalizedLabel(node: TreeNode): string { function normalizedLabel(node: TreeNode): string {
@@ -67,7 +69,7 @@ function filterSidebarTreeWithMatcher(nodes: TreeNode[], matchLabel: SidebarLabe
const label = normalizedLabel(node); const label = normalizedLabel(node);
const canSelfMatch = !searchableNodeTypes || searchableNodeTypes.has(node.type); const canSelfMatch = !searchableNodeTypes || searchableNodeTypes.has(node.type);
const selfMatch = canSelfMatch ? (matchLabel ? bestMatch(matchLabel, label, node.comment) : { score: 0 }) : null; const selfMatch = canSelfMatch ? (matchLabel ? bestMatch(matchLabel, label, node.comment, node.searchAliases) : { score: 0 }) : null;
// Type-only filtering keeps matching rows and their ancestor path, but not // Type-only filtering keeps matching rows and their ancestor path, but not
// unrelated descendants that would make the selected type appear ignored. // unrelated descendants that would make the selected type appear ignored.
const preservesSubtree = !!matchLabel && !!selfMatch && preserveMatchedSubtreeTypes.has(node.type); const preservesSubtree = !!matchLabel && !!selfMatch && preserveMatchedSubtreeTypes.has(node.type);
@@ -55,6 +55,7 @@ import {
remapSidebarLayoutConnectionIds, remapSidebarLayoutConnectionIds,
reorderEntry as reorderEntryOp, reorderEntry as reorderEntryOp,
buildConnectionGroupPathMap, buildConnectionGroupPathMap,
connectionSidebarSearchAliases,
type DropPosition, type DropPosition,
} from "@/lib/sidebar/sidebarLayout"; } from "@/lib/sidebar/sidebarLayout";
import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionObject, SqlCompletionTable } from "@/lib/sql/sqlCompletion"; import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionObject, SqlCompletionTable } from "@/lib/sql/sqlCompletion";
@@ -2882,6 +2883,7 @@ export const useConnectionStore = defineStore("connection", () => {
if (existing) { if (existing) {
existing.label = config.name; existing.label = config.name;
existing.type = "connection"; existing.type = "connection";
existing.searchAliases = connectionSidebarSearchAliases(config);
existing.connectionId = id; existing.connectionId = id;
existing.comment = config.note || null; existing.comment = config.note || null;
existing.children = existing.children || []; existing.children = existing.children || [];
@@ -2890,6 +2892,7 @@ export const useConnectionStore = defineStore("connection", () => {
id, id,
label: config.name, label: config.name,
type: "connection", type: "connection",
searchAliases: connectionSidebarSearchAliases(config),
connectionId: id, connectionId: id,
isExpanded: false, isExpanded: false,
children: [], children: [],
@@ -6917,6 +6920,7 @@ export const useConnectionStore = defineStore("connection", () => {
...existing, ...existing,
label: node.label, label: node.label,
comment: node.comment, comment: node.comment,
searchAliases: node.searchAliases,
pinned: node.pinned, pinned: node.pinned,
children: withSavedSqlRoot(node.connectionId!, existing.children || [], existing), children: withSavedSqlRoot(node.connectionId!, existing.children || [], existing),
}); });
+2
View File
@@ -968,6 +968,8 @@ export interface TreeNode {
id: string; id: string;
label: string; label: string;
type: TreeNodeType; type: TreeNodeType;
/** Additional values matched by sidebar search without rendering them. */
searchAliases?: string[];
children?: TreeNode[]; children?: TreeNode[];
isLoading?: boolean; isLoading?: boolean;
isExpanded?: boolean; isExpanded?: boolean;
+10
View File
@@ -164,6 +164,16 @@ test("buildTreeNodesFromLayout creates group nodes with connection children", ()
assert.equal(nodes[1].id, "c"); assert.equal(nodes[1].id, "c");
}); });
test("buildTreeNodesFromLayout adds connection host and username as search aliases", () => {
const connection = conn("a", "Production reporting");
connection.host = "192.168.0.27";
connection.username = "report_user";
const nodes = buildTreeNodesFromLayout({ groups: [], order: [{ type: "connection", id: "a" }] }, [connection], new Set());
assert.deepEqual(nodes[0]?.searchAliases, ["192.168.0.27", "report_user"]);
});
test("buildTreeNodesFromLayout respects collapsed groups", () => { test("buildTreeNodesFromLayout respects collapsed groups", () => {
const layout: SidebarLayout = { const layout: SidebarLayout = {
groups: [{ id: "g1", name: "G", collapsed: true }], groups: [{ id: "g1", name: "G", collapsed: true }],
+16
View File
@@ -212,6 +212,22 @@ test("preserves loaded children when the connection itself matches search", () =
assert.equal(filtered[0]?.children?.[0]?.children?.[0]?.label, "products"); assert.equal(filtered[0]?.children?.[0]?.children?.[0]?.label, "products");
}); });
test("matches connections by host and username search aliases", () => {
const connection: TreeNode = {
id: "conn:1",
label: "Production reporting",
type: "connection",
connectionId: "conn:1",
searchAliases: ["192.168.0.27", "report_user"],
isExpanded: false,
children: [],
};
assert.equal(filterSidebarTree([connection], "192.168.0", new Set())[0]?.id, connection.id);
assert.equal(filterSidebarTree([connection], "report_user", new Set())[0]?.id, connection.id);
assert.deepEqual(filterSidebarTree([connection], "unrelated", new Set()), []);
});
test("omits synthetic connection management entries when the connection itself matches search", () => { test("omits synthetic connection management entries when the connection itself matches search", () => {
const nodes: TreeNode[] = [ const nodes: TreeNode[] = [
{ {