fix(admin): complete web uploads, hold Esc during a run, keep focus and restores across job refreshes

The upload dialog's completion helper called `api`, which only exists inside
openUpload, so every web upload failed with a ReferenceError at its last step.
complete() now takes the dialog's api. A new test lexes every admin module and
fails when a top-level function uses api without taking it; it flags the old
upload.js.

During a run Esc is now stopped on the document in the capture phase. The old
listener sat on the dialog, so with the focus on the body (Start disables
itself, a click on the dialog's text) a second Esc closed the dialog and threw
the uploads away. The listener is added when a run starts and removed when it
ends or the dialog closes.

A sequence page refreshes after any ended job, because jobState names no
sequence. That refresh now redraws the header only when the sequence changed,
and the renders only when their list did. Stars, tags and renders are redrawn
through redraw(), so the keyboard focus stays put. restoreFocus finds an
unnamed control by its place, not as the first unnamed one. Failed thumbnails
are retried with a doubling wait and at most 8 tries, counted again once the
sequence changes.

The trash page reads the emptying it follows from /api/v1/jobs/{id} on every
load and after the event socket reconnects (api.events now emits
"reconnected"), so Empty no longer stays disabled when the job's end was lost.
A Restore in flight stays disabled across redraws.

The Library list counts cancelled jobs as ended (shared JOB_ENDED). Comments
are corrected in shared.js and library.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 641b9cd96b95d523990d1414c2bdead4138c5f4d)
This commit is contained in:
Alby96 committed 2026-10-27 13:53:41 +01:00
1 parent 4518ffe655
commit 81cf58227d
9 files changed
+379 -52

No files matched your search

+15 -11
View File
@@ -152,11 +152,12 @@ export function openUpload({ api, sequence, onDone } = {}) {
});
// Esc closes only while nothing is being sent; Cancel stops a run. Chromium lets a dialog's cancel be prevented only
// once per user activation, so during a run Esc is stopped at its keydown; the cancel handler stays for close requests
// that do not come from the keyboard (a back gesture).
dialog.addEventListener("keydown", (e) => {
if (running && e.key === "Escape") e.preventDefault();
});
// once per user activation, so during a run Esc is stopped at its keydown, on the document in the capture phase: a
// click on the dialog's text moves the focus to the body, and Esc from there would pass the dialog by. The cancel
// handler stays for close requests that do not come from the keyboard (a back gesture).
const stopEscape = (e) => {
if (e.key === "Escape") e.preventDefault();
};
dialog.addEventListener("cancel", (e) => {
if (running) e.preventDefault();
});
@@ -176,6 +177,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
clearTimeout(searchTimer);
cancelAnimationFrame(frame);
window.removeEventListener("beforeunload", guard);
document.removeEventListener("keydown", stopEscape, true);
window.removeEventListener("chronos:signed-out", signedOut);
dialog.remove();
resolve();
@@ -456,6 +458,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
sameContent = new Map();
const signal = controller.signal;
window.addEventListener("beforeunload", guard);
document.addEventListener("keydown", stopEscape, true);
drawButtons();
let changed = false;
try {
@@ -496,6 +499,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
running = false;
controller = null;
window.removeEventListener("beforeunload", guard);
document.removeEventListener("keydown", stopEscape, true);
todo.filter((i) => ACTIVE.has(i.state)).forEach((i) => setState(i, "cancelled"));
drawButtons();
drawTotal();
@@ -629,7 +633,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
if (failure !== null && !filing(failure)) throw failure;
setState(item, "verifying");
try {
await complete(item.uploadId, restore, signal);
await complete(api, item.uploadId, restore, signal);
} catch (err) {
if (!(err instanceof ApiFailure && err.status === 409 && err.code === "photo_removed")) throw err;
// Deleted from the sequence while it was on its way: not filed, and the server dropped the upload (R6).
@@ -656,12 +660,12 @@ function filing(err) {
}
/**
* Completes an upload (with restore, a photo deleted from the sequence meanwhile is filed anyway). A completion refused
* while another one is filing the upload (the same photo started twice, a retry after a lost answer) waits for it: once
* the upload is committed, completing again answers its frame. Only an upload that failed or was cancelled, or one that
* stays busy for about a minute, fails the photo.
* Completes an upload through api, the dialog's (with restore, a photo deleted from the sequence meanwhile is filed
* anyway). A completion refused while another one is filing the upload (the same photo started twice, a retry after a
* lost answer) waits for it: once the upload is committed, completing again answers its frame. Only an upload that
* failed or was cancelled, or one that stays busy for about a minute, fails the photo.
*/
async function complete(uploadId, restore, signal) {
async function complete(api, uploadId, restore, signal) {
const url = `/api/v1/uploads/${uploadId}/complete${restore ? "?restore=true" : ""}`;
for (let check = 0; ; check++) {
try {