feat(connection): choose group when creating a connection

This commit is contained in:
zipg authored and GitHub committed 2026-08-28 11:11:00 +08:00
1 parent 4636788f49
commit d8ec9bb8d0
13 files changed
+230 -39

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { connectionIdsInGroups, deleteGroups, findConnectionGroupPath, reorderEntries } from "@/lib/sidebar/sidebarLayout";
import { connectionGroupDestinationRows, connectionGroupIdForSelection, connectionIdsInGroups, deleteGroups, findConnectionGroupPath, reorderEntries } from "@/lib/sidebar/sidebarLayout";
import type { SidebarLayout } from "@/types/database";
const layout: SidebarLayout = {
@@ -203,3 +203,24 @@ describe("sorted sidebar move-only drops", () => {
});
});
});
describe("connection group selection", () => {
it("lists nested groups in sidebar order with complete paths", () => {
expect(connectionGroupDestinationRows(layout)).toEqual([
{ id: "project", name: "Project", depth: 0, path: ["Project"] },
{ id: "staging", name: "Staging", depth: 1, path: ["Project", "Staging"] },
]);
});
it("uses the focused group or the group containing a focused connection", () => {
expect(connectionGroupIdForSelection(layout, "staging")).toBe("staging");
expect(connectionGroupIdForSelection(layout, "nested", "nested")).toBe("staging");
expect(connectionGroupIdForSelection(layout, "grouped")).toBe("project");
});
it("falls back to ungrouped for root or unknown selections", () => {
expect(connectionGroupIdForSelection(layout, "root", "root")).toBeNull();
expect(connectionGroupIdForSelection(layout, "missing")).toBeNull();
expect(connectionGroupIdForSelection(layout)).toBeNull();
});
});
@@ -367,6 +367,14 @@ export function connectionGroupDestinationRows(layout: SidebarLayout): Connectio
return rows;
}
/** Resolve the group implied by a selected group, connection, or descendant node. */
export function connectionGroupIdForSelection(layout: SidebarLayout, selectedNodeId?: string | null, selectedConnectionId?: string | null): string | null {
if (selectedNodeId && connectionGroupDestinationRows(layout).some((group) => group.id === selectedNodeId)) return selectedNodeId;
const connectionId = selectedConnectionId || selectedNodeId;
if (!connectionId) return null;
return findConnectionLocation(layout, connectionId)?.groupId ?? null;
}
function findGroupEntry(entries: SidebarOrderEntry[], groupId: string): Extract<SidebarOrderEntry, { type: "group" }> | null {
for (const entry of entries) {
if (entry.type !== "group") continue;