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

96.61% Statements 57/59
82.85% Branches 29/35
92.85% Functions 26/28
97.87% Lines 46/47

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  16x 2x 26x             3x         14x           2x   2x       2x               1x 2x 1x                                               1x     2x                                                   7x   7x   7x     7x 14x   8x 2x     7x 25x           7x 16x   16x 23x 14x 14x   14x           1x         1x                 7x 3x 3x 3x     7x 1x     7x 3x 2x       7x 4x 3x   3x 3x 2x      
<template>
    <div class="flex flex-col gap-4">
        <div class="flex flex-wrap gap-2">
            <UButton
                v-for="type in types"
                :key="type.id"
                :label="$t('workflowPagePicker.add', { type: type.title, points: type.cost })"
                icon="i-lucide-plus"
                color="neutral"
                variant="outline"
                @click="add(type.id)"
            />
        </div>
 
        <ul v-if="cards.length > 0" class="grid gap-3 sm:grid-cols-2">
            <li
                v-for="card in cards"
                :key="card.key"
                class="flex flex-col gap-3 rounded-xl border p-4"
                :class="card.editing ? 'border-primary bg-primary/5' : 'border-default'"
            >
                <div class="flex items-start gap-3">
                    <span
                        class="flex size-9 shrink-0 items-center justify-center rounded-lg bg-elevated text-muted"
                    >
                        <UIcon name="i-lucide-file-text" class="size-5" />
                    </span>
                    <div class="min-w-0 flex-1">
                        <UInput
                            v-if="card.editing"
                            :model-value="card.title"
                            :placeholder="$t('workflowPagePicker.name', { type: card.typeTitle })"
                            :aria-label="$t('workflowPagePicker.name', { type: card.typeTitle })"
                            autofocus
                            class="w-full"
                            @update:model-value="rename(card.key, String($event))"
                            @keydown.enter.prevent="commit(card.key)"
                            @blur="commit(card.key)"
                        />
                        <button
                            v-else
                            type="button"
                            class="block w-full cursor-text truncate py-1 text-left font-semibold"
                            :aria-label="$t('workflowPagePicker.rename', { page: card.title })"
                            @click="editing = card.key"
                        >
                            {{ card.title }}
                        </button>
                        <p v-if="card.error" class="mt-1 text-xs text-error">{{ card.error }}</p>
                    </div>
                    <UButton
                        icon="i-lucide-x"
                        :aria-label="
                            $t('workflowPagePicker.remove', {
                                page: card.title || card.typeTitle.toLowerCase(),
                            })
                        "
                        color="neutral"
                        variant="ghost"
                        size="sm"
                        @mousedown.prevent
                        @click="remove(card.key)"
                    />
                </div>
                <div class="flex flex-wrap items-center gap-1.5">
                    <UBadge :label="card.typeTitle" color="neutral" variant="subtle" size="sm" />
                    <UBadge
                        :label="$t('workflowPagePicker.points', { points: card.points })"
                        color="neutral"
                        variant="subtle"
                        size="sm"
                        class="tabular-nums"
                    />
                    <span v-if="card.note" class="text-xs text-muted">{{ card.note }}</span>
                </div>
            </li>
        </ul>
    </div>
</template>
 
<script setup lang="ts">
import { pageCosts, type Template } from "#layers/workflows/shared/workflows.ts";
 
interface Props {
    template: Template;
    /** One per page, in the model's order. */
    errors: (string | null)[];
}
 
const { template, errors } = defineProps<Props>();
const pages = defineModel<{ key: number; type: string; title: string }[]>({ required: true });
 
const { t } = useI18n();
 
const editing = ref<number | null>(null);
 
/** The setups a type waits on that come in with a page, such as the comments setup. */
const setupsOf = (type: string) =>
    template.capabilities.filter(
        ({ id, onDemand }) =>
            onDemand &&
            template.types.find((candidate) => candidate.id === type)?.dependsOn.includes(id),
    );
 
const types = computed(() =>
    template.types.map((type) => ({
        ...type,
        cost: pageCosts(template, [...pages.value, { type: type.id }]).at(-1)?.points ?? 0,
    })),
);
 
const cards = computed(() => {
    const costs = pageCosts(template, pages.value);
 
    return pages.value.map((page, index) => {
        const type = template.types.find(({ id }) => id === page.type);
        const { points, setup } = costs[index] ?? { points: 0, setup: [] };
        const shared = setupsOf(page.type).filter((block) => !setup.includes(block));
 
        return {
            ...page,
            typeTitle: type?.title ?? page.type,
            points,
            note: [
                ...setup.map((block) =>
                    t("workflowPagePicker.includes", {
                        block: block.title,
                        points: block.complexity ?? 0,
                    }),
                ),
                ...shared.map((block) => t("workflowPagePicker.counted", { block: block.title })),
            ].join(" ยท "),
            editing: editing.value === page.key,
            // An empty title is the state a new block starts in, not a mistake yet.
            error: page.title.trim() ? (errors[index] ?? null) : null,
        };
    });
});
 
const add = (type: string) => {
    const key = Math.max(0, ...pages.value.map((page) => page.key)) + 1;
    pages.value = [...pages.value, { key, type, title: "" }];
    editing.value = key;
};
 
const rename = (key: number, title: string) => {
    pages.value = pages.value.map((page) => (page.key === key ? { ...page, title } : page));
};
 
const remove = (key: number) => {
    pages.value = pages.value.filter((page) => page.key !== key);
    if (editing.value === key) editing.value = null;
};
 
/** Saves the block being named; one left empty is dropped, one with a problem stays open. */
const commit = (key: number) => {
    const index = pages.value.findIndex((page) => page.key === key);
    const page = pages.value[index];
 
    Iif (!page) return;
    if (!page.title.trim()) remove(key);
    else if (!errors[index]) editing.value = null;
};
</script>