All files / apps/developer-portal/layers/workflows/app/utils pathRelation.ts

95.65% Statements 22/23
70% Branches 7/10
100% Functions 8/8
100% Lines 16/16

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      4x       32x   32x 14x 28x 28x   28x 42x 14x 14x 14x         28x     14x   14x 20x 22x        
import type { CapabilityView } from "#layers/workflows/shared/workflows.ts";
 
/** Whether a block sits on the selected block's path, and how; everything else is greyed out. */
export const pathRelation = (
    shown: CapabilityView[],
    selected: string | null,
): Record<string, "" | "needed" | "waits"> => {
    if (!selected) return Object.fromEntries(shown.map(({ id }) => [id, ""]));
 
    const byId = new Map(shown.map((capability) => [capability.id, capability]));
    const walk = (from: string, next: (capability: CapabilityView) => string[]) => {
        const seen = new Set<string>();
        const queue = [from];
 
        for (let id = queue.pop(); id !== undefined; id = queue.pop()) {
            for (const other of next(byId.get(id)!) ?? []) {
                Eif (!seen.has(other) && byId.has(other)) {
                    seen.add(other);
                    queue.push(other);
                }
            }
        }
 
        return seen;
    };
 
    Iif (!byId.has(selected)) return {};
 
    return {
        ...Object.fromEntries([...walk(selected, (c) => c.needs)].map((id) => [id, "needed"])),
        ...Object.fromEntries([...walk(selected, (c) => c.unblocks)].map((id) => [id, "waits"])),
        [selected]: "",
    };
};