Files
pipelines/web/src/templates.js
T
alan b55de770fe web: create a pipeline from a template — validate then submit (P5)
A template detail now offers "Create pipeline from this template". Its
dialog ("Create from {name}", one required field "New pipeline name
(required)", the note that it creates a draft and does not run, Create /
Cancel with Cancel reading Close after a success) validates the
template's source first and, only when it parses, submits it create-only
with the trimmed name. Create is disabled while the name is blank or
while sending; the field locks while sending and after a success. A
refusal shows the server's own error in the still-open dialog; a success
shows `Created "{name}". It is in the Pipelines list.` and refreshes
list_pipelines so the new row is visible.

Pure helpers + both request shapes are unit-tested; the flow is captured
against a scratch server with its own throwaway database. The live
server's pipeline list is never touched: the default ui-smoke run sends
no submit and create runs only under its --scratch flag. Wording matches
android/.../TemplateDetailScreen.kt and PipelinesRepository.kt.
2026-10-19 06:06:05 +01:00

159 lines
6.3 KiB
JavaScript

// Pure Templates derivations, mirroring the Android app's ui/Templates.kt,
// ui/Format.kt and ui/graph/StageDetailPane.kt. No DOM and no protocol here, so
// the gallery/detail wording and the stage-count mapping are unit-tested without
// a browser. Every value returned is plain text (or plain data); the caller sets
// it with textContent and never with innerHTML.
/** A gallery card's name: a blank name is the explicit unnamed-template placeholder. */
export function templateGalleryName(name) {
return (name ?? "").trim() ? name : "(unnamed template)";
}
/** A detail's heading: a blank name falls back to the template's key. */
export function templateDetailName(entry) {
const name = (entry?.name ?? "").trim();
return name ? entry.name : (entry?.key ?? "");
}
/** "1 stage" / "N stages", mirroring ui/Format.kt stageCountText. */
export function stageCountText(count) {
return `${count} ${count === 1 ? "stage" : "stages"}`;
}
/**
* The card's stage-count line: the count parsed from the source, or an explicit
* "unavailable" when the source could not be parsed (never "0 stages", which
* would claim a count the client does not have).
*/
export function templateStageCountLabel(stageCount) {
return stageCount == null ? "stage count unavailable" : stageCountText(stageCount);
}
/** The effective stage type, mirroring internal/stage.EffectiveType: blank is the model stage. */
export function effectiveStageType(type) {
return (type ?? "").trim() ? type : "model";
}
/**
* One gallery card's content: the template's name and description (from the
* catalogue) and its stage count (from the graph the server parsed out of the
* source; null when the source did not parse, so the card says so rather than
* claiming zero stages).
*/
export function templateCard(entry, graph = null) {
return {
key: entry?.key ?? "",
name: entry?.name ?? "",
description: entry?.description ?? "",
stageCount: graph?.stages?.length ?? null,
};
}
/** The unparsed-source note the Summary tab shows instead of a stage list. */
export const TEMPLATE_UNPARSED_SUMMARY_NOTE =
"This template's source could not be parsed, so its stages are not listed. See the Source tab for the raw definition.";
/** The note the Summary tab shows for a parsed template that declares no stages. */
export const TEMPLATE_NO_STAGES_NOTE = "This template has no stages.";
/** The Graph tab's note when the source did not parse. */
export const TEMPLATE_UNPARSED_GRAPH_NOTE =
"This template's source could not be parsed, so there is no graph. The Source tab shows the raw definition.";
/** The Graph tab's note when the source parsed; the graph is the parsed definition. */
export const TEMPLATE_GRAPH_NOTE =
"Pinch to zoom and drag to pan. The graph is the parsed definition; nothing runs.";
/** The Source tab's note when the source did not parse on the server. */
export const TEMPLATE_UNPARSED_SOURCE_NOTE =
"This source could not be parsed on the server; showing the raw definition.";
/**
* The Summary tab's stage section: an explicit [note] and no stages when the
* source did not parse or parsed to nothing, else the stage rows (key + effective
* type). One derivation, so the note and the list can never disagree.
*/
export function templateStageSummary(graph) {
if (graph == null) {
return { note: TEMPLATE_UNPARSED_SUMMARY_NOTE, stages: [] };
}
const stages = graph.stages ?? [];
if (!stages.length) {
return { note: TEMPLATE_NO_STAGES_NOTE, stages: [] };
}
return {
note: null,
stages: stages.map((stage) => ({ key: stage.key, type: effectiveStageType(stage.type) })),
};
}
/** The Graph tab's note: the unparsed note when there is no graph, else the parsed-definition note. */
export function templateGraphNote(graph) {
return graph == null ? TEMPLATE_UNPARSED_GRAPH_NOTE : TEMPLATE_GRAPH_NOTE;
}
// --- Create a pipeline from this template (P5) -----------------------------
// The wording, verbatim from android/.../TemplateDetailScreen.kt's
// CreateFromTemplateDialog. The dialog validates the template's source first,
// then submits it create-only; nothing runs and an existing name is never
// overwritten.
/** The template-detail action that opens the create dialog. */
export const CREATE_ACTION_LABEL = "Create pipeline from this template";
/** The dialog's single required field label. */
export const CREATE_NAME_LABEL = "New pipeline name (required)";
/** The dialog's explanatory note (verbatim; states that nothing runs). */
export const CREATE_NOTE =
"Creates a new draft pipeline on the server from this template's source; it is not run.";
/** The confirm button's idle label. */
export const CREATE_CONFIRM = "Create";
/** The confirm button's in-flight label. */
export const CREATE_SENDING = "Creating…";
/** The dismiss button's label before a success. */
export const CREATE_CANCEL = "Cancel";
/** The dismiss button's label after a success. */
export const CREATE_CLOSE = "Close";
/** The dialog's title: `Create from {name}`, a blank name falling back to the key. */
export function createFormTitle(entry) {
return `Create from ${templateDetailName(entry)}`;
}
/**
* Whether the dialog can submit: not while sending, not once a pipeline has
* been created, and never with a blank (or whitespace-only) name.
*/
export function createFormValid(name, { sending = false, created = null } = {}) {
return !sending && created == null && String(name ?? "").trim() !== "";
}
/** The confirm button's label: "Creating…" while in flight, else "Create". */
export function createConfirmLabel(sending) {
return sending ? CREATE_SENDING : CREATE_CONFIRM;
}
/** The dismiss button's label: "Close" once a pipeline was created, else "Cancel". */
export function createDismissLabel(created) {
return created != null ? CREATE_CLOSE : CREATE_CANCEL;
}
/** The dialog's success line: `Created "{name}". It is in the Pipelines list.` */
export function createSuccessMessage(created) {
return `Created "${created}". It is in the Pipelines list.`;
}
/**
* The name to report after a successful submit: the created pipeline's own name
* when the server returned one, else the name the operator submitted (trimmed),
* mirroring `created = it.name.ifBlank { name.trim() }`.
*/
export function createdPipelineName(detail, submittedName) {
return String(detail?.name ?? "").trim() || String(submittedName ?? "").trim();
}