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

95.91% Statements 47/49
80% Branches 16/20
100% Functions 15/15
100% Lines 40/40

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    1x     1x 7x 7x   7x 7x 7x   14x   7x 7x 7x                 7x 14x 14x 14x               16x 7x   7x   7x 11x 11x 11x           7x 2x 2x 2x     7x   10x 40x     7x 7x 1x 1x 1x     7x   7x 1x 1x 1x       7x                                    
import { featuresOf, type TicketSize } from "#layers/workflows/shared/workflows.ts";
 
const SAVED_KEYS = ["title", "summary", "milestone", "area", "ticketSize"] as const;
 
/** A project's settings as a draft over its file, saved in one edit; `ready` settles on load. */
export const useWorkflowSettings = () => {
    const route = useRoute();
    const id = computed(() => String(route.params.id));
 
    const read = useWorkflows();
    const { data } = read;
    const { editable, edit } = useWorkflowEdit();
 
    const base = computed(() => data.value.projects.find(({ project }) => project.id === id.value));
 
    const switches: Record<string, boolean> = {};
    const ticketSize: TicketSize = "block";
    const draft = reactive({
        title: "",
        summary: "",
        milestone: "",
        area: "",
        switches,
        ticketSize,
    });
 
    const saved = computed(() => {
        const project = base.value?.project;
        const size: TicketSize = project?.ticketSize ?? "block";
        return {
            title: project?.title ?? "",
            summary: project?.summary ?? "",
            milestone: project?.milestone ?? "",
            area: project?.area ?? "",
            ticketSize: size,
        };
    });
    const discard = () => Object.assign(draft, saved.value, { switches: {} });
    watch(saved, discard, { immediate: true });
 
    const sizeFixed = computed(() => (base.value?.tickets.filed ?? 0) > 0);
 
    const features = computed(() => {
        Iif (!base.value) return [];
        const { project, template } = base.value;
        return featuresOf(
            { ...project, switches: { ...project.switches, ...draft.switches } },
            template,
        );
    });
 
    const toggle = (capability: string, on: boolean) => {
        const current = base.value?.features.find((feature) => feature.id === capability)?.enabled;
        Iif (on === current) delete draft.switches[capability];
        else draft.switches[capability] = on;
    };
 
    const changed = computed(
        () =>
            Object.keys(draft.switches).length > 0 ||
            SAVED_KEYS.some((key) => draft[key] !== saved.value[key]),
    );
 
    const saving = ref(false);
    const save = async () => {
        saving.value = true;
        await edit({ action: "settings", project: id.value, ...draft });
        saving.value = false;
    };
 
    const { leaving, leave } = useLeaveGuard(() => changed.value, discard);
 
    const remove = async () => {
        Eif (await edit({ action: "delete", project: id.value })) {
            discard();
            await navigateTo("/workflows");
        }
    };
 
    return {
        id,
        base,
        editable,
        draft,
        sizeFixed,
        features,
        toggle,
        changed,
        saving,
        save,
        discard,
        leaving,
        leave,
        remove,
        ready: read,
    };
};