components/ui and the pages now use only the style.css scale: text-[0.8rem]→text-label, text-[0.625rem]→text-micro, text-base/text-sm→ text-label (rows and controls), text-xs→text-caption (captions and secondary text), text-lg/text-xl/text-2xl→text-title (panel and page titles). Rendered markdown (ChatMarkdown/ChatInline/markdown.ts) keeps Tailwind steps as the issue allows. 196 files, class-token swaps only.
232 lines
9.6 KiB
Vue
232 lines
9.6 KiB
Vue
<script setup lang="ts">
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
|
import { Label } from "@/components/ui/label";
|
|
import { computed, onMounted, ref } from "vue";
|
|
import { ArrowLeft } from "@lucide/vue";
|
|
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { api } from "@/lib/api";
|
|
import { constantValue, statusKind, statusLabel, type Binding, type InputSource } from "@/bindings/binding-draft";
|
|
import ScheduleEditor from "@/schedules/ScheduleEditor.vue";
|
|
import { type Schedule } from "@/schedules/schedule";
|
|
import type { InputSpec } from "./bind-event";
|
|
import OptionSelect from "./OptionSelect.vue";
|
|
import { workflowField } from "./workflow-edit";
|
|
import { SCHEDULE_FIELDS, SCHEDULE_MAPPING, SCHEDULE_SOURCE, scheduleFilter, scheduleOfBinding } from "./workflow-triggers";
|
|
|
|
/**
|
|
* A schedule trigger: a binding on the schedule source filtered to one
|
|
* schedule, each workflow input read from the firing or given a fixed value.
|
|
*/
|
|
const props = defineProps<{ yaml: string; binding: Binding | null }>();
|
|
const emit = defineEmits<{ back: []; saved: [] }>();
|
|
|
|
const NEW = "__new__";
|
|
const FIXED = "__fixed__";
|
|
const workflow = computed(() => String(workflowField(props.yaml, "id") ?? ""));
|
|
const inputs = computed(() =>
|
|
Object.entries((workflowField(props.yaml, "inputs") as Record<string, InputSpec> | undefined) ?? {}),
|
|
);
|
|
|
|
const schedules = ref<Schedule[]>([]);
|
|
const loading = ref(true);
|
|
const loadError = ref("");
|
|
const scheduleId = ref(props.binding ? scheduleOfBinding(props.binding) : "");
|
|
const rows = ref<Record<string, { from: string; value: string }>>({});
|
|
const status = ref(props.binding?.status ?? "");
|
|
const saving = ref(false);
|
|
const error = ref("");
|
|
const confirmRemove = ref(false);
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
schedules.value = (await api.schedules<{ schedules?: Schedule[] }>()).schedules ?? [];
|
|
} catch (err) {
|
|
loadError.value = err instanceof Error ? err.message : String(err);
|
|
}
|
|
for (const [name] of inputs.value) {
|
|
const src = props.binding?.inputs?.[name];
|
|
rows.value[name] = src?.param
|
|
? { from: src.param, value: "" }
|
|
: src && src.value !== undefined
|
|
? { from: FIXED, value: typeof src.value === "string" ? src.value : JSON.stringify(src.value) }
|
|
: { from: FIXED, value: "" };
|
|
}
|
|
loading.value = false;
|
|
});
|
|
|
|
const scheduleOptions = computed(() => [
|
|
{ value: "", label: "Pick a schedule", disabled: true },
|
|
...schedules.value.map((s) => ({ value: s.id, label: s.name })),
|
|
{ value: NEW, label: "New schedule…" },
|
|
]);
|
|
const fieldOptions = [
|
|
...SCHEDULE_FIELDS.map((field) => ({ value: field, label: field })),
|
|
{ value: FIXED, label: "Fixed value" },
|
|
];
|
|
|
|
// Creating a schedule here writes the same record the Schedules page lists.
|
|
const editorOpen = ref(false);
|
|
const draft = ref<Schedule | null>(null);
|
|
function pickSchedule(value: string): void {
|
|
if (value !== NEW) {
|
|
scheduleId.value = value;
|
|
return;
|
|
}
|
|
draft.value = {
|
|
id: crypto.randomUUID(),
|
|
name: workflow.value,
|
|
owner: { kind: "instance" },
|
|
enabled: true,
|
|
built_in: false,
|
|
timing: { kind: "interval", interval: "24h0m0s" },
|
|
};
|
|
editorOpen.value = true;
|
|
}
|
|
function created(schedule: Schedule): void {
|
|
schedules.value.push(schedule);
|
|
scheduleId.value = schedule.id;
|
|
}
|
|
|
|
const schedule = computed(() => schedules.value.find((s) => s.id === scheduleId.value));
|
|
const blockers = computed(() => {
|
|
if (!scheduleId.value) return ["pick a schedule"];
|
|
return inputs.value
|
|
.filter(([name, spec]) => spec.required && rows.value[name]?.from === FIXED && rows.value[name]?.value === "")
|
|
.map(([name]) => `${name} is required`);
|
|
});
|
|
|
|
function bindingInputs(): Record<string, InputSource> {
|
|
const out: Record<string, InputSource> = {};
|
|
for (const [name, spec] of inputs.value) {
|
|
const row = rows.value[name];
|
|
if (!row) continue;
|
|
if (row.from !== FIXED) out[name] = { param: row.from };
|
|
else if (row.value !== "") out[name] = { value: constantValue(row.value, spec.type ?? "string") };
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function save(): Promise<void> {
|
|
if (blockers.value.length) return;
|
|
saving.value = true;
|
|
error.value = "";
|
|
try {
|
|
const body = {
|
|
name: `${schedule.value?.name ?? scheduleId.value} → ${workflow.value}`,
|
|
matcher: { source: SCHEDULE_SOURCE },
|
|
mapping_id: SCHEDULE_MAPPING,
|
|
filter: scheduleFilter(scheduleId.value),
|
|
workflow: workflow.value,
|
|
repo_param: "",
|
|
owner: props.binding?.owner ?? "",
|
|
repo: props.binding?.repo ?? "",
|
|
inputs: bindingInputs(),
|
|
};
|
|
if (props.binding) await api.bindingUpdate(props.binding.id, body);
|
|
else await api.bindingCreate(body);
|
|
emit("saved");
|
|
} catch (err) {
|
|
error.value = err instanceof Error ? err.message : String(err);
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
|
|
async function transition(command: (id: string) => Promise<unknown>, next: string): Promise<void> {
|
|
if (!props.binding) return;
|
|
error.value = "";
|
|
try {
|
|
await command(props.binding.id);
|
|
status.value = next;
|
|
emit("saved");
|
|
} catch (err) {
|
|
error.value = err instanceof Error ? err.message : String(err);
|
|
}
|
|
}
|
|
async function remove(): Promise<void> {
|
|
if (!props.binding) return;
|
|
try {
|
|
await api.bindingDelete(props.binding.id);
|
|
emit("saved");
|
|
} catch (err) {
|
|
error.value = err instanceof Error ? err.message : String(err);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="flex h-full flex-col">
|
|
<AlertDialog v-model:open="confirmRemove">
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Remove schedule trigger?</AlertDialogTitle>
|
|
<AlertDialogDescription>The schedule keeps running; it stops starting this workflow.</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction @click="remove">Remove trigger</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
<header class="flex items-center gap-2 border-b border-border px-4 py-3">
|
|
<Button type="button" variant="ghost" size="icon-sm" aria-label="Back to settings" @click="emit('back')"><ArrowLeft /></Button>
|
|
<div class="min-w-0 flex-1 truncate text-label font-medium">{{ schedule?.name ?? "Add a schedule" }}</div>
|
|
<Badge v-if="status" :variant="statusKind(status)">{{ statusLabel(status) }}</Badge>
|
|
</header>
|
|
|
|
<div v-if="loading" class="flex flex-1 items-center justify-center"><Spinner /></div>
|
|
<form v-else class="flex-1 space-y-5 overflow-y-auto px-4 py-4" @submit.prevent="save">
|
|
<section class="space-y-1.5">
|
|
<Label for="schedule-trigger" class="text-caption font-medium text-fg-muted">Schedule</Label>
|
|
<Alert v-if="loadError" variant="destructive"><AlertDescription>{{ loadError }}</AlertDescription></Alert>
|
|
<OptionSelect v-else id="schedule-trigger" aria-label="Schedule" :model-value="scheduleId" :options="scheduleOptions" class="w-full" @update:model-value="pickSchedule" />
|
|
</section>
|
|
|
|
<section class="space-y-1.5">
|
|
<div class="text-caption font-medium text-fg-muted">Inputs</div>
|
|
<div class="divide-y divide-border rounded-md border border-border">
|
|
<div v-for="[name, spec] in inputs" :key="name" class="space-y-1 px-3 py-2">
|
|
<div class="flex items-center gap-1.5 text-caption">
|
|
<span class="font-mono">{{ name }}</span>
|
|
<span class="text-fg-subtle">{{ spec.type ?? "string" }}</span>
|
|
<span v-if="spec.required" class="text-danger" title="Required">*</span>
|
|
</div>
|
|
<OptionSelect :aria-label="`${name} source`" :model-value="rows[name]!.from" :options="fieldOptions" class="w-full" @update:model-value="rows[name]!.from = $event" />
|
|
<Input v-if="rows[name]!.from === FIXED" v-model="rows[name]!.value" :aria-label="`${name} fixed value`" placeholder="fixed value" class="h-8 w-full font-mono text-caption" />
|
|
</div>
|
|
<Empty v-if="!inputs.length"><EmptyHeader><EmptyTitle>No inputs.</EmptyTitle></EmptyHeader></Empty>
|
|
</div>
|
|
</section>
|
|
|
|
<div class="space-y-2">
|
|
<Alert v-if="error" variant="destructive"><AlertDescription>{{ error }}</AlertDescription></Alert>
|
|
<p v-else-if="blockers.length" class="text-caption text-fg-subtle">Cannot save: {{ blockers[0] }}</p>
|
|
<div class="flex flex-wrap gap-2">
|
|
<Button type="submit" size="sm" :disabled="saving || !!blockers.length"><Spinner v-if="saving" />Save trigger</Button>
|
|
<Button v-if="binding && status === 'pending_approval'" type="button" size="sm" variant="outline" @click="transition(api.bindingApprove, 'armed')">Approve</Button>
|
|
<Button v-if="binding && status === 'armed'" type="button" size="sm" variant="outline" @click="transition(api.bindingPause, 'paused')">Pause</Button>
|
|
<Button v-if="binding && status === 'paused'" type="button" size="sm" variant="outline" @click="transition(api.bindingResume, 'pending_approval')">Resume</Button>
|
|
<Button v-if="binding" type="button" size="sm" variant="destructive" @click="confirmRemove = true">Remove trigger</Button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<ScheduleEditor v-model:open="editorOpen" :schedule="draft" title="New schedule" @saved="created" />
|
|
</div>
|
|
</template>
|