/** * The bindings vocabulary: the wire shapes the daemon returns, the draft the * editor form edits, and the status ramp the list renders. Pure, so it reads * (and would test) without the page's fetch wiring -- the same split * binding-editor.jsx had from bindings.jsx in the Preact build. */ /** * A binding as GET /api/bindings returns it. Secret is never returned: the * daemon strips it from every read, and a blank secret on update is its * server-side "keep the current one". */ export interface Binding { id: number; name: string; matcher?: { source?: string }; mapping_id?: number; workflow?: string; /** A pin is a complete owner/repo pair or not a pin at all. */ owner?: string; repo?: string; status?: string; version?: number; } /** The fields of GET /api/mappings this page needs. */ export interface MappingOption { id: number; name: string; } /** One entry of GET /api/workflows' `definitions`. */ export interface WorkflowOption { id: string; name: string; enabled?: boolean; } /** The Badge variants a binding status maps onto. */ export type StatusKind = "ok" | "warn" | "idle"; const STATUS_LABELS: Record = { draft: "draft", pending_approval: "pending approval", armed: "armed", }; const STATUS_KINDS: Record = { draft: "idle", pending_approval: "warn", armed: "ok", }; /** A status the daemon grows later renders as itself in the neutral kind, * rather than as an unlabelled pill. */ export function statusLabel(status?: string): string { if (!status) return "unknown"; return STATUS_LABELS[status] ?? status; } export function statusKind(status?: string): StatusKind { return STATUS_KINDS[status ?? ""] ?? "idle"; } /** * The editor's field state. mappingId is the select's string value because a * select's model is its option values; the store's mapping_id is a number. */ export interface BindingDraft { id: number | null; name: string; source: string; mappingId: string; workflow: string; owner: string; repo: string; secret: string; } export function emptyDraft(): BindingDraft { return { id: null, name: "", source: "", mappingId: "", workflow: "", owner: "", repo: "", secret: "", }; } export function draftFromBinding(binding: Binding): BindingDraft { return { id: binding.id, name: binding.name, source: binding.matcher?.source || "", mappingId: binding.mapping_id ? String(binding.mapping_id) : "", workflow: binding.workflow || "", owner: binding.owner || "", repo: binding.repo || "", // Deliberately blank: the secret is never sent to the browser, and an // empty one on update keeps whatever the sender already signs with. secret: "", }; } /** The body POST /api/bindings and PATCH /api/bindings/{id} both accept. A * blank mapping is 0, which the store rejects -- a binding with no mapping * could never be dispatched. */ export function bindingPayload(draft: BindingDraft): Record { return { name: draft.name, matcher: { source: draft.source }, mapping_id: Number(draft.mappingId) || 0, workflow: draft.workflow, owner: draft.owner, repo: draft.repo, secret: draft.secret, }; } /** The repo pin a row shows, or an em dash when the binding takes the * single-configured-repo default. */ export function repoPin(binding: Binding): string { return binding.owner && binding.repo ? `${binding.owner}/${binding.repo}` : "—"; } /** The matcher source a row shows: the path segment senders POST to. */ export function matcherSource(binding: Binding): string { return binding.matcher?.source || "—"; }