Show progress for uploads and updates, and reload when they are done
An uploaded bundle gets a progress bar over the page; a running update an overlay that follows its steps. A page that finished working, or that was sent to the login page by a restart, is loaded whole. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
This commit is contained in:
@@ -14,12 +14,17 @@
|
||||
setTimeout(function () {
|
||||
fetch(location.pathname + location.search,{ credentials: "same-origin", headers: { "X-Live": "1" } })
|
||||
.then(function (r) {
|
||||
if (r.redirected || !r.ok) throw new Error("not the page");
|
||||
// Sent elsewhere: the web UI restarted and forgot the session. Show where it leads.
|
||||
if (r.redirected) { location.reload(); return null; }
|
||||
if (!r.ok) throw new Error("not the page");
|
||||
return r.text();
|
||||
})
|
||||
.then(function (html) {
|
||||
if (html === null) return;
|
||||
var next = new DOMParser().parseFromString(html, "text/html").getElementById("live");
|
||||
if (!next) throw new Error("no content");
|
||||
// Done: load the page whole, as it is when nothing is going on.
|
||||
if (!next.getAttribute("data-live")) { location.reload(); return; }
|
||||
var old = live.querySelector("pre.log");
|
||||
var atEnd = !old || old.scrollTop + old.clientHeight >= old.scrollHeight - 8;
|
||||
var kept = old ? old.scrollTop : 0;
|
||||
|
||||
@@ -58,6 +58,11 @@ h1 { font-size: 1.45rem; font-weight: 600; letter-spacing: -.01em; margin: .25re
|
||||
h2 { font-size: .95rem; font-weight: 600; margin: 0 0 .6rem; }
|
||||
section { margin: 0 0 1.5rem; }
|
||||
section.busy, section.pending { padding: .75rem 1rem; border-radius: var(--radius); background: var(--warn); }
|
||||
.upload-overlay { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, .55); padding: 1rem; }
|
||||
.upload-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 32px rgba(0, 0, 0, .35); padding: 1.25rem 1.5rem; width: min(30rem, 100%); }
|
||||
.upload-card h2 { margin: 0 0 .75rem; }
|
||||
.upload-card progress { width: 100%; height: 1rem; accent-color: var(--accent); }
|
||||
.upload-card p { margin: .6rem 0 0; font-size: .9rem; }
|
||||
.flash, .error { padding: .6rem .9rem; border-radius: var(--radius); margin: 0 0 1rem; }
|
||||
.flash { background: var(--ok); }
|
||||
.error { background: color-mix(in srgb, var(--bad) 12%, transparent); }
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// A form marked data-upload is sent with a progress bar over the page: a
|
||||
// bundle is hundreds of megabytes. The server answers with a redirect once it
|
||||
// has taken the file; the browser follows it and we go to where it ended up.
|
||||
(function () {
|
||||
function size(n) {
|
||||
return n >= 1 << 30 ? (n / (1 << 30)).toFixed(2) + " GiB" : (n / (1 << 20)).toFixed(1) + " MiB";
|
||||
}
|
||||
|
||||
function overlay() {
|
||||
var box = document.createElement("div");
|
||||
box.className = "upload-overlay";
|
||||
box.setAttribute("role", "dialog");
|
||||
box.setAttribute("aria-modal", "true");
|
||||
var card = document.createElement("div");
|
||||
card.className = "upload-card";
|
||||
var title = document.createElement("h2");
|
||||
var bar = document.createElement("progress");
|
||||
bar.max = 100;
|
||||
var text = document.createElement("p");
|
||||
text.className = "muted";
|
||||
card.append(title, bar, text);
|
||||
box.append(card);
|
||||
document.body.append(box);
|
||||
return { box: box, title: title, bar: bar, text: text };
|
||||
}
|
||||
|
||||
document.addEventListener("submit", function (e) {
|
||||
var form = e.target;
|
||||
if (!form.hasAttribute("data-upload") || e.defaultPrevented || !window.XMLHttpRequest || !window.FormData) return;
|
||||
e.preventDefault();
|
||||
var ui = overlay();
|
||||
var started = Date.now();
|
||||
ui.title.textContent = "Uploading…";
|
||||
ui.bar.value = 0;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", form.action);
|
||||
xhr.upload.onprogress = function (ev) {
|
||||
if (!ev.lengthComputable) {
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = size(ev.loaded) + " sent";
|
||||
return;
|
||||
}
|
||||
var secs = Math.max((Date.now() - started) / 1000, 0.5);
|
||||
var rate = ev.loaded / secs;
|
||||
ui.bar.value = (ev.loaded / ev.total) * 100;
|
||||
ui.text.textContent = size(ev.loaded) + " of " + size(ev.total) + " (" + Math.floor((ev.loaded / ev.total) * 100) + " %), " +
|
||||
size(rate) + "/s";
|
||||
};
|
||||
xhr.upload.onload = function () {
|
||||
// All sent; the router checks and unpacks it before it answers.
|
||||
ui.title.textContent = "Verifying…";
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = "The router is checking the signature and unpacking the bundle.";
|
||||
};
|
||||
function failed(msg) {
|
||||
ui.box.remove();
|
||||
alert(msg);
|
||||
}
|
||||
xhr.onerror = function () { failed("The upload failed: the router could not be reached."); };
|
||||
xhr.onabort = function () { failed("The upload was cancelled."); };
|
||||
xhr.onload = function () {
|
||||
// The answer is the page the server redirected to (with its message).
|
||||
if (xhr.responseURL && xhr.responseURL !== location.href) location.href = xhr.responseURL;
|
||||
else location.reload();
|
||||
};
|
||||
xhr.send(new FormData(form));
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user