Files
archie/ui/src/events/EventDetail.vue
T

78 lines
3.4 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { RouterLink, useRouter } from "vue-router";
import CodeView from "@/base/CodeView.vue";
import { Button } from "@/components/ui/button";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { ReadError } from "@/components/ui/read-error";
import { Skeleton } from "@/components/ui/skeleton";
import { errors } from "@/copy/errors";
import { api, describeError, type EventGroup } from "@/lib/api";
import { emptyValue } from "@/lib/format";
import { useRead } from "@/lib/use-read";
/** One event's payload and the runs it started. */
const props = defineProps<{ id: string }>();
const router = useRouter();
const { data, error, reload } = useRead(() => api.event(props.id));
const groups = useRead(() => api.eventGroups());
onMounted(() => {
void reload();
void groups.reload();
});
const body = computed(() => (emptyValue(data.value?.body) ? "" : JSON.stringify(data.value?.body, null, 2)));
const started = computed(() => (data.value?.runs ?? []).filter((run) => run.run_id));
const blocked = computed(() => (data.value?.runs ?? []).filter((run) => !run.run_id));
const matchingGroup = computed<EventGroup | undefined>(() => {
const d = data.value;
if (!d || d.matched) return undefined;
return (groups.data.value ?? []).find(
(g) =>
g.proposed.declared_event_type === d.declared_event_type &&
(!d.source_id || g.proposed.source_id === d.source_id),
);
});
const proposing = ref(false);
const proposeError = ref("");
async function propose(): Promise<void> {
if (!matchingGroup.value || proposing.value) return;
proposing.value = true;
proposeError.value = "";
try {
const proposal = await api.proposeForGroup(matchingGroup.value.id);
await router.push(`/proposals/${proposal.id}`);
} catch (cause) {
proposeError.value = describeError(cause, errors.common.unknown);
} finally {
proposing.value = false;
}
}
</script>
<template>
<ReadError v-if="error" :message="error" retry-label="Retry" @retry="reload" />
<Skeleton v-else-if="!data" class="h-24 w-full" />
<div v-else class="space-y-2" @click.stop>
<p v-if="started.length" class="text-caption text-fg-muted">
Started
<template v-for="(run, i) in started" :key="run.run_id"><template v-if="i">, </template><RouterLink class="underline" :to="`/runs/${run.run_id}`">a run</RouterLink> through {{ run.trigger }}</template>
</p>
<ul v-if="blocked.length" class="space-y-1 text-caption text-fg-muted">
<li v-for="run in blocked" :key="run.trigger_id">
Trigger <span class="font-medium">{{ run.trigger }}</span> did not start ({{ run.outcome }})<template v-if="run.reason?.detail">: {{ run.reason.detail }}</template>
</li>
</ul>
<div v-else-if="!started.length && data.matched === false" class="flex flex-wrap items-center gap-2 text-caption text-fg-muted">
<span>No triggers matched this event.</span>
<Button v-if="matchingGroup" size="xs" variant="outline" :disabled="proposing" @click="propose">
{{ proposing ? "Proposing…" : "Propose workflow" }}
</Button>
</div>
<p v-if="proposeError" role="alert" class="text-caption text-danger">{{ proposeError }}</p>
<CodeView v-if="body" :code="body" lang="json" />
<Empty v-else><EmptyHeader><EmptyTitle>No payload</EmptyTitle></EmptyHeader></Empty>
</div>
</template>