All files / apps/developer-portal/layers/workflows/app/components WorkflowGraph.vue

86.07% Statements 68/79
80% Branches 64/80
73.8% Functions 31/42
88.33% Lines 53/60

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266  34x                       2x 2x 1x       2x                                   102x                           23x                       72x                                                       7x               3x             3x   3x                           2x                                                                                                   23x                         23x 23x     23x 31x 23x 27x 4x     23x 30x     57x 24x 29x   23x     23x 60x 30x                     23x 92x 48x     23x 24x           23x 60x     23x 7x 6x 6x     23x 10x     23x 2x     23x 23x     23x       23x 23x 23x 30x 7x      
<template>
    <Teleport to="body" :disabled="!expanded">
        <div
            ref="viewport"
            v-bind="$attrs"
            class="group touch-none overflow-hidden bg-default select-none"
            :class="[
                expanded ? 'fixed inset-0 z-50 rounded-none' : 'relative h-144 rounded-xl',
                dragging ? 'cursor-grabbing' : 'cursor-grab',
            ]"
            :role="expanded ? 'dialog' : undefined"
            :aria-modal="expanded || undefined"
            :aria-label="expanded ? $t('workflowGraph.label') : undefined"
            @pointerdown="press"
            @pointermove="move"
            @pointerup="release"
            @pointercancel="release"
            @click="clickMap"
            @keydown="keydownMap"
            @wheel="wheel($event, expanded)"
        >
            <div
                class="absolute top-0 left-0 origin-top-left"
                :class="{ 'transition-transform duration-500 ease-out': easing }"
                :style="{
                    width: `${layout.width}px`,
                    height: `${layout.height}px`,
                    transform: `translate(${camera.x}px, ${camera.y}px) scale(${camera.k})`,
                }"
            >
                <svg
                    class="absolute inset-0 overflow-visible"
                    :width="layout.width"
                    :height="layout.height"
                    aria-hidden="true"
                >
                    <defs>
                        <marker
                            v-for="tone in edgeTones"
                            :id="`arrow-${tone}-${uid}`"
                            :key="tone"
                            viewBox="0 0 10 10"
                            refX="9"
                            refY="5"
                            markerWidth="7"
                            markerHeight="7"
                            orient="auto-start-reverse"
                        >
                            <path d="M0,0 L10,5 L0,10 z" :fill="tones[tone].stroke" />
                        </marker>
                    </defs>
                    <path
                        v-for="edge in orderedEdges"
                        :key="edge.key"
                        :d="edge.path"
                        fill="none"
                        :stroke="tones[edgeTone(edge)].stroke"
                        :stroke-width="tones[edgeTone(edge)].width"
                        :opacity="tones[edgeTone(edge)].opacity"
                        :marker-end="`url(#arrow-${edgeTone(edge)}-${uid})`"
                    />
                </svg>
 
                <div
                    v-for="node in layout.nodes"
                    :key="node.id"
                    class="absolute flex transition"
                    :class="relation[node.id] === undefined && 'opacity-25 grayscale'"
                    :style="{
                        left: `${node.x}px`,
                        top: `${node.y}px`,
                        width: `${NODE_WIDTH}px`,
                        height: `${NODE_HEIGHT}px`,
                        '--state': capabilityColor(node.capability.state),
                    }"
                >
                    <button
                        type="button"
                        class="relative flex flex-1 flex-col justify-center gap-0.5 overflow-hidden rounded-xl border bg-default py-2 pr-9 pl-4 text-left"
                        :class="
                            node.id === focus
                                ? 'border-inverted ring-2 ring-inverted'
                                : 'border-(--state)'
                        "
                        :aria-pressed="node.id === focus"
                        :aria-label="
                            $t('workflowGraph.node', {
                                title: node.capability.title,
                                state: $t(`workflows.capabilityState.${node.capability.state}`),
                            })
                        "
                        @click="click(node.id)"
                    >
                        <span
                            class="absolute inset-y-0 left-0 w-1.5"
                            :style="{ background: stageColors[node.capability.stage] }"
                            aria-hidden="true"
                        />
                        <span
                            class="flex items-center gap-1 truncate text-xs/tight font-semibold text-highlighted"
                        >
                            <UIcon
                                v-if="node.icon"
                                :name="node.icon"
                                class="size-3.5 shrink-0 text-dimmed"
                            />
                            <span class="truncate">{{ node.capability.title }}</span>
                        </span>
                        <span class="truncate text-xs font-medium text-(--state)">
                            {{ $t(`workflows.capabilityState.${node.capability.state}`) }}
                            <span v-if="relation[node.id]" class="text-muted">
                                ยท {{ $t(`workflowGraph.relation.${relation[node.id]}`) }}</span
                            >
                        </span>
                    </button>
                    <UButton
                        icon="i-lucide-info"
                        color="neutral"
                        variant="ghost"
                        size="xs"
                        class="absolute top-1/2 right-1.5 -translate-y-1/2 rounded-full"
                        :aria-label="$t('workflowGraph.details', { title: node.capability.title })"
                        @click="details(node.id)"
                    />
                </div>
            </div>
 
            <div
                class="absolute top-3 right-3 flex flex-col gap-1 transition-opacity"
                :class="!expanded && 'opacity-0 group-hover:opacity-100 focus-within:opacity-100'"
                @pointerdown.stop
            >
                <UButton
                    :icon="expanded ? 'i-lucide-minimize-2' : 'i-lucide-maximize-2'"
                    color="neutral"
                    variant="outline"
                    size="sm"
                    :aria-label="expanded ? $t('workflowGraph.exitFull') : $t('workflowGraph.full')"
                    @click="expanded = !expanded"
                />
                <UButton
                    icon="i-lucide-plus"
                    color="neutral"
                    variant="outline"
                    size="sm"
                    :aria-label="$t('workflowGraph.zoomIn')"
                    @click="zoomBy(1.25)"
                />
                <UButton
                    icon="i-lucide-minus"
                    color="neutral"
                    variant="outline"
                    size="sm"
                    :aria-label="$t('workflowGraph.zoomOut')"
                    @click="zoomBy(0.8)"
                />
                <UButton
                    icon="i-lucide-locate-fixed"
                    color="neutral"
                    variant="outline"
                    size="sm"
                    :aria-label="$t('workflowGraph.fit')"
                    @click="fit(true)"
                />
            </div>
        </div>
    </Teleport>
</template>
 
<script setup lang="ts">
const { capabilities, stages, selected = null } = defineProps<Props>();
 
const emit = defineEmits<{ select: [capability: string]; details: [capability: string] }>();
 
defineOptions({ inheritAttrs: false });
 
import type { CapabilityView, StageView } from "#layers/workflows/shared/workflows.ts";
 
interface Props {
    /** Every block in the project; switched-off ones are left out of the map. */
    capabilities: CapabilityView[];
    stages: StageView[];
    selected?: string | null;
}
 
const uid = useId();
const viewport = ref<HTMLElement | null>(null);
 
/** The whole map, without leaving the page's selection, which the cards and skyline still show. */
const overview = ref(false);
const focus = computed(() => (overview.value ? null : selected));
watch(
    () => selected,
    () => (overview.value = false),
);
 
const stageColors = computed(() =>
    Object.fromEntries(stages.map((stage, index) => [stage.id, stageColor(index)])),
);
 
const shown = computed(() => capabilities.filter(({ state }) => state !== "off"));
const layout = computed(() => graphLayout(shown.value));
const relation = computed(() => pathRelation(shown.value, focus.value));
 
const edgeTones = ["path", "off", "plain"] as const;
type EdgeTone = (typeof edgeTones)[number];
 
const tones = computed<Record<EdgeTone, { stroke: string; width: number; opacity: number }>>(() => {
    const stage = capabilities.find(({ id }) => id === focus.value)?.stage;
    return {
        path: {
            stroke: (stage && stageColors.value[stage]) || "var(--ui-text-highlighted)",
            width: 2.5,
            opacity: 1,
        },
        off: { stroke: "var(--ui-border-accented)", width: 1, opacity: 0.12 },
        plain: { stroke: "var(--ui-text-muted)", width: 1.25, opacity: 0.6 },
    };
});
 
const edgeTone = ({ from, to }: { from: string; to: string }): EdgeTone => {
    if (!focus.value) return "plain";
    return relation.value[from] !== undefined && relation.value[to] !== undefined ? "path" : "off";
};
 
const orderedEdges = computed(() =>
    [...layout.value.edges].sort(
        (x, y) => Number(edgeTone(x) === "path") - Number(edgeTone(y) === "path"),
    ),
);
 
const { camera, easing, dragging, wasDragged, fit, zoomBy, wheel, press, move, release } =
    useGraphCamera(viewport, () =>
        layout.value.nodes.filter(({ id }) => relation.value[id] !== undefined),
    );
 
const click = (id: string) => {
    if (wasDragged()) return;
    if (id === selected) overview.value = !overview.value;
    emit("select", id);
};
 
const clickMap = (event: MouseEvent) => {
    if (!wasDragged() && !(event.target as Element).closest("button")) overview.value = true;
};
 
const details = (id: string) => {
    if (!wasDragged()) emit("details", id);
};
 
const expanded = ref(false);
defineShortcuts({ escape: { usingInput: true, handler: () => (expanded.value = false) } });
 
/** Full screen claims Escape to close, so only the inline map treats it as a click on empty space. */
const keydownMap = (event: KeyboardEvent) => {
    if (event.key === "Escape" && !expanded.value) overview.value = true;
};
 
onMounted(() => fit(false));
watch(expanded, () => nextTick(() => fit(false)));
watch(
    () => [focus.value, layout.value] as const,
    () => nextTick(() => fit(true)),
);
</script>