All files / apps/developer-portal/layers/workflows/server/utils projectMap.ts

97.72% Statements 43/44
86.66% Branches 13/15
100% Functions 19/19
100% Lines 37/37

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                            2x     2x               2x   2x 21x 19x 8x     2x 21x     2x             7x 7x 28x 14x   7x 21x                   21x   7x 14x 21x 21x 14x 14x 14x 14x 14x 14x 14x 14x                   7x   7x 14x       42x         14x       21x 21x         3x            
import type { CapabilityState } from "../../shared/types/CapabilityState.ts";
import type { ProjectView } from "../../shared/types/ProjectView.ts";
 
/** A map's grid in SVG units; `header` is the band its stage titles sit in. */
export interface MapScale {
    column: number;
    nodeWidth: number;
    nodeHeight: number;
    row: number;
    header: number;
}
 
type MapTone = "done" | "moving" | "waiting";
 
const FULL_MAP: MapScale = { column: 184, nodeWidth: 164, nodeHeight: 30, row: 40, header: 36 };
 
/** Blocks only, no labels: the map at a glance. */
export const GLYPH_MAP: MapScale = {
    column: 52,
    nodeWidth: 10,
    nodeHeight: 10,
    row: 16,
    header: 0,
};
 
const MAX_CHARS = 21;
 
const toneOf = (state: CapabilityState): MapTone => {
    if (state === "done" || state === "skipped") return "done";
    if (state === "waits" || state === "blocked") return "waiting";
    return "moving";
};
 
const clip = (text: string): string =>
    text.length > MAX_CHARS ? `${text.slice(0, MAX_CHARS - 1).trimEnd()}…` : text;
 
/** A project as a static map: stages become columns, blocks rows, and each need one curve. */
export const projectMap = (view: ProjectView, scale: MapScale = FULL_MAP) => {
    const {
        column: COLUMN,
        nodeWidth: NODE_WIDTH,
        nodeHeight: NODE_HEIGHT,
        row: ROW,
        header: HEADER,
    } = scale;
    const stages = view.stages
        .map((stage) => ({ ...stage, blocks: stage.capabilities.filter((block) => block.enabled) }))
        .filter((stage) => stage.blocks.length > 0);
 
    const nodes = stages.flatMap((stage, column) =>
        stage.blocks.map((block, row) => ({
            id: block.id,
            title: block.title,
            label: clip(block.title),
            tone: toneOf(block.state),
            owner: block.kind === "owner",
            x: column * COLUMN,
            y: HEADER + row * ROW,
        })),
    );
    const byId = new Map(nodes.map((node) => [node.id, node]));
 
    const edges = stages.flatMap((stage) =>
        stage.blocks.flatMap((block) => {
            const to = byId.get(block.id)!;
            return block.needs.flatMap((need) => {
                const from = byId.get(need);
                Iif (!from) return [];
                const x1 = from.x + NODE_WIDTH;
                const y1 = from.y + NODE_HEIGHT / 2;
                const x2 = to.x;
                const y2 = to.y + NODE_HEIGHT / 2;
                const bend = Math.max(NODE_WIDTH, (x2 - x1) / 2);
                return [
                    {
                        id: `${need}-${block.id}`,
                        d: `M${x1} ${y1} C${x1 + bend} ${y1} ${x2 - bend} ${y2} ${x2} ${y2}`,
                    },
                ];
            });
        }),
    );
 
    const issues = view.snapshot?.issues.filter((issue) => issue.reason !== "not_planned");
 
    return {
        stages: stages.map((stage, column) => ({
            id: stage.id,
            title: stage.title,
            x: column * COLUMN,
            blocks: nodes.filter((node) => node.x === column * COLUMN),
        })),
        nodes,
        edges,
        width: Math.max(0, stages.length * COLUMN - (COLUMN - NODE_WIDTH)),
        height: HEADER + Math.max(0, ...stages.map((stage) => stage.blocks.length)) * ROW,
        nodeWidth: NODE_WIDTH,
        nodeHeight: NODE_HEIGHT,
        blocks: {
            done: nodes.filter((node) => node.tone === "done").length,
            moving: nodes.filter((node) => node.tone === "moving").length,
            total: nodes.length,
        },
        tickets: issues
            ? {
                  closed: issues.filter((issue) => issue.state === "closed").length,
                  total: issues.length,
              }
            : null,
    };
};