Files
archie/ui/src/settings/config-row.js
T
sam fb7fba4232 fix(webui): rebuild the Configuration page layout so values stop colliding
The page had three compounding layout faults, all visible on a wide
screen and all still present after the earlier spacing pass:

  1. .cfg-row used justify-content: space-between on a full-width card,
     so the label sat at the far left and its value at the far right with
     roughly a thousand pixels between them. Reading one pair meant
     tracking across the viewport. The earlier "tighten KV row spacing"
     change reduced vertical padding only, which is why the complaint it
     was meant to answer survived it.
  2. .cfg-value had no min-width: 0, so a long value could not shrink
     inside its flex parent. It overflowed into the Edit button, which is
     why 400 rendered as "40" and 100000000 was clipped mid-number.
  3. The locked-row reason was a flex sibling of that overflowing value,
     so it ran onto the end of the path with no separation, rendering
     ".../archie/work" and "pins the daemon's working layout" as one
     unbroken string.

Replace the flex row with a three-column grid: label, value, actions.
Label and value are adjacent and left-aligned so the eye travels a fixed
short distance at any viewport width; the value column is minmax(0, 1fr)
so it can actually shrink, truncating with an ellipsis and keeping the
full text in a title attribute; actions occupy a reserved column that
nothing can overlap. Secondary text (locked reason, override marker,
validation error) gets its own grid row instead of sharing the value
line. Narrow viewports stack label over value rather than truncating
both to a few characters.

Extract the row renderer into settings/config-row.js. settings.js
imports its own stylesheet, which the node test shim cannot parse, so
the rendering logic was untestable where it sat; this mirrors
tasks/task-row.js. Seven tests now cover the two collision bugs
structurally, asserting on per-child text rather than the row's
flattened string so a future layout change cannot pass by merging the
fields back into one node.

Also stop sharing .cfg-rows with the Workflows "Start work" form. A
class owned by the Configuration page meant restyling Configuration
could silently restyle that form; it is now .wf-form, owned by the
feature that uses it.
2026-08-10 03:00:56 +10:00

166 lines
6.2 KiB
JavaScript

import { api } from "../base/api.js";
import { el, mount } from "../base/dom.js";
/**
* One key/value row on the Configuration page, and its inline edit flow.
*
* Split out of settings.js so it can be unit-tested: settings.js imports
* its own stylesheet, which the node test shim cannot parse. Same reason
* tasks/task-row.js exists separately from tasks.js.
*
* A row renders as a three-column grid: label, value, actions. That shape
* is the fix for three faults in the previous flex layout, all of which
* were visible on screen:
*
* - label and value were pushed to opposite ends of a full-width card by
* `justify-content: space-between`, so reading a pair meant tracking
* across the viewport;
* - the value could not shrink (no `min-width: 0`), so long values
* overflowed into the Edit button and appeared truncated ("400" as
* "40");
* - the locked reason sat beside the overflowing value and ran onto the
* end of it, rendering "...archie/work" and "pins the daemon's working
* layout" as one unbroken string.
*/
// valueText normalises a config value for display. 0 and false are real
// values and must not collapse to the em dash the way null or an unset
// string does, so the emptiness test is explicit rather than falsy.
export function valueText(value) {
if (value === null || value === undefined || value === "") return "—";
return String(value);
}
// row renders one label/value pair. opts.key makes it editable via a
// PATCH to /api/config; opts.locked[key] disables it with the server's
// reason; opts.overridden marks a key whose file value is shadowed by the
// runtime overlay and offers a reset; no key means a plain read-only row
// (structured values and provenance).
//
// The value carries a title attribute because the column truncates with
// an ellipsis: a long path or gate command must stay recoverable without
// widening the row back into the action column.
export function row(label, value, opts) {
const text = valueText(value);
const labelEl = el("span.kv-label", label);
const valueEl = el(`span.kv-value.mono${text === "—" ? ".is-empty" : ""}`, { title: text }, text);
if (!opts?.key) return el("div.kv", labelEl, valueEl);
const lockedReason = opts.locked?.[opts.key];
if (lockedReason) {
// The reason gets its own grid line rather than sitting beside the
// value, which is what produced the run-together string above.
return el("div.kv.is-locked", labelEl, valueEl, el("span.kv-note", lockedReason));
}
const overridden = opts.overridden?.includes(opts.key);
const editBtn = el("button.kv-btn", { type: "button", title: `Edit ${label}` }, "Edit");
const resetBtn = overridden
? el("button.kv-btn", { type: "button", title: "Discard the dashboard value and fall back to the config file" }, "Reset")
: null;
const actions = el(
"div.kv-actions",
overridden && el("span.kv-flag", { title: "Set from the dashboard; this shadows the value in the config file" }, "overridden"),
editBtn,
resetBtn,
);
const rowEl = el("div.kv", labelEl, valueEl, actions);
editBtn.addEventListener("click", () => startEdit(rowEl, label, opts, [labelEl, valueEl, actions]));
resetBtn?.addEventListener("click", () => resetOverride(rowEl, resetBtn, opts));
return rowEl;
}
// resetOverride deletes one runtime-overlay row via the reset seam, then
// re-renders so the row returns to its file value. A failure reports on
// the row's own note line; the previous version overwrote the "overridden"
// marker's text with the error, destroying the marker to show it.
function resetOverride(rowEl, btn, opts) {
const { key, onSaved } = opts;
btn.disabled = true;
api
.configReset(key)
.then(() => onSaved?.())
.catch((err) => {
btn.disabled = false;
note(rowEl, String(err.message || err));
});
}
// note puts a message on the row's secondary line, creating the line if
// the row does not already have one.
function note(rowEl, message) {
let target = rowEl.querySelector?.(".kv-note");
if (!target) {
target = el("span.kv-note.is-error");
rowEl.appendChild(target);
}
target.classList.add("is-error");
target.textContent = message;
}
function startEdit(rowEl, label, opts, restore) {
const { key, type, raw, onSaved } = opts;
const input = type === "bool"
? el(
"select.kv-input",
el("option", { value: "true", selected: raw === true }, "true"),
el("option", { value: "false", selected: raw === false }, "false"),
)
: el("input.kv-input", { type: "text", value: String(raw ?? "") });
const save = el("button.kv-save", { type: "button" }, "Save");
const cancel = el("button.kv-cancel", { type: "button" }, "Cancel");
const error = el("span.kv-note.is-error");
mount(rowEl, el("span.kv-label", label), input, el("div.kv-actions", save, cancel), error);
input.focus?.();
input.select?.();
const done = () => mount(rowEl, ...restore);
cancel.addEventListener("click", done);
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault?.();
save.click();
}
if (e.key === "Escape") done();
});
save.addEventListener("click", async () => {
const parsed = parseEdit(type, input.value);
if (parsed.error) {
error.textContent = parsed.error;
return;
}
save.disabled = true;
try {
await api.configUpdate({ [key]: parsed.value });
onSaved?.();
} catch (err) {
// The server's reason (validation failure, denylisted key) is the
// part the operator can act on; show it instead of the status.
error.textContent = String(err.message || err);
save.disabled = false;
}
});
}
// parseEdit converts the input text to the JSON type the config field
// expects. Durations and strings pass through (the server validates
// duration syntax); ints are parsed client-side so a typo fails before a
// round trip.
export function parseEdit(type, text) {
switch (type) {
case "int": {
const n = parseInt(text, 10);
if (Number.isNaN(n)) return { error: "Enter a whole number" };
return { value: n };
}
case "bool":
return { value: text === "true" };
default:
return { value: text };
}
}