import { ref } from "vue"; import { api, ApiError, describeError } from "@/lib/api"; import { errors } from "@/copy/errors"; import type { EventType } from "@/captures/event-types"; import { bindingOfTrigger, bindingPayload, type Binding, type BindingDraft, type MappingOption, type WorkflowOption, } from "./binding-trigger"; /** * Why the page has nothing to show. "unconfigured" is a server that doesn't * serve triggers (501/503), which reads very differently from one that * answered badly. */ type LoadFailureKind = "unconfigured" | "failed"; export interface LoadFailure { kind: LoadFailureKind; message: string; } function failureFor(err: unknown): LoadFailure { const message = describeError(err, errors.common.unknown); const status = err instanceof ApiError ? err.status : undefined; return { kind: status === 501 || status === 503 ? "unconfigured" : "failed", message, }; } /** * The bindings page's data: the lists the table and the editor read, the load * that fills them, and the mutations. Every mutation goes through api.* so the * CSRF and Content-Type headers are sent. */ export function useBindings() { // null means loading, [] means loaded and empty. const bindings = ref(null); const mappings = ref([]); const eventTypes = ref([]); const workflows = ref([]); const failure = ref(null); // A failed mutation is not a failed load: the list on screen is still // accurate, so it keeps rendering and the failure lands beside it. const actionFailure = ref(null); const saving = ref(false); const saveFailure = ref(null); async function load(): Promise { failure.value = null; try { const [triggers, m, w, t] = await Promise.all([ api.triggers(), api.mappings<{ mappings?: MappingOption[] }>(), api.workflowList(), api.eventTypes<{ event_types?: EventType[] }>(), ]); bindings.value = triggers.filter((x) => x.event_type_id).map(bindingOfTrigger); mappings.value = m.mappings || []; eventTypes.value = t.event_types || []; workflows.value = w.map((d) => ({ id: d.id, name: d.name })); } catch (err) { failure.value = failureFor(err); bindings.value = null; } } function resetSaveFailure(): void { saveFailure.value = null; } /** True when the binding was written, so the caller knows to close. */ async function save(draft: BindingDraft): Promise { saving.value = true; saveFailure.value = null; try { await api.putTrigger(draft.id ?? draft.name, bindingPayload(draft, eventTypes.value)); await load(); return true; } catch (err) { saveFailure.value = describeError(err, errors.common.unknown); return false; } finally { saving.value = false; } } async function transition(command: (id: string) => Promise, binding: Binding): Promise { actionFailure.value = null; try { await command(binding.id); await load(); } catch (err) { actionFailure.value = failureFor(err); } } const approve = (binding: Binding) => transition(api.approveTrigger, binding); const pause = (binding: Binding) => transition(api.pauseTrigger, binding); const resume = (binding: Binding) => transition(api.resumeTrigger, binding); const remove = (binding: Binding) => transition(api.deleteTrigger, binding); return { bindings, mappings, eventTypes, workflows, failure, actionFailure, saving, saveFailure, load, resetSaveFailure, save, approve, pause, resume, remove, }; }