Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / infra / agents / agentd / src / static / app.js

app.js121 lines · 4.3 KiBRaw
1/* agentd live updates. No HTML is ever built from event JSON: on each
2 * SSE event the script fetches server-rendered maud fragments
3 * (/tasks/:id/timeline?after=seq, /panel, /cards) and swaps them in. */
4
5(function () {
6 "use strict";
7
8 // Disable a form's buttons on submit (double-click guard; the server also
9 // refuses double approves via claim_approve).
10 document.addEventListener("submit", function (ev) {
11 var form = ev.target;
12 if (!form || form.tagName !== "FORM") return;
13 var btns = form.querySelectorAll("button, input[type=submit]");
14 for (var i = 0; i < btns.length; i++) btns[i].disabled = true;
15 });
16
17 function nearBottom() {
18 var el = document.documentElement;
19 return el.scrollHeight - el.scrollTop - el.clientHeight < 240;
20 }
21
22 function maxSeqIn(root, attr) {
23 var max = null;
24 var nodes = root.querySelectorAll("[data-seq]");
25 for (var i = 0; i < nodes.length; i++) {
26 var s = parseInt(nodes[i].getAttribute("data-seq"), 10);
27 if (!isNaN(s) && (max === null || s > max)) max = s;
28 }
29 return max;
30 }
31
32 // ---- task page live timeline ----
33 var timeline = document.getElementById("timeline");
34 if (timeline && typeof EventSource !== "undefined") {
35 var taskId = timeline.getAttribute("data-task-id");
36 var lastSeq = parseInt(timeline.getAttribute("data-after") || "-1", 10);
37 var panel = document.getElementById("panel");
38 var cards = document.getElementById("cards");
39 var backoff = 1000;
40 var source = null;
41 var refreshing = false;
42
43 function refreshFragments() {
44 if (refreshing) return;
45 refreshing = true;
46 var stick = nearBottom();
47 var jobs = [];
48 jobs.push(
49 fetch("/tasks/" + taskId + "/timeline?after=" + lastSeq, { credentials: "same-origin" })
50 .then(function (r) { return r.text(); })
51 .then(function (html) {
52 if (!html) return;
53 var tmp = document.createElement("div");
54 tmp.innerHTML = html;
55 while (tmp.firstChild) timeline.appendChild(tmp.firstChild);
56 var m = maxSeqIn(timeline, "data-seq");
57 if (m !== null && m > lastSeq) lastSeq = m;
58 })
59 );
60 if (panel) {
61 jobs.push(
62 fetch("/tasks/" + taskId + "/panel", { credentials: "same-origin" })
63 .then(function (r) { return r.text(); })
64 .then(function (html) { if (html) panel.innerHTML = html; })
65 );
66 }
67 if (cards) {
68 jobs.push(
69 fetch("/tasks/" + taskId + "/cards", { credentials: "same-origin" })
70 .then(function (r) { return r.text(); })
71 .then(function (html) { if (html) cards.innerHTML = html; })
72 );
73 }
74 Promise.all(jobs).then(function () {
75 refreshing = false;
76 if (stick) window.scrollTo(0, document.documentElement.scrollHeight);
77 }).catch(function () {
78 refreshing = false;
79 });
80 }
81
82 function connect() {
83 if (source) { try { source.close(); } catch (e) {} }
84 source = new EventSource("/tasks/" + taskId + "/events?after=" + lastSeq);
85 source.onopen = function () { backoff = 1000; };
86 source.onmessage = function () {
87 // The event JSON only signals "something new"; the markup always
88 // comes from the server-rendered fragments below.
89 backoff = 1000;
90 refreshFragments();
91 };
92 source.onerror = function () {
93 try { source.close(); } catch (e) {}
94 var wait = Math.min(backoff, 30000);
95 backoff = Math.min(backoff * 2, 30000);
96 setTimeout(connect, wait);
97 };
98 }
99 connect();
100 }
101
102 // ---- dashboard live refresh (every 10s) ----
103 var dash = document.getElementById("dashboard");
104 if (dash) {
105 setInterval(function () {
106 if (document.hidden) return;
107 fetch("/fragments/dashboard", { credentials: "same-origin" })
108 .then(function (r) { return r.text(); })
109 .then(function (html) {
110 if (!html) return;
111 var tmp = document.createElement("div");
112 tmp.innerHTML = html;
113 var title = tmp.querySelector("[data-doc-title]");
114 if (title) document.title = title.getAttribute("data-doc-title");
115 var body = tmp.querySelector("[data-dash-body]");
116 if (body) dash.innerHTML = body.innerHTML;
117 })
118 .catch(function () {});
119 }, 10000);
120 }
121})();