1 parent
5f0ad9217a
commit
01320286e8
4 files changed
+120
-5
No files matched your search
@@ -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