package file→ .shtukbase/packages/live/templates/tasks-app.html
templates/tasks-app.html
139 lines, 7.1 KB.
- <!doctype html>
- <!--
- An example of an interactive page for the live server (package live). Plain HTML, no build, no libraries:
- the buttons call the server API through fetch, the list refreshes itself on the /api/events events.
- Copy and change: the collection is COLLECTION, the buttons are ACTIONS, the check is CHECK.
- -->
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Tasks - live page</title>
- <style>
- :root { --bg: #f7f6f3; --card: #fff; --ink: #37352f; --mute: #787774; --line: #e9e9e7; --blue: #2f80ed; --red: #e03e3e; --green: #0f7b6c; }
- @media (prefers-color-scheme: dark) { :root { --bg: #191919; --card: #252525; --ink: #e6e6e4; --mute: #9b9a97; --line: #373737; } }
- * { box-sizing: border-box; }
- body { margin: 0; font: 14px/1.45 -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
- header { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 16px 24px; border-bottom: 1px solid var(--line); }
- h1 { font-size: 18px; margin: 0 12px 0 0; }
- .state { color: var(--mute); font-size: 12.5px; }
- .state.ro { color: var(--red); }
- form { display: flex; gap: 6px; }
- input { font: inherit; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink); min-width: 240px; }
- button { font: inherit; padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink); cursor: pointer; }
- button.main { background: var(--blue); border-color: var(--blue); color: #fff; }
- button:disabled { opacity: .5; cursor: default; }
- main { display: flex; gap: 14px; padding: 18px 24px; overflow-x: auto; align-items: flex-start; }
- .col { flex: 0 0 250px; background: color-mix(in srgb, var(--line) 45%, transparent); border-radius: 12px; padding: 10px; }
- .col h2 { font-size: 13px; margin: 2px 4px 10px; display: flex; justify-content: space-between; color: var(--mute); }
- .card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 8px; }
- .card .t { font-weight: 600; margin-bottom: 8px; }
- .card .row { display: flex; gap: 6px; flex-wrap: wrap; }
- .err { color: var(--red); font-size: 12.5px; margin-top: 6px; }
- pre { margin: 0 24px 24px; padding: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; max-height: 280px; overflow: auto; font-size: 12px; white-space: pre-wrap; }
- pre:empty { display: none; }
- </style>
- </head>
- <body>
- <header>
- <h1>Tasks</h1>
- <form id="new"><input name="title" placeholder="New task - title" required><button class="main">Create</button></form>
- <button id="check">Check project</button>
- <span id="state" class="state">…</span>
- </header>
- <main id="board"></main>
- <pre id="out"></pre>
- <script>
- // Which collection and which buttons. A button shows on a task in the state `from` and moves it to `to`.
- const COLLECTION = "/work/tasks";
- const ACTIONS = [
- { from: "todo", to: "doing", label: "Take" },
- { from: "doing", to: "done", label: "Done" },
- ];
- // An action from the allowlist (run: of the page /base/live.md): "shtuk check" runs all checks of the project.
- const CHECK = { package: "shtuk", action: "check" };
- const $ = (s) => document.querySelector(s);
- const h = (tag, attrs, ...kids) => { const el = Object.assign(document.createElement(tag), attrs || {}); el.append(...kids.filter((k) => k != null)); return el; };
- let hello = { write: false, run: [] };
- /** An API call: JSON both ways; a server error becomes an exception with its text. */
- async function api(method, path, body) {
- const r = await fetch(path, { method, headers: body ? { "Content-Type": "application/json" } : {}, body: body ? JSON.stringify(body) : undefined });
- const data = await r.json().catch(() => ({}));
- if (!r.ok) throw new Error(data.error || r.status + " " + r.statusText);
- return data;
- }
- async function load() {
- const { collection, records } = await api("GET", "/api/records?collection=" + encodeURIComponent(COLLECTION));
- const phases = collection.phases.status || [];
- const board = $("#board");
- board.replaceChildren();
- for (const ph of phases) {
- const items = records.filter((r) => r.fields.status === ph.name);
- const col = h("section", { className: "col" }, h("h2", {}, h("span", {}, ph.title), h("span", {}, String(items.length))));
- for (const r of items) col.append(card(r));
- board.append(col);
- }
- }
- function card(r) {
- const err = h("div", { className: "err" });
- const row = h("div", { className: "row" });
- for (const a of ACTIONS.filter((a) => a.from === r.fields.status)) {
- row.append(h("button", { textContent: a.label, disabled: !hello.write, onclick: async () => {
- err.textContent = "";
- try { await api("PATCH", "/api/record", { path: r.path, set: { status: a.to }, etag: r.etag }); }
- catch (e) { err.textContent = e.message; }
- } }));
- }
- return h("div", { className: "card" }, h("div", { className: "t", textContent: r.title }), row, err);
- }
- $("#new").onsubmit = async (e) => {
- e.preventDefault();
- const input = e.target.title;
- try { await api("POST", "/api/records", { collection: COLLECTION, fields: { title: input.value } }); input.value = ""; }
- catch (err) { alert(err.message); }
- };
- $("#check").onclick = async () => {
- const out = $("#out");
- out.textContent = "";
- // stream: true - the output arrives line by line (NDJSON) while the action runs
- const r = await fetch("/api/run", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...CHECK, stream: true }) });
- if (!r.ok) { out.textContent = (await r.json()).error; return; }
- const reader = r.body.getReader(), dec = new TextDecoder();
- let rest = "";
- for (;;) {
- const { done, value } = await reader.read();
- if (done) break;
- rest += dec.decode(value, { stream: true });
- const lines = rest.split("\n");
- rest = lines.pop();
- for (const l of lines) {
- const ev = JSON.parse(l);
- if (ev.type === "out") out.textContent += ev.text + "\n";
- if (ev.type === "end") out.textContent += (ev.code === 0 ? "PASS " : "FAIL ") + ev.step + "\n";
- if (ev.type === "exit") out.textContent += ev.code === 0 ? "\nAll checks passed.\n" : "\nThere are failures.\n";
- }
- out.scrollTop = out.scrollHeight;
- }
- };
- (async () => {
- try { hello = await api("GET", "/api/hello"); }
- catch { $("#state").textContent = "No live server: open the page through shtuk run live serve."; return; }
- $("#state").textContent = hello.write ? "Editing is on" : "Read only: open the server link with the token";
- $("#state").classList.toggle("ro", !hello.write);
- $("#new button").disabled = !hello.write;
- $("#check").disabled = !hello.write || !hello.run.includes(CHECK.package + " " + CHECK.action);
- if (hello.write && $("#check").disabled) $("#check").title = "Add \"shtuk check\" to run: of the page /base/live.md";
- await load();
- // the project files changed (here, in another tab or in an editor): read them again
- new EventSource("/api/events").addEventListener("change", () => load());
- })();
- </script>
- </body>
- </html>