feat(plugin): add table-scoped context-menu contributions

This commit is contained in:
jearhe authored and GitHub committed 2026-09-22 14:23:53 +08:00
1 parent def42987a4
commit 78315c377a
15 files changed
+318 -29

No files matched your search

+17
View File
@@ -72,6 +72,23 @@ describe("case-sensitive database objects", () => {
expect(grouped.find((node) => node.type === "group-views")?.children?.map((node) => node.valid)).toEqual([false, true]);
});
it("stores the canonical table name separately from the display label", () => {
const nodes = buildTableTreeNodes({
...context,
schema: "public",
tables: [
{ name: "users", table_type: "BASE TABLE" },
{ name: "active_users", table_type: "VIEW" },
],
});
const table = nodes.find((node) => node.type === "table");
const view = nodes.find((node) => node.type === "view");
expect(table).toMatchObject({ type: "table", label: "users", tableName: "users" });
expect(view).toMatchObject({ type: "view", label: "active_users" });
expect(view?.tableName).toBeUndefined();
});
it("keeps table nodes whose names differ only by case across pages", () => {
const firstPage = buildTableTreeNodes({
...context,
+12 -6
View File
@@ -214,15 +214,21 @@ describe("FrontendPluginRegistry", () => {
const registry = createFrontendPluginRegistry([
installedPlugin("com.example.plugin", [
{ type: "context-menu", id: "example.inspect", label: "Inspect endpoint", menu: "connection" },
{ type: "context-menu", id: "example.other", label: "Wrong surface", menu: "tree" },
{ type: "context-menu", id: "example.inspect-table", label: "Inspect table", menu: "table" },
]),
]);
const items = registry.listContextMenuItems("connection");
expect(items).toHaveLength(1);
expect(items[0]?.contribution.id).toBe("example.inspect");
expect(items[0]?.plugin.manifest.id).toBe("com.example.plugin");
expect(registry.listContextMenuItems("tree")).toHaveLength(1);
const connectionItems = registry.listContextMenuItems("connection");
expect(connectionItems).toHaveLength(1);
expect(connectionItems[0]?.contribution.id).toBe("example.inspect");
expect(connectionItems[0]?.plugin.manifest.id).toBe("com.example.plugin");
const tableItems = registry.listContextMenuItems("table");
expect(tableItems).toHaveLength(1);
expect(tableItems[0]?.contribution.id).toBe("example.inspect-table");
const emptyRegistry = createFrontendPluginRegistry([installedPlugin("com.example.empty")]);
expect(emptyRegistry.listContextMenuItems("table")).toHaveLength(0);
expect(registry.listContextMenuItems("missing")).toHaveLength(0);
});
@@ -58,7 +58,7 @@ export class FrontendPluginRegistry {
return this.listContributions("filesystem-provider");
}
/** Native context-menu entries declared for a specific menu surface such as `connection`. */
/** Native context-menu entries declared for a specific menu surface. */
listContextMenuItems(menu: string): PluginContributionEntry<PluginContextMenuContribution>[] {
return this.listContributions("context-menu").filter((entry) => entry.contribution.menu === menu);
}
+63
View File
@@ -0,0 +1,63 @@
import { describe, expect, it } from "vitest";
import type { TreeNode } from "@/types/database";
import { buildPluginTableContext, buildPluginTableContextMenuInvocation } from "./pluginContext";
function tableNode(overrides: Partial<TreeNode> = {}): TreeNode {
return {
id: "connection:demo:public:users",
label: "users",
type: "table",
connectionId: "conn-1",
database: "demo",
schema: "public",
tableName: "users",
...overrides,
};
}
describe("plugin table context", () => {
it("builds the public context from canonical table metadata", () => {
expect(buildPluginTableContext(tableNode())).toEqual({
connectionId: "conn-1",
database: "demo",
schema: "public",
table: "users",
});
});
it("omits optional database and schema values when the node has no scope", () => {
expect(buildPluginTableContext(tableNode({ database: undefined, schema: undefined }))).toEqual({
connectionId: "conn-1",
table: "users",
});
expect(buildPluginTableContext(tableNode({ database: "", schema: "" }))).toEqual({
connectionId: "conn-1",
table: "users",
});
});
it("fails closed when canonical table identity is missing instead of using the label", () => {
const node = tableNode({ tableName: undefined, label: "users" });
expect(buildPluginTableContext(node)).toBeNull();
expect(buildPluginTableContextMenuInvocation("example.inspect", node)).toBeNull();
});
it("prepares the existing contextMenu backend invocation without secrets", () => {
expect(buildPluginTableContextMenuInvocation("example.inspect", tableNode())).toEqual({
method: "contextMenu/example.inspect",
params: {
table: {
connectionId: "conn-1",
database: "demo",
schema: "public",
table: "users",
},
},
});
});
it("does not build table context for non-table nodes", () => {
expect(buildPluginTableContext(tableNode({ type: "view" }))).toBeNull();
});
});
@@ -0,0 +1,44 @@
import type { PluginTableContext, TreeNode } from "@/types/database";
export type PluginTableContextNode = Pick<TreeNode, "type" | "connectionId" | "database" | "schema" | "tableName">;
export interface PluginContextMenuInvocation {
method: string;
params: {
table: PluginTableContext;
};
}
function nonEmptyContextValue(value: string | undefined): string | undefined {
const normalized = value?.trim();
return normalized || undefined;
}
/**
* Builds the public table identity from the sidebar node's canonical metadata.
* Display labels are deliberately not part of this input or its fallback path.
*/
export function buildPluginTableContext(node: PluginTableContextNode): PluginTableContext | null {
if (node.type !== "table") return null;
const connectionId = nonEmptyContextValue(node.connectionId);
const table = nonEmptyContextValue(node.tableName);
if (!connectionId || !table) return null;
const context: PluginTableContext = { connectionId, table };
const database = nonEmptyContextValue(node.database);
const schema = nonEmptyContextValue(node.schema);
if (database !== undefined) context.database = database;
if (schema !== undefined) context.schema = schema;
return context;
}
export function buildPluginTableContextMenuInvocation(contributionId: string, node: PluginTableContextNode): PluginContextMenuInvocation | null {
if (!contributionId.trim()) return null;
const table = buildPluginTableContext(node);
if (!table) return null;
return {
method: `contextMenu/${contributionId}`,
params: { table },
};
}
+1
View File
@@ -87,6 +87,7 @@ function makeTableTreeEntry({
database,
schema,
catalog,
...(objectType === "TABLE" ? { tableName: name } : {}),
isExpanded: false,
children: [],
};