All files / apps/developer-portal/layers/workflows/app/composables useCapabilitySelection.ts

96.55% Statements 28/29
84.61% Branches 11/13
92.3% Functions 12/13
95.83% Lines 23/24

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        2x 9x   9x 10x 10x 17x   10x 8x           9x 11x     9x 9x 9x 9x 9x 11x       9x 9x 11x     9x 2x 2x     9x                      
import type { ComputedRef } from "vue";
import type { ProjectView } from "#layers/workflows/shared/workflows.ts";
 
/** One block, picked anywhere, drives the cards, the skyline and the graph. */
export const useCapabilitySelection = (view: ComputedRef<ProjectView | undefined>) => {
    const chosenId = ref<string | null>(null);
    /** What was picked, else what needs doing first, else the last block on the map. */
    const selectedId = computed(() => {
        const capabilities = view.value?.capabilities ?? [];
        const isOn = (capabilityId: string | null) =>
            capabilities.some(({ id: other, state }) => other === capabilityId && state !== "off");
 
        if (isOn(chosenId.value)) return chosenId.value;
        return (
            view.value?.left[0]?.id ??
            capabilities.findLast(({ state }) => state !== "off")?.id ??
            null
        );
    });
    const selected = computed(() =>
        view.value?.capabilities.find((capability) => capability.id === selectedId.value),
    );
 
    const stageId = ref<string | null>(null);
    const activeIndex = computed(() => {
        const stages = view.value?.stages ?? [];
        const wanted = stageId.value ?? selected.value?.stage;
        return Math.max(
            stages.findIndex((stage) => stage.id === wanted),
            0,
        );
    });
    const activeStage = computed(() => view.value?.stages[activeIndex.value]);
    const selectedStage = computed(
        () => view.value?.stages.find((stage) => stage.id === selected.value?.stage)?.title,
    );
 
    const select = (capabilityId: string) => {
        chosenId.value = capabilityId;
        stageId.value = null;
    };
 
    return {
        chosenId,
        selectedId,
        selected,
        stageId,
        activeIndex,
        activeStage,
        selectedStage,
        select,
    };
};