All files / apps/developer-portal/layers/projects/app/utils architectureMap.ts

97.22% Statements 35/36
57.14% Branches 8/14
100% Functions 9/9
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67      3x 3x 3x   3x 8x 8x 8x   8x 8x 8x 8x 8x     8x       3x 9x 9x             9x   20x 9x 20x   9x 20x   9x   9x   9x   9x       20x 20x           8x                
import { Graph, layout as runLayout, type EdgeLabel, type NodeLabel } from "@dagrejs/dagre";
import type { ProjectNode } from "#layers/snapshot/shared/types/ProjectNode.ts";
 
export const MAP_NODE_WIDTH = 184;
export const MAP_NODE_HEIGHT = 48;
const MAP_PADDING = 24;
 
const smoothPath = (points: { x: number; y: number }[]): string => {
    const [first, ...rest] = points;
    const last = rest.at(-1);
    Iif (!first || !last) return "";
 
    let path = `M${first.x},${first.y}`;
    for (let index = 0; index < rest.length - 1; index++) {
        const point = rest[index]!;
        const next = rest[index + 1]!;
        path += ` Q${point.x},${point.y} ${(point.x + next.x) / 2},${(point.y + next.y) / 2}`;
    }
 
    return `${path} L${last.x},${last.y}`;
};
 
/** Dependents above what they depend on, one arrow per `dependsOn` edge between listed projects. */
export const architectureMap = (projects: ProjectNode[]) => {
    const graph = new Graph<object, NodeLabel, EdgeLabel>();
    graph.setGraph({
        rankdir: "TB",
        nodesep: 24,
        ranksep: 56,
        marginx: MAP_PADDING,
        marginy: MAP_PADDING,
    });
    graph.setDefaultEdgeLabel(() => ({}));
 
    const listed = new Set(projects.map(({ name }) => name));
    for (const { name } of projects)
        graph.setNode(name, { width: MAP_NODE_WIDTH, height: MAP_NODE_HEIGHT });
 
    const links = projects.flatMap(({ name, dependsOn }) =>
        dependsOn.filter((target) => listed.has(target)).map((target) => [name, target] as const),
    );
    for (const [from, to] of links) graph.setEdge(from, to);
 
    runLayout(graph);
 
    const label = graph.graph() as { width?: number; height?: number };
 
    return {
        width: label.width ?? 0,
        height: label.height ?? 0,
        nodes: projects.map((project) => {
            const node = graph.node(project.name);
            return {
                project,
                x: (node.x ?? 0) - MAP_NODE_WIDTH / 2,
                y: (node.y ?? 0) - MAP_NODE_HEIGHT / 2,
            };
        }),
        edges: links.map(([from, to]) => ({
            key: `${from}>${to}`,
            from,
            to,
            path: smoothPath(graph.edge(from, to).points ?? []),
        })),
    };
};