1 parent
5f0ad9217a
commit
01320286e8
4 files changed
+120
-5
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sortConnectionListForDisplay } from "@/lib/sidebar/connectionListSort";
|
||||
import { orderConnectionsForSidebarDisplay, sortConnectionListForDisplay } from "@/lib/sidebar/connectionListSort";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
|
||||
function connection(id: string, label: string): TreeNode {
|
||||
@@ -36,4 +36,18 @@ describe("connection list display sort", () => {
|
||||
expect(manualTree[1]?.children?.map((node) => node.id)).toEqual(["beta", "alpha", "alpha-two"]);
|
||||
expect(sorted[1]).not.toBe(manualTree[1]);
|
||||
});
|
||||
|
||||
it("flattens nested manual order, appends missing connections, and removes duplicates", () => {
|
||||
const connections = [{ id: "alpha" }, { id: "beta" }, { id: "zebra" }, { id: "new" }];
|
||||
const tree = [connection("zebra", "Zebra"), group("parent", "Parent", [connection("beta", "Beta"), group("nested", "Nested", [connection("alpha", "Alpha"), connection("zebra", "Zebra duplicate")])])];
|
||||
|
||||
expect(orderConnectionsForSidebarDisplay(connections, tree, "manual").map((connection) => connection.id)).toEqual(["zebra", "beta", "alpha", "new"]);
|
||||
});
|
||||
|
||||
it("projects configs through the sidebar's alphabetical display mode", () => {
|
||||
const connections = [{ id: "zebra" }, { id: "alpha" }, { id: "beta" }];
|
||||
const tree = [group("parent", "Parent", [connection("zebra", "Zebra"), group("nested", "Nested", [connection("beta", "Beta")]), connection("alpha", "Alpha")])];
|
||||
|
||||
expect(orderConnectionsForSidebarDisplay(connections, tree, "asc").map((connection) => connection.id)).toEqual(["alpha", "beta", "zebra"]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import type { ConnectionConfig, TreeNode } from "@/types/database";
|
||||
|
||||
export type ConnectionListSortMode = "manual" | "asc" | "desc";
|
||||
|
||||
@@ -38,3 +38,34 @@ export function sortConnectionListForDisplay(nodes: readonly TreeNode[], mode: C
|
||||
if (mode === "manual") return nodes as TreeNode[];
|
||||
return sortConnectionSiblingsForDisplay(nodes, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects connection configs into the order shown by the sidebar. Connections
|
||||
* missing from the current tree are appended in store order so a stale layout
|
||||
* or newly added connection cannot make them unavailable to other surfaces.
|
||||
*/
|
||||
export function orderConnectionsForSidebarDisplay<T extends Pick<ConnectionConfig, "id">>(connections: readonly T[], nodes: readonly TreeNode[], mode: ConnectionListSortMode): T[] {
|
||||
const connectionsById = new Map<string, T>();
|
||||
for (const connection of connections) {
|
||||
if (!connectionsById.has(connection.id)) connectionsById.set(connection.id, connection);
|
||||
}
|
||||
|
||||
const ordered: T[] = [];
|
||||
const seen = new Set<string>();
|
||||
const appendConnection = (id: string) => {
|
||||
const connection = connectionsById.get(id);
|
||||
if (!connection || seen.has(id)) return;
|
||||
seen.add(id);
|
||||
ordered.push(connection);
|
||||
};
|
||||
const visit = (treeNodes: readonly TreeNode[]) => {
|
||||
for (const node of treeNodes) {
|
||||
if (node.type === "connection") appendConnection(node.connectionId ?? node.id);
|
||||
else if (node.type === "connection-group" && node.children) visit(node.children);
|
||||
}
|
||||
};
|
||||
|
||||
visit(sortConnectionListForDisplay(nodes, mode));
|
||||
for (const connection of connections) appendConnection(connection.id);
|
||||
return ordered;
|
||||
}
|
||||
Reference in new issue
Block a user