web: templates gallery and template detail (P4)

This commit is contained in:
2026-10-19 02:30:39 +01:00
parent 4721b8af10
commit da3c488506
9 changed files with 586 additions and 2 deletions
+209 -1
View File
@@ -4,6 +4,16 @@
import { PipelinesClient } from "./client.js";
import { PipelinesRepository } from "./repository.js";
import { validateServerUrl, resolveArtifactUrl } from "./serverUrl.js";
import {
effectiveStageType,
templateCard,
templateDetailName,
templateGalleryName,
templateGraphNote,
templateStageCountLabel,
templateStageSummary,
TEMPLATE_UNPARSED_SOURCE_NOTE,
} from "./templates.js";
const STORAGE_KEY = "pipelines.web.serverUrl.v2";
@@ -43,6 +53,13 @@ const state = {
runDetail: null,
selectedPipelineId: null,
selectedRunId: null,
templates: [],
templateLoading: false,
// key -> graph|null: the parsed definition, cached once per template key. A
// failed/absent validate stores null so the card/detail say "unavailable".
templateGraphs: new Map(),
selectedTemplateKey: null,
templateTab: "summary",
loading: false,
error: "",
};
@@ -168,6 +185,7 @@ function renderContent() {
{ class: "tabs" },
tabButton("pipelines", `Pipelines (${state.pipelines.length})`),
tabButton("runs", `Runs (${state.runs.length})`),
tabButton("templates", "Templates"),
h("span", { class: "spacer" }),
h("button", { class: "ghost", text: "Refresh", onclick: () => loadCurrentView(true) }),
),
@@ -176,8 +194,10 @@ function renderContent() {
const split = h("div", { class: "split" });
if (state.view === "pipelines") {
split.append(renderPipelineList(), renderPipelineDetail());
} else {
} else if (state.view === "runs") {
split.append(renderRunList(), renderRunDetail());
} else {
split.append(renderTemplateGallery(), renderTemplateDetail());
}
nodes.push(split);
content.replaceChildren(...nodes);
@@ -193,6 +213,7 @@ function tabButton(view, label) {
renderContent();
if (view === "runs" && !state.runs.length) loadCurrentView();
if (view === "pipelines" && !state.pipelines.length) loadCurrentView();
if (view === "templates" && !state.templates.length) loadCurrentView();
},
});
}
@@ -264,6 +285,140 @@ function renderPipelineDetail() {
return pane;
}
// --- Templates -------------------------------------------------------------
/** The cached parsed graph for a template key, or null when it did not parse. */
function templateGraphFor(key) {
return state.templateGraphs.has(key) ? state.templateGraphs.get(key) : null;
}
function renderTemplateGallery() {
const pane = h("div", { class: "list gallery" });
pane.append(
h(
"div",
{ class: "pane-head" },
h("h2", { text: "Templates" }),
h("button", { class: "ghost", text: "Refresh templates", onclick: () => loadTemplates(true) }),
),
);
if (state.templateLoading && !state.templates.length) {
pane.append(h("div", { class: "muted pad", text: "Loading templates…" }));
return pane;
}
if (!state.templates.length) {
pane.append(h("div", { class: "muted pad", text: "No templates are available on this server." }));
return pane;
}
const cards = h("div", { class: "cards" });
for (const entry of state.templates) {
const card = templateCard(entry, templateGraphFor(entry.key));
cards.append(
h(
"button",
{
class: `card${state.selectedTemplateKey === entry.key ? " selected" : ""}`,
onclick: () => selectTemplate(entry.key),
},
h("span", { class: "card-name", text: templateGalleryName(card.name) }),
h("span", { class: "card-desc", text: card.description }),
h("span", { class: "card-meta", text: templateStageCountLabel(card.stageCount) }),
),
);
}
pane.append(cards);
return pane;
}
function renderTemplateDetail() {
const pane = h("div", { class: "detail" });
const entry = state.templates.find((t) => t.key === state.selectedTemplateKey);
if (!entry) {
pane.append(h("div", { class: "muted pad", text: "Select a template." }));
return pane;
}
const graph = templateGraphFor(entry.key);
pane.append(
h(
"div",
{ class: "tabstrip" },
templateTabButton("summary", "Summary"),
templateTabButton("graph", "Graph"),
templateTabButton("source", "Source"),
),
);
if (state.templateTab === "graph") pane.append(renderTemplateGraphTab(graph));
else if (state.templateTab === "source") pane.append(renderTemplateSourceTab(entry, graph));
else pane.append(renderTemplateSummaryTab(entry, graph));
return pane;
}
function templateTabButton(tab, label) {
return h("button", {
class: `minitab${state.templateTab === tab ? " active" : ""}`,
text: label,
onclick: () => {
state.templateTab = tab;
renderContent();
},
});
}
function renderTemplateSummaryTab(entry, graph) {
const wrap = h("div", { class: "tabbody" });
wrap.append(h("h2", { text: templateDetailName(entry) }));
if ((entry.description ?? "").trim()) {
wrap.append(h("p", { class: "body", text: entry.description }));
}
wrap.append(h("div", { class: "kv" }, kvMono("Key", entry.key)));
wrap.append(
h("div", { class: "meta-line", text: templateStageCountLabel(graph?.stages?.length ?? null) }),
);
if (entry.scheduled) wrap.append(h("div", { class: "meta-line", text: "Includes a schedule" }));
const summary = templateStageSummary(graph);
if (summary.note) {
wrap.append(h("div", { class: "muted pad", text: summary.note }));
return wrap;
}
wrap.append(h("h3", { text: "Stages" }));
const ul = h("ul", { class: "stages" });
for (const stage of summary.stages) {
ul.append(
h(
"li",
{},
h("code", { text: stage.key }),
h("span", { class: "chip", text: effectiveStageType(stage.type) }),
),
);
}
wrap.append(ul);
return wrap;
}
function renderTemplateGraphTab(graph) {
const wrap = h("div", { class: "tabbody" });
wrap.append(h("p", { class: "muted", text: templateGraphNote(graph) }));
return wrap;
}
function renderTemplateSourceTab(entry, graph) {
const wrap = h("div", { class: "tabbody" });
if (graph == null) {
wrap.append(h("p", { class: "muted", text: TEMPLATE_UNPARSED_SOURCE_NOTE }));
}
wrap.append(
h(
"div",
{ class: "codeblock" },
h("div", { class: "codeblock-label", text: "Definition source" }),
h("pre", { class: "codeblock-body", text: entry.source ?? "" }),
),
);
return wrap;
}
function renderRunList() {
const list = h("div", { class: "list" });
if (state.usage) {
@@ -401,6 +556,16 @@ function kv(key, value) {
);
}
/** A key/value row whose value is monospace (a key, an id) rather than prose. */
function kvMono(key, value) {
return h(
"div",
{ class: "kv-row" },
h("span", { class: "kv-key", text: key }),
h("span", { class: "kv-val" }, h("code", { text: value })),
);
}
// ---------------------------------------------------------------------------
// Actions
// ---------------------------------------------------------------------------
@@ -445,6 +610,10 @@ async function loadCurrentView(force = false) {
renderContent();
return;
}
if (state.view === "templates") {
await loadTemplates(force);
return;
}
state.loading = true;
state.error = "";
renderContent();
@@ -467,6 +636,45 @@ async function loadCurrentView(force = false) {
}
}
/**
* Loads the template catalogue, then validates each template's source once and
* caches the parsed graph (null when it did not parse) so a card's stage count
* and the detail's stage list read the same result and can never disagree.
*/
async function loadTemplates(force = false) {
state.templateLoading = true;
state.error = "";
renderContent();
try {
state.templates = await repo.listTemplates();
for (const entry of state.templates) {
if (force || !state.templateGraphs.has(entry.key)) {
state.templateGraphs.set(entry.key, await validateTemplate(entry));
}
}
state.templateLoading = false;
renderContent();
} catch (err) {
state.templateLoading = false;
setError(err);
}
}
/** The parsed graph for a template, or null when the server cannot parse its source. */
async function validateTemplate(entry) {
try {
return await repo.validate(entry.source);
} catch {
return null;
}
}
function selectTemplate(key) {
state.selectedTemplateKey = key;
state.templateTab = "summary";
renderContent();
}
async function selectPipeline(summary) {
state.selectedPipelineId = summary.id;
state.pipelineDetail = null;