// Schedules tab: what's coming up, every schedule as a card, and the preset picker. import {get, post, del, printJob, sources} from "./api.js"; import {h, clear, fmtTime, relative, toast} from "./dom.js"; import {PRESETS} from "./presets.js"; export const STATUS_LABELS = { queued: "Queued", printing: "Printing", done: "Printed", failed: "Failed", skipped: "Skipped", missed: "Missed", error: "Problem", retrying: "Retrying", held: "Held", watching: "Watching", finished: "Finished", }; const STATUS_CLASS = {done: "ok", watching: "ok", failed: "err", error: "err", missed: "warn", skipped: "warn", retrying: "warn", held: "warn"}; export const TRIGGER_ICONS = {once: "◷", recurring: "↻", sun: "☀", calendar: "▦", watch: "◉", webhook: "⚡"}; // Set by the editor so a preset or a duplicate opens pre-filled. export const pending = {draft: null}; export async function schedulesView(view) { const list = h("div", {class: "cards"}); const coming = h("div", {class: "coming"}); clear(view, h("div", {class: "toolbar"}, h("h2", {}, "Schedules"), h("button", {class: "primary", onclick: () => showPresets(view)}, "New schedule")), coming, list); const [r, up] = await Promise.all([get("api/schedules"), get("api/upcoming?hours=24")]); if (!r.ok) return clear(list, h("p", {class: "err"}, r.text)); const schedules = r.body.schedules; if (up.ok && up.body.upcoming.length) { clear(coming, h("h3", {class: "small muted"}, "Next 24 hours"), h("div", {class: "timeline"}, up.body.upcoming.slice(0, 12).map((u) => h("a", {class: "tick", href: `#schedules/${u.schedule_id}`}, h("strong", {}, new Date(u.at).toLocaleTimeString([], {hour: "2-digit", minute: "2-digit"})), h("span", {}, u.name))))); } if (!schedules.length) { clear(list, h("div", {class: "empty"}, h("p", {}, "Nothing scheduled yet. Pick a starting point:"), presetGrid())); return; } clear(list, schedules.map((s) => card(s, () => schedulesView(view)))); } function card(s, reload) { const type = s.spec?.trigger?.type; const toggle = h("input", {type: "checkbox", class: "switch", checked: s.enabled, "aria-label": s.enabled ? "Turn off" : "Turn on", onchange: async () => { const r = await post(`api/schedules/${s.id}/toggle`, {enabled: toggle.checked}); if (!r.ok) { toggle.checked = !toggle.checked; return toast(r.text, "err"); } toast(toggle.checked ? `${s.name} is on.` : `${s.name} is off.`); reload(); }}); const next = s.enabled && s.next_run_at ? `Next: ${fmtTime(s.next_run_at)} (${relative(s.next_run_at)})` : s.enabled ? (type === "webhook" ? "Waits for its webhook" : "") : "Off"; const status = s.last_status ? h("span", {class: `badge ${STATUS_CLASS[s.last_status] || ""}`, title: s.last_message || ""}, STATUS_LABELS[s.last_status] || s.last_status) : null; const busy = h("span", {class: "muted small"}); async function testPrint() { const result = await printJob(() => post(`api/schedules/${s.id}/run`), (text) => (busy.textContent = text)); busy.textContent = ""; toast(result.text, result.ok ? "" : "err"); } async function duplicate() { const r = await post(`api/schedules/${s.id}/duplicate`); if (!r.ok) return toast(r.text, "err"); location.hash = `#schedules/${r.body.schedule.id}`; } async function remove() { if (!confirm(`Delete "${s.name}"?`)) return; const r = await del(`api/schedules/${s.id}`); if (!r.ok) return toast(r.text, "err"); toast("Deleted."); reload(); } return h("article", {class: `card schedule ${s.enabled ? "" : "off"}`}, h("div", {class: "card-head"}, h("span", {class: "icon", "aria-hidden": "true"}, TRIGGER_ICONS[type] || "?"), h("a", {class: "title", href: `#schedules/${s.id}`}, s.name), toggle), h("p", {class: "summary"}, s.summary), h("p", {class: "meta"}, next ? h("span", {}, next) : null, status, s.last_message && s.last_status !== "done" ? h("span", {class: "muted small last"}, s.last_message) : null), h("div", {class: "actions"}, h("a", {class: "button small", href: `#schedules/${s.id}`}, "Edit"), h("button", {class: "small", onclick: testPrint}, "Test print"), h("button", {class: "ghost small", onclick: duplicate}, "Duplicate"), h("button", {class: "danger ghost small", onclick: remove}, "Delete"), busy)); } function presetGrid() { return h("div", {class: "presets"}, PRESETS.map((p) => h("button", {class: "preset", onclick: async () => { pending.draft = p.make(await sources()); location.hash = "#schedules/new"; }}, h("span", {class: "icon", "aria-hidden": "true"}, p.icon), h("strong", {}, p.title), h("span", {class: "muted small"}, p.blurb)))); } function showPresets(view) { const dialog = h("dialog", {class: "sheet", onclose: () => dialog.remove()}, h("div", {class: "toolbar"}, h("h2", {}, "New schedule"), h("button", {class: "ghost", onclick: () => dialog.close()}, "Close")), presetGrid()); view.append(dialog); dialog.showModal(); }