Files
archie/ui/src/bindings/BindingEditor.vue
T
sam 5c8fb901f9 feat(ui/events): captures, mappings and bindings run on the binary's API (#2062, #2098)
The Capture, Map and Bind steps read the module's captures, event types,
mappings and triggers. A binding is a trigger bound to an event type; the
sources panel creates HMAC sources and shows each delivery's verification
result. The workspace filter is removed: the binary has one workspace per
organisation and the data carries none.
2026-11-22 11:46:02 +11:00

204 lines
6.5 KiB
Vue

<script setup lang="ts">
import { errors } from "@/copy/errors";
import TriggerRuns from "./TriggerRuns.vue";
import { computed, ref, watch } from "vue";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { eventTypeLabel, type EventType } from "@/captures/event-types";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Spinner } from "@/components/ui/spinner";
import {
draftFromBinding,
emptyDraft,
mappingsForEventType,
type Binding,
type BindingDraft,
type MappingOption,
type WorkflowOption,
} from "./binding-trigger";
/**
* The binding editor: name, the event type it applies to, an optional mapping
* of that type, and the workflow it starts. Signing is the source's.
*/
const props = defineProps<{
/** null is a new binding; a binding is an edit. */
binding: Binding | null;
mappings: MappingOption[];
eventTypes: EventType[];
workflows: WorkflowOption[];
saving: boolean;
error: string | null;
}>();
const open = defineModel<boolean>("open", { required: true });
const emit = defineEmits<{ save: [draft: BindingDraft] }>();
// The draft is re-seeded on every open, so an edit that was cancelled -- or a
// save that failed -- never leaks into the next binding's form.
const draft = ref<BindingDraft>(emptyDraft());
watch(
open,
(isOpen) => {
if (isOpen)
draft.value = props.binding ? draftFromBinding(props.binding) : emptyDraft();
},
{ immediate: true },
);
const typeMappings = computed(() =>
mappingsForEventType(props.mappings, draft.value.eventTypeId),
);
// A mapping belongs to one event type, so changing the type clears a mapping
// that no longer fits it.
watch(
() => draft.value.eventTypeId,
() => {
if (!typeMappings.value.some((m) => m.id === draft.value.mappingId))
draft.value.mappingId = "";
},
);
// Select items cannot carry an empty value.
const NO_MAPPING = "__none__";
const mappingChoice = computed({
get: () => draft.value.mappingId || NO_MAPPING,
set: (v: string) => {
draft.value.mappingId = v === NO_MAPPING ? "" : v;
},
});
const title = computed(() => (props.binding ? "Edit binding" : "New binding"));
</script>
<template>
<Dialog v-model:open="open">
<DialogContent class="max-h-[85vh] overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>{{ title }}</DialogTitle>
<DialogDescription>Saving returns it to pending approval.</DialogDescription>
</DialogHeader>
<form @submit.prevent="emit('save', draft)">
<FieldGroup>
<Field>
<FieldLabel for="binding-name">Name</FieldLabel>
<Input
id="binding-name"
v-model="draft.name"
:disabled="!!props.binding"
pattern="[a-z][a-z0-9\-]*"
required
/>
</Field>
<Field>
<FieldLabel for="binding-event-type">Event type</FieldLabel>
<Select v-model="draft.eventTypeId">
<SelectTrigger id="binding-event-type" class="w-full">
<SelectValue placeholder="Pick an event type" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem
v-for="type in props.eventTypes"
:key="type.id"
:value="type.id"
>
{{ eventTypeLabel(type.id, props.eventTypes) }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
<Field>
<FieldLabel for="binding-mapping">Field mapping</FieldLabel>
<Select v-model="mappingChoice" :disabled="!draft.eventTypeId">
<SelectTrigger id="binding-mapping" class="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem :value="NO_MAPPING">None</SelectItem>
<SelectItem
v-for="mapping in typeMappings"
:key="mapping.id"
:value="String(mapping.id)"
>
{{ mapping.name }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
<Field>
<FieldLabel for="binding-workflow">Workflow</FieldLabel>
<Select v-model="draft.workflow">
<SelectTrigger id="binding-workflow" class="w-full">
<SelectValue placeholder="Pick a workflow" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem
v-for="workflow in props.workflows"
:key="workflow.id"
:value="workflow.id"
>
{{ workflow.name }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
</FieldGroup>
<section v-if="props.binding" class="mt-4 flex flex-col gap-2">
<h3 class="text-caption font-medium tracking-[0.06em] text-fg-subtle uppercase">Recent runs</h3>
<TriggerRuns kind="triggers" :id="props.binding.id" />
</section>
<!-- Beside the buttons, not at the top: the dialog scrolls, and Save is
clicked at the bottom, where a refusal must be seen. -->
<Alert v-if="props.error" variant="destructive" class="mt-4">
<AlertTitle>{{ errors.bindings.editor.saveFailed }}</AlertTitle>
<AlertDescription>{{ props.error }}</AlertDescription>
</Alert>
<DialogFooter class="mt-4">
<Button type="button" variant="outline" @click="open = false"
>Cancel</Button
>
<Button
type="submit"
:disabled="props.saving || !draft.eventTypeId || !draft.workflow"
>
<Spinner v-if="props.saving" data-icon="inline-start" />{{ props.saving ? "Saving" : "Save" }}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</template>