Files
archie/ui/src/mappings/MappingForm.vue
T
sam 30aa351eb6 feat(events,drafts): give sources an immutable id and a scope (#2127, #2027)
A source is keyed by a uuid, owned by the organisation or one person, and
labelled within that scope. The webhook path is a separate unique column, so
renaming either leaves stored events, event types, triggers, groups, mappings
and drafts alone. Delivery policies, sealed secrets and group keys use the id.
2026-11-24 02:43:51 +11:00

155 lines
5.1 KiB
Vue

<script setup lang="ts">
import { errors } from "@/copy/errors";
import { computed, onMounted } from "vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { api } from "@/lib/api";
import { ago } from "@/lib/format";
import { useRead } from "@/lib/use-read";
import { eventTypeLabel } from "@/captures/event-types";
import {
captures,
capturesEnabled,
capturesError,
draft,
eventTypes,
eventTypesError,
loadMappings,
selectCapture,
type Capture,
} from "./state";
/**
* The mapping's own fields: the name it is saved under, the organisational
* source, and the captured event the payload and the preview resolve
* against. The hint is a note to a human, never a matcher; bindings decide which
* events a mapping applies to.
*
* Each label is bound to its control by id so the association survives a click
* on either.
*/
/** The picker's value for "no capture chosen". Capture ids are numeric, so no
* real option can collide with this one, and reka-ui refuses an empty-string
* item value outright. */
const NO_CAPTURE = "none";
/** The picker's value for "no source". */
const NO_SOURCE = "none";
const sources = useRead(() => api.eventSources());
onMounted(() => void sources.reload());
const sourceValue = computed(() => draft.value.sourceId || NO_SOURCE);
function onSourceChange(value: unknown): void {
draft.value.sourceId = value === NO_SOURCE ? "" : String(value);
}
const captureValue = computed(() =>
draft.value.captureId === null ? NO_CAPTURE : String(draft.value.captureId),
);
function onCaptureChange(value: unknown): void {
selectCapture(String(value));
}
function captureLabel(capture: Capture): string {
return `${capture.source || "(unknown)"} · ${ago(capture.received_at)}`;
}
/** What an empty picker means, which is not the same on every deployment.
* A failed read is not an empty store, so it shows no hint here -- the error
* row below says so and offers Retry. */
const captureHint = computed(() => {
if (capturesError.value || captures.value.length) return null;
return capturesEnabled.value
? "No captured events yet."
: "Capture is not configured on this deployment.";
});
</script>
<template>
<div class="flex flex-col gap-4">
<div class="flex flex-col gap-2">
<Label for="mapping-name">Name</Label>
<Input id="mapping-name" v-model="draft.name" />
</div>
<div class="flex flex-col gap-2">
<Label for="mapping-event-type">Event type</Label>
<Select v-model="draft.eventTypeId">
<SelectTrigger id="mapping-event-type" class="w-full">
<SelectValue placeholder="Pick an event type" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem
v-for="type in eventTypes"
:key="type.id"
:value="type.id"
>
{{ eventTypeLabel(type.id, eventTypes) }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
<p v-if="eventTypesError" class="flex items-center gap-2 text-caption text-danger">
{{ errors.mappings.form.eventTypesNotLoaded }}
<Button variant="link" size="xs" class="px-0" @click="loadMappings">Retry</Button>
</p>
</div>
<div class="flex flex-col gap-2">
<Label for="mapping-source">Source (organisational only)</Label>
<Select :model-value="sourceValue" @update:model-value="onSourceChange">
<SelectTrigger id="mapping-source" class="w-full"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem :value="NO_SOURCE">No source</SelectItem>
<SelectItem v-for="s in sources.data.value ?? []" :key="s.id" :value="s.id">{{ s.label }}</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>
<div class="flex flex-col gap-2">
<Label for="mapping-capture">Preview against captured event</Label>
<Select :model-value="captureValue" @update:model-value="onCaptureChange">
<SelectTrigger id="mapping-capture" class="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem :value="NO_CAPTURE"
>Pick a captured event</SelectItem
>
<SelectItem
v-for="capture in captures"
:key="capture.id"
:value="String(capture.id)"
>
{{ captureLabel(capture) }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
<p v-if="captureHint" class="text-caption text-fg-subtle">{{ captureHint }}</p>
<p v-if="capturesError" class="flex items-center gap-2 text-caption text-danger">
{{ errors.mappings.form.capturesNotLoaded }}
<Button variant="link" size="xs" class="px-0" @click="loadMappings">Retry</Button>
</p>
</div>
</div>
</template>