Files
ComfyAssets-ComfyUI_PromptM…/web/js/workspace/system-sources.mjs
T
Vito 043403d09e feat(thumbnails): choose where thumbnails are kept and move them (#320) (#592)
* refactor(thumbnails): one resolver for where previews live on disk (#320)

Add py/api/preview_store.py: the store of a source, a preview's path,
URL parts to disk, containment (owns), a write generation and the lock
around every preview's final rename. Every preview reader, writer and
remover now asks it instead of joining <source>/thumbnails by hand:
serve, on-demand, capture (its path resolved when the write starts),
the after-scan queue, the thumbnail plans and job payloads, clear, the
temp sweep, History covers, the auto-tag listing and the owned-preview
lookups.

Source mode only: every path is the one built before, so nothing
changes on disk or on the wire. A structural test keeps new
thumbnails joins out of the backend.

* refactor(thumbnails): keep the after-scan queue's store rule and pin the write lock (#320)

A planned target keeps the plan's own containment rule (inside the
store, as the thumbnail job writes it); captures keep the serve rule.
The structural test also covers PREVIEW_FOLDER joins and the
repo-root modules; the write-lock test asserts the lock instead of
waiting on a timer; new tests cover nested roots in the after-scan
queue and a legacy upper-case preview served through the resolver.

* feat(thumbnails): keep previews in one folder, set from config.json (#320)

thumbnails.location in config.json can now name one folder for every
source's previews: <folder>/<source key>/<rel>_thumb<ext>. A source key is
a readable slug plus a short hash of the source's identity, stored in the
folder's marker (.promptmanager-thumbnails.json) with the install that
made it. Serving, on-demand and capture writes, listings, clear, delete,
duplicates, the temp sweep and History covers all follow it; a leftover in
<source>/thumbnails is never served and other text under thumbnails/ is
refused.

A missing, unplugged or foreign folder is unavailable (a probe checks it
every 5 s within 2 s; a stalled or failing store call flips it at once):
originals are served, nothing is written anywhere, the folder is never
re-created, and generate, convert and clear answer 409
location-unavailable. A bad config value is unavailable too, never a
fall-back to the sources.

No UI yet: POST /settings refuses thumbnail_location (409
location-needs-move); GET /settings reports it with its path, the
thumbnail status without one. Sources inside or around the folder and a
library move into it are refused.

* fix(thumbnails): never follow a marker or a link out of the thumbnail folder (#320)

Marker keys are data, not paths: only a single <slug>-<hex> folder name
is ever joined onto the thumbnail folder; anything else is dropped and
logged. Every preview write and delete passes one ownership check (below a
key folder, lexically and resolved, no link, junction or reparse point on
the way); make_parent refuses anything else and writes nothing while the
folder lacks this install's marker (an unmounted mount point). Clear never
descends into a junction.

The configured location is in force before any request (unchecked folder
mode is unavailable), and a startup check that fails leaves it invalid,
never the sources. Older listings that run on the event loop no longer
touch the store in folder mode. A leftover under another case of
thumbnails/ on a case-insensitive disk is refused too.

* test(memory): let the musl test fake os.confstr on Windows (#523)

The portability run on main fails since #523: Windows has no
os.confstr, so patching it without create=True raises before the test
runs.

* feat(thumbnails): move thumbnails between the sources and one folder (#320)

POST /images/thumbnails/plan reviews a move (what is copied, removed and
left, free space, too-long Windows paths, sources no longer listed) and
hands out a token; POST /jobs/thumbnails {mode: move} runs it as a
thumbnails job: hold automatic previews, copy and check every preview on
the job pool, switch with one config commit that records the old copies,
then remove them. Per-file errors still switch; a folder-wide error, a
cancel before the switch or a crash leaves nothing changed (a journal is
undone at the next start); old copies that could not go are removed by a
finishing job at the next start. Leaving a folder removes the previews of
this install's sources that are no longer listed (Q320-8).

Recreate folder and Use this folder answer an unavailable folder; the
Thumbnail maintenance scope prune removes the thumbnails of removed
sources.

* feat(system): choose where thumbnails are kept and move them (#320)

System › Sources & scans gains the Thumbnail location row (D196): Inside
each source or One folder, the folder path, the status line and Review
move…, whose review (D197) lists what is copied, removed and left before
Move thumbnails starts; a refused folder is explained under the field, a
folder of your own asks for a confirmation first. Activity shows a move
and a prune with their own tiles and step line (D198). An unavailable
folder shows a notice on Overview and Sources & scans with Recreate folder
or Use this folder (D199). Thumbnail maintenance offers Remove thumbnails
of removed sources in folder mode (D200), and Move library's review says
where the thumbnails stay (D201).

Closes #320

* fix(thumbnails): keep a move's own previews and never walk out of a store (#320)

A cleanup that waited from an earlier move never names a key folder the
new move just filled, so moving back keeps the previews. Removing empty
folders walks top-down and never into a link or junction. Any error
while copying undoes the copies; once the switch is committed, a journal
that cannot be removed no longer undoes it. Hand-edited cleanup entries
and marker keys never name a folder outside the stores; removal stops at
interpreter exit. The path field carries aria-invalid with its reason,
and a review that went stale reviews again.

* test(thumbnails): cover the prune scope, the Overview notice and the space refusal (#320)

The frontend coverage gate counted the new branches of thumbnail
maintenance, the Activity views, the notices' Change location and the
api-client's space refusal as uncovered; page and unit tests now drive
them.

* fix(thumbnails): report removals that fail and explain the quiet excepts (#320)

An old thumbnail the finishing move cannot remove (held open on Windows)
is now an error row of the job with its path, and logged; a failed copy's
temp that cannot go is logged with its path. Every except that only
passes says why, and the thread runners say their caller re-raises.

* fix(thumbnails): never plan or delete through a linked key folder (#320)

Review finding P1 on #591: in folder mode a source key folder that is a
link or junction to a folder outside was still handed to the planner, so
Convert outdated could remove an older-format file outside the thumbnail
folder. ensure_store now refuses a linked store before any planning, and
each removal of an old preview passes may_write at the moment it runs.

* fix(thumbnails): the temp sweep never walks into a link (#320)

Review finding P2 on #591: Clear thumbnails and the startup sweep removed
crashed-write temps with a plain os.walk, which on Windows walks into a
junction even without followlinks. The sweeper now skips a folder that is
a link, junction or reparse point (the top one too), and each temp passes
may_write before it is removed.

* fix(thumbnails): lock the folder marker across processes (#320)

Review finding P2 on #591: two installs sharing one thumbnail folder could
each read the marker, add a key and replace it, so the last writer
dropped the other's key. write_marker now re-reads, changes and replaces
the marker under a lock file made with O_CREAT | O_EXCL beside it (atomic
on shares, unlike flock); a lock older than 30 s, left by a writer that
died, is taken over, and a wait past 1.5 s fails as a TimeoutError like
an unreachable folder. Tested with three processes adding keys at once.

* test(thumbnails): cover the space refusal, Overview's Use this folder and the prune review (#320)

The frontend coverage floors of activity-kinds, system-overview and
thumbnail-review: a disk without the space shows the review but cannot
move (a fixture route fakes the free space), Use this folder on Overview
reloads the host values, the prune review's text and rows are pure
functions with unit tests, and Overview takes the System drafts it is
always mounted with.

* fix(thumbnails): never copy through a linked folder; refuse a target inside the folder in use (#320)

Moving back to Inside each source followed an existing linked folder in the
destination and could replace a file outside the source (Codex P1). A target
nested inside the thumbnail folder in use was accepted, and the old-copy
cleanup then removed the previews just copied there (Codex P2).

Every copy now checks the whole destination chain for links, also when the
folders exist, and the move refuses a folder inside the one in use with
location-inside-current. The cancelled branches no longer mix explicit and
implicit returns (code-quality note).

* fix(system): one full stop in the move-back review (#320)

* fix(thumbnails): a folder check answers for the folder as it is now (#320)

Probe.check() joined a check already running, or a finished one nobody had
collected. A folder deleted right after configure() woke the probe therefore
stayed available until the next probe, which failed the Overview notice spec in
the merge queue. It now waits for a running check and starts a new one, still
never two at once, and a stuck check still times out. The spec also asserts the
fixture's removal took effect.
2026-11-01 13:11:13 +00:00

430 lines
23 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* System › Sources & scans (#249 System S4 Task 11): the concept's `renderSystem` "sources" body (app.js 679–681).
*
* - The optional `.notice` "Bring older images into your prompt history" shows until this browser dismissed the 3.2.0
* update notice (older-images-notice.mjs, the legacy admin page's own key). Later dismisses it without scanning;
* Review scan dismisses it, as the legacy page's Scan does, and reviews every source. D166: dismissing it in the shell
* or another tab removes it here too, and the shell does not show it on this section.
* - "Image sources" lists the server's sources (GET /settings, S4 Task 9) in `#source-list`, one `.operation` each:
* its label, "<path> · watching | watching until restart | manual scan | restart pending" and "Scan changes"
* (watched) or "Scan folder", which opens the shared "Review metadata scan" (D55) for that one source ("1 output
* folder") and starts the session job over it (`roots` with its public path, S4 Task 10). The job opens in Activity;
* the toast only says it started. When the sources changed since they were read, nothing starts: the toast says so
* and the list is read again.
* - "+ Add source" asks the server whether a folder can be a source (`validate-source`, which writes nothing), then adds
* it to the host draft of `gallery_root_paths`; Remove takes one out. Both wait for the one Save row.
* - "Watch for new outputs" (`monitoring_enabled`) and "Worker threads" (`worker_threads`, a range up to the server's
* `cpu_count`, D113) are host drafts too. "Enrich existing metadata" reviews a scan of every source.
* - D158 (#247): "Save prompts from other workflows" (`save_foreign_runs`), a host draft below "Watch for new outputs",
* applied by the server at once (the monitor reads it for every file); disabled, saying why, while monitoring is off,
* unless the running monitor still watches until a restart (D128 `stopping`): then it can still be set.
* - "Thumbnail generation → Review options" opens "Thumbnail maintenance" (thumbnail-review.mjs, S5 Task 14), which
* starts the thumbnail Activity job.
*
* D98: per-source Remove; an added source reads "not saved yet" until Save; the line under the list says when
* ComfyUI's output folder is used instead of configured sources or after an unsaved removal of the last one; with no
* source configured, ComfyUI's output folder is the row "ComfyUI/output · watching · default" (`in_comfyui`: its public
* path is relative to the ComfyUI base) and its scan keeps the server's public path; "restart pending" for a source the running monitor does not watch yet; the
* "Refresh files" operation (`/gallery/rescan`) closes the surface. D127: the add dialog's field is "Folder path",
* starts empty and says the server's reason in place; its action is "Add", and Enter adds too.
* D128: "watching until restart" for a source the running monitor still watches after monitoring was switched off;
* "same folder as another source" for a configured source the effective list already holds under another spelling; a
* save refused for one source (`invalid-source`) marks that row and the status says the server's reason.
*/
import { createButton } from '../shared/toolkit/core.js';
import { closeWorkspaceDialog, field, openContentDialog } from './workspace-dialogs.mjs';
import { openMetadataScan } from './activity-scan.mjs';
import { openThumbnailReview } from './thumbnail-review.mjs';
import { locationBlock, noticeSlot } from './system-thumbnail-location.mjs';
import { settingRow, operationRow } from './system-controls.mjs';
import { HOST_UNAVAILABLE } from './system-appearance.mjs';
import { showWorkspaceToast } from './toast.mjs';
import { noun } from './plural.mjs';
import { dismissNotice, noticeDismissed, noticeNode, watchNoticeDismissal } from './older-images-notice.mjs';
export { NOTICE_KEY, NOTICE_VERSION, dismissNotice, noticeDismissed } from './older-images-notice.mjs';
const SETTINGS_URL = '/prompt_manager/settings';
const VALIDATE_URL = '/prompt_manager/settings/validate-source';
const RESCAN_URL = '/prompt_manager/gallery/rescan';
const ROOTS_KEY = 'gallery_root_paths';
export const WATCH_COPY = Object.freeze({ watching: 'watching', stopping: 'watching until restart', manual: 'manual scan',
restart_pending: 'restart pending' });
// The states whose observer runs now: their scan only has to catch up on changes.
const WATCHED = new Set(['watching', 'stopping']);
const NOT_SAVED = 'not saved yet';
// An auto-detected source (ComfyUI's output folder) says it is the default on its own row.
const DEFAULT_SOURCE = 'default';
const COMFYUI_PREFIX = 'ComfyUI/';
export const DUPLICATE_SOURCE = 'same folder as another source';
/** D98: the line under the source list when it does not say everything by itself. */
export const NOTES = Object.freeze({
removedAll: 'No source is configured. After saving, ComfyUI’s output folder is used.',
fallback: 'No configured source is available; ComfyUI’s output folder is used.',
none: 'No source is available.',
});
export const LOADING_TEXT = 'Loading sources…';
export const LOAD_FAILED_TEXT = 'Sources could not be loaded. Your saved data has not changed.';
export const DUPLICATE_TEXT = 'This folder is already a source.';
export const VALIDATE_FAILED_TEXT = 'The folder could not be checked. Try again.';
export const REFRESH_FAILED_TEXT = 'Files could not be refreshed. Try again.';
/** D158 (#247): the row the concept does not draw, below "Watch for new outputs". */
export const FOREIGN_RUNS = Object.freeze({ id: 'foreign-runs', key: 'save_foreign_runs', label: 'Save prompts from other workflows',
description: 'New outputs of workflows without a PromptManager node add their prompt to the library when it is certain.',
off: 'Applies while Watch for new outputs is on.' });
/**
* D158: the row for the drafted `save_foreign_runs` (`value`) and `monitoring_enabled` (`monitoring`): disabled until
* read, and while monitoring is off, when `note` says why (the value is kept and saved as it is). `stopping`: a source
* is still watched until the restart (D128), so the value can still be set; the note stays.
*/
export function foreignRunsState({ value, monitoring, stopping = false }) {
if (value === undefined) return { disabled: true, checked: null, note: null };
const off = monitoring === false;
return { disabled: off && !stopping, checked: value === true, note: off ? FOREIGN_RUNS.off : null };
}
// Labels of the sources added in this page session (validate-source answers them), for the rows not saved yet.
const addedLabels = new Map();
const defaultStorage = () => window.localStorage;
const sameRoots = (a, b) => Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((path, index) => path === b[index]);
/** The roots to draft: the saved list itself when they are equal, so the draft is clean again. */
export function draftRoots(next, saved) {
return sameRoots(next, saved) ? saved : [...next];
}
const folderName = path => path.split('/').filter(Boolean).at(-1) ?? path;
function effectiveRow(source, remove) {
const watched = WATCHED.has(source.watch);
const auto = remove === null;
// The default row reads from ComfyUI's own folder ("ComfyUI/output") when its public path is relative to the base.
const shown = auto && source.in_comfyui === true ? `${COMFYUI_PREFIX}${source.path}` : source.path;
const parts = [shown, WATCH_COPY[source.watch] ?? WATCH_COPY.manual, ...(auto ? [DEFAULT_SOURCE] : [])];
return { key: remove ?? `auto:${source.root_index}`, title: source.label, description: parts.join(' · '),
scan: { label: watched ? 'Scan changes' : 'Scan folder', root: source.root_index, path: source.path }, remove };
}
/**
* The source list for `sources` (GET /settings' `sources`), the saved `gallery_root_paths` and the drafted ones
* (`current`): `{rows: [{key, title, description, scan: {label, root, path} | null, remove: path | null}], note}`.
* A valid configured source missing from the effective list is another spelling of a folder already listed.
*/
export function sourceRows({ sources, saved, current, labels = addedLabels }) {
const configured = sources.configured ?? [];
const effective = sources.effective ?? [];
const savedRoots = saved ?? configured.map(source => source.path);
const roots = current ?? savedRoots;
const dirty = !sameRoots(roots, savedRoots);
const rows = [];
for (const path of new Set(roots)) {
const entry = configured.find(source => source.path === path);
const live = entry?.valid ? effective.find(source => source.path === path) : undefined;
if (live) rows.push(effectiveRow(live, path));
else if (entry) {
const why = entry.valid ? DUPLICATE_SOURCE : entry.reason ?? WATCH_COPY.manual;
rows.push({ key: path, title: entry.label, description: `${path} · ${why}`, scan: null, remove: path });
} else rows.push({ key: path, title: labels.get(path) ?? folderName(path), description: `${path} · ${NOT_SAVED}`, scan: null, remove: path });
}
if (dirty) return { rows, note: roots.length ? null : NOTES.removedAll };
if (sources.auto_detected) {
rows.push(...effective.map(source => effectiveRow(source, null)));
return { rows, note: configured.length ? NOTES.fallback : null };
}
return { rows, note: rows.length ? null : NOTES.none };
}
/** The Refresh files toast for the rescan's `{total, roots}`. */
export function refreshedText(result) {
const { total, roots } = result ?? {};
if (![total, roots].every(value => Number.isSafeInteger(value) && value >= 0)) return 'Files refreshed.';
return `Files refreshed. ${total.toLocaleString('en-US')} ${noun(total, 'file')} in ${roots.toLocaleString('en-US')} output ${noun(roots, 'folder')}.`;
}
function el(tag, text, className) {
const node = document.createElement(tag);
if (text !== undefined) node.textContent = text;
if (className) node.className = className;
return node;
}
/**
* "Add image source" (the concept's `simpleInput`, D127): the server checks the folder; a refused one keeps the
* dialog open with the server's reason under the field. Enter in the field adds, as the Add button does.
*/
function openAddSource({ api, scope, trigger, listed, onAdd }) {
const input = el('input');
const reason = el('p', undefined, 'small');
reason.setAttribute('role', 'alert');
const cancel = createButton('Cancel');
const add = createButton('Add', 'ca-primary');
let open = true;
const refuse = text => {
reason.textContent = text;
if (!reason.isConnected) actions.before(reason);
input.setAttribute('aria-invalid', 'true');
input.focus();
};
scope.listen(input, 'input', () => {
reason.remove();
input.removeAttribute('aria-invalid');
});
scope.listen(input, 'keydown', event => {
if (event.key !== 'Enter' || event.isComposing) return;
event.preventDefault();
submit();
});
scope.listen(cancel, 'click', () => closeWorkspaceDialog());
scope.listen(add, 'click', () => submit());
async function submit() {
const path = input.value.trim();
if (!path) { input.focus(); return; }
if (add.getAttribute('aria-disabled') === 'true') return;
add.setAttribute('aria-disabled', 'true');
try {
const result = await api.post(VALIDATE_URL, { path }, { signal: scope.signal });
if (!open) return;
if (!result.valid) refuse(result.reason ?? VALIDATE_FAILED_TEXT);
else if (listed().includes(result.path)) refuse(DUPLICATE_TEXT);
else {
closeWorkspaceDialog();
onAdd(result);
}
} catch {
if (open) refuse(VALIDATE_FAILED_TEXT);
} finally {
add.removeAttribute('aria-disabled');
}
}
const actions = el('div', undefined, 'dialog-actions');
actions.append(cancel, add);
openContentDialog({ scope, title: 'Add image source', trigger, nodes: [field('Folder path', input, 'action-input'), actions],
onClose: () => { open = false; } });
input.focus();
}
/** The row's control (`data-setting`), wherever the Toolkit switch nests it. */
const controlOf = (row, id) => row.querySelector(`[data-setting="${id}"]`);
/**
* Mount Sources in `container` (System's `.settings-layout`); returns the section's nodes. `activity` is the page's
* Activity store (scans start through it), `drafts` the System drafts.
*/
export function mountSources(container, { api, activity, scope, drafts, notify = showWorkspaceToast, storage = defaultStorage,
latestOnly = window.ApiClient.latestOnly, STALE = window.ApiClient.STALE }) {
const statusRead = latestOnly({ lifetime: scope.signal });
// undefined while read, null when the read failed, else GET /settings' `sources`.
let sources;
const nodes = [];
const scanAll = trigger => openMetadataScan({ scope, store: activity, trigger, sources: sources?.effective?.length ?? 0 });
if (!noticeDismissed(storage)) {
const notice = noticeNode({ scope,
onLater: () => { dismissNotice(storage); notice.remove(); },
onReview: () => {
dismissNotice(storage);
// The pressed button leaves with the notice, so focus returns to the list's Add source.
notice.remove();
scanAll(addButton);
} });
watchNoticeDismissal({ scope, storage, onDismissed: () => notice.remove() });
nodes.push(notice);
}
// #320 D199: the unavailable thumbnail folder's notice, first on the page while it lasts.
const thumbnailLocation = () => drafts.get('host', 'thumbnail_location');
const location = locationBlock({ api, store: activity, scope, location: thumbnailLocation, notify });
const unavailable = noticeSlot({ api, scope, location: thumbnailLocation, notify, onChange: () => location.focus(),
onDone: () => drafts.reloadHost?.() });
const paintLocation = () => {
location.paint();
unavailable.paint();
};
scope.cleanup(drafts.subscribe(paintLocation));
nodes.push(unavailable.anchor);
paintLocation();
const surface = el('section', undefined, 'surface');
const heading = el('div', undefined, 'panel-heading');
const addButton = createButton('+ Add source');
heading.append(el('h2', 'Image sources'), addButton);
const list = el('div');
list.id = 'source-list';
surface.append(heading, el('p', 'The same folders power browsing, monitoring and metadata enrichment.'), list);
const roots = () => drafts.get('host', ROOTS_KEY) ?? sources?.configured?.map(source => source.path) ?? [];
const setRoots = next => drafts.set('host', ROOTS_KEY, draftRoots(next, drafts.saved('host')[ROOTS_KEY]));
scope.listen(addButton, 'click', () => openAddSource({ api, scope, trigger: addButton, listed: roots,
onAdd: ({ path, label }) => {
addedLabels.set(path, label);
setRoots([...roots(), path]);
} }));
const monitoring = settingRow({ id: 'monitoring', label: 'Watch for new outputs',
description: 'New files enter the same library. Existing scans do not count as new runs.', control: { type: 'toggle' },
scope, onChange: value => drafts.set('host', 'monitoring_enabled', value) });
const foreign = settingRow({ id: FOREIGN_RUNS.id, label: FOREIGN_RUNS.label, description: FOREIGN_RUNS.description,
control: { type: 'toggle' }, scope, onChange: value => drafts.set('host', FOREIGN_RUNS.key, value) });
const workers = settingRow({ id: 'workers', label: 'Worker threads',
description: 'Balance background scans and thumbnail work against generation.', control: { type: 'range', value: 4 },
scope, onChange: value => drafts.set('host', 'worker_threads', value) });
const hostRows = [[monitoring, 'monitoring', 'monitoring_enabled'], [foreign, FOREIGN_RUNS.id, FOREIGN_RUNS.key],
[workers, 'workers', 'worker_threads']]
.map(([row, id, key]) => ({ input: controlOf(row, id), key, text: row.children[0], note: null }));
for (const row of hostRows) row.input.dataset.key = row.key;
surface.append(monitoring, foreign, workers,
operationRow({ scope, title: 'Enrich existing metadata', description: 'Resolve both prompts, deduplicate recipes, preserve uncertain links.',
label: 'Review scan', onClick: scanAll }),
operationRow({ scope, title: 'Thumbnail generation', description: 'Generate missing previews; rebuilding all is a separate explicit option.',
label: 'Review options', onClick: trigger => openThumbnailReview({ api, store: activity, scope, trigger,
location: thumbnailLocation() }) }),
// #320 D196: where previews are kept; never a draft (Move thumbnails is its only writer).
location.node,
// D98: the legacy gallery's Refresh, which rereads every source's file list.
operationRow({ scope, title: 'Refresh files', description: 'Read every source again to find files added or removed outside ComfyUI.',
label: 'Refresh', onClick: refresh }));
nodes.push(surface);
async function refresh(button) {
if (button.getAttribute('aria-disabled') === 'true') return;
button.setAttribute('aria-disabled', 'true');
try {
const result = await api.post(RESCAN_URL, {}, { signal: scope.signal });
if (!scope.signal.aborted) notify(refreshedText(result));
} catch {
if (!scope.signal.aborted) notify(REFRESH_FAILED_TEXT);
} finally {
button.removeAttribute('aria-disabled');
}
}
function sourceNode(row) {
const actions = [];
if (row.scan) {
const scan = createButton(row.scan.label);
scope.listen(scan, 'click', () => openMetadataScan({ scope, store: activity, trigger: scan, sources: 1,
roots: [row.scan.root], paths: [row.scan.path], onStale: reread }));
actions.push(scan);
}
if (row.remove !== null) {
// D98: a plain button; nothing is removed until Save.
const remove = createButton('Remove');
scope.listen(remove, 'click', () => setRoots(roots().filter(path => path !== row.remove)));
actions.push(remove);
}
const node = el('div', undefined, 'operation');
// D128: the source the last save was refused for; the status says why.
if (row.remove !== null && row.remove === drafts.invalidSource?.()) node.setAttribute('aria-invalid', 'true');
const text = el('div');
text.append(el('h3', row.title), el('p', row.description));
const group = el('div', undefined, 'row-actions');
group.append(...actions);
node.append(text, group);
return node;
}
function paintList() {
if (sources === undefined) {
const loading = el('p', LOADING_TEXT, 'small');
loading.setAttribute('role', 'status');
list.replaceChildren(loading);
return;
}
if (sources === null) {
const alert = el('p', LOAD_FAILED_TEXT);
alert.setAttribute('role', 'alert');
const retry = createButton('Retry');
scope.listen(retry, 'click', () => {
load();
if (drafts.hostState?.() === 'failed') drafts.reloadHost();
});
list.replaceChildren(alert, retry);
return;
}
const { rows, note } = sourceRows({ sources, saved: drafts.saved('host')[ROOTS_KEY], current: drafts.get('host', ROOTS_KEY) });
list.replaceChildren(...rows.map(sourceNode), ...(note ? [el('p', note, 'small')] : []));
}
function paintControls() {
const unavailable = drafts.hostState?.() === 'failed';
for (const row of hostRows) {
const value = drafts.get('host', row.key);
row.input.disabled = value === undefined;
if (row.key === FOREIGN_RUNS.key) {
paintForeign(row, value, unavailable);
continue;
}
if (row.key === 'worker_threads') {
// D113: the range ends at the server's core count (the concept's fixed 16).
const cpus = drafts.get('host', 'cpu_count');
if (Number.isSafeInteger(cpus) && cpus >= 1) row.input.max = String(cpus);
if (value !== undefined) row.input.value = String(value);
} else if (value !== undefined) row.input.checked = value === true;
if (unavailable && value === undefined) {
if (!row.note) { row.note = el('p', HOST_UNAVAILABLE, 'small'); row.text.append(row.note); }
} else if (row.note) { row.note.remove(); row.note = null; }
}
}
// D158: disabled while monitoring is drafted off, with the reason under the description.
function paintForeign(row, value, unavailable) {
const stopping = sources?.effective?.some(source => source.watch === 'stopping') === true;
const state = foreignRunsState({ value, monitoring: drafts.get('host', 'monitoring_enabled'), stopping });
row.input.disabled = state.disabled;
if (state.checked !== null) row.input.checked = state.checked;
const note = unavailable && value === undefined ? HOST_UNAVAILABLE : state.note;
if (note && row.note?.textContent !== note) {
row.note?.remove();
row.note = el('p', note, 'small');
row.note.id = `${FOREIGN_RUNS.id}-note`;
row.text.append(row.note);
} else if (!note && row.note) { row.note.remove(); row.note = null; }
// The note is the switch's description, so a screen reader says why it is disabled.
if (row.note) row.input.setAttribute('aria-describedby', row.note.id);
else row.input.removeAttribute('aria-describedby');
}
// The sources changed since this page read them (a refused per-source scan): read them and the saved values again.
function reread() {
load();
drafts.reloadHost?.();
}
async function load() {
sources = undefined;
paintList();
let answer;
try {
answer = await statusRead(signal => api.get(SETTINGS_URL, { signal, cache: 'no-store' }));
} catch {
answer = null;
}
if (answer === STALE || scope.signal.aborted) return;
sources = answer?.sources ?? null;
paintList();
paintControls();
}
// A save (or a reread after a conflict) changed the saved sources or monitoring: read their state again. The first
// host read on mount is not a change.
const savedState = () => {
const saved = drafts.saved('host');
return saved[ROOTS_KEY] === undefined ? undefined : JSON.stringify([saved[ROOTS_KEY], saved.monitoring_enabled]);
};
let state = savedState();
scope.cleanup(drafts.subscribe(() => {
const previous = state;
state = savedState();
paintControls();
if (previous !== undefined && state !== previous) load();
else if (sources) paintList();
}));
paintControls();
load();
return nodes;
}