mandrel 2.124.0 → 2.125.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.agents/docs/agentrc-reference.json +1 -0
  2. package/.agents/docs/configuration.md +2 -0
  3. package/.agents/docs/runbooks/dispatch-board.md +174 -0
  4. package/.agents/docs/runbooks/dispatch.md +219 -0
  5. package/.agents/docs/runbooks/session-titles.md +52 -0
  6. package/.agents/schemas/agentrc.schema.json +60 -0
  7. package/.agents/scripts/acceptance-eval.js +11 -2
  8. package/.agents/scripts/dispatch.js +400 -0
  9. package/.agents/scripts/lib/child-detached.js +48 -0
  10. package/.agents/scripts/lib/child-exec.js +11 -0
  11. package/.agents/scripts/lib/config/delivery-routing.js +15 -12
  12. package/.agents/scripts/lib/config/temp-paths.js +26 -0
  13. package/.agents/scripts/lib/config-settings-schema-delivery.js +45 -0
  14. package/.agents/scripts/lib/dispatch/environments.js +135 -0
  15. package/.agents/scripts/lib/dispatch/launch-cloud.js +164 -0
  16. package/.agents/scripts/lib/dispatch/launch-local.js +77 -0
  17. package/.agents/scripts/lib/dispatch/liveness.js +21 -0
  18. package/.agents/scripts/lib/dispatch/record.js +54 -0
  19. package/.agents/scripts/lib/dispatch/status.js +285 -0
  20. package/.agents/scripts/lib/dispatch/targets.js +101 -0
  21. package/.agents/scripts/lib/dispatch/verdict.js +207 -0
  22. package/.agents/scripts/lib/generated/agentrc-validator.js +2 -2
  23. package/.agents/scripts/lib/observability/source-classifier.js +2 -0
  24. package/.agents/scripts/lib/orchestration/single-story-close/runner.js +2 -0
  25. package/.agents/scripts/lib/orchestration/story-handoff.js +6 -0
  26. package/.agents/scripts/lib/orchestration/story-stage-publish.js +147 -0
  27. package/.agents/scripts/lib/orchestration/ticketing/reads.js +2 -0
  28. package/.agents/scripts/session-title.js +187 -0
  29. package/.agents/scripts/single-story-confirm-merge.js +7 -0
  30. package/.agents/scripts/single-story-init.js +3 -0
  31. package/.agents/workflows/helpers/audit-lens-core.md +4 -0
  32. package/.agents/workflows/helpers/deliver-light.md +4 -1
  33. package/.agents/workflows/helpers/qa-core.md +2 -0
  34. package/.agents/workflows/mandrel-deliver.md +7 -3
  35. package/.agents/workflows/mandrel-orchestrate.md +2 -0
  36. package/.agents/workflows/mandrel-plan.md +4 -0
  37. package/README.md +8 -0
  38. package/bin/mandrel.js +35 -13
  39. package/docs/CHANGELOG.md +14 -0
  40. package/lib/cli/dispatch.js +27 -0
  41. package/lib/cli/ui/accounts.js +81 -0
  42. package/lib/cli/ui/board-model.js +372 -0
  43. package/lib/cli/ui/board.css +525 -0
  44. package/lib/cli/ui/board.html +80 -0
  45. package/lib/cli/ui/board.js +676 -0
  46. package/lib/cli/ui/log-tail.js +89 -0
  47. package/lib/cli/ui/panel-model.js +100 -0
  48. package/lib/cli/ui/preferences.js +136 -0
  49. package/lib/cli/ui/request-guard.js +108 -0
  50. package/lib/cli/ui/server.js +211 -0
  51. package/lib/cli/ui/sources.js +202 -0
  52. package/lib/cli/ui/story-detail.js +92 -0
  53. package/lib/cli/ui.js +114 -0
  54. package/package.json +1 -1
@@ -0,0 +1,676 @@
1
+ /* node:coverage ignore file -- browser-only DOM glue; its logic is board-model.js */
2
+ // lib/cli/ui/board.js
3
+ /**
4
+ * The `mandrel ui` page script: fetch, render and wire events. Every view
5
+ * decision comes from `board-model.js`; this file only turns the model into
6
+ * elements, built with `textContent` and `setAttribute` alone, and posts the
7
+ * action a drop or the Move menu chose. It implements no dispatch rule.
8
+ */
9
+
10
+ import {
11
+ buildBoard,
12
+ dropZones,
13
+ environmentCards,
14
+ moveOptions,
15
+ storyDetailView,
16
+ zoneVerdict,
17
+ } from './board-model.js';
18
+ import {
19
+ connectionsView,
20
+ environmentRows,
21
+ headerView,
22
+ nextPanelView,
23
+ storyOfView,
24
+ } from './panel-model.js';
25
+ import {
26
+ DEFAULT_PREFERENCES,
27
+ doneHeader,
28
+ loadPreferences,
29
+ savePreferences,
30
+ settingsFields,
31
+ withPreference,
32
+ } from './preferences.js';
33
+
34
+ const $ = (id) => document.getElementById(id);
35
+ const token = document.querySelector('meta[name="mandrel-token"]').content;
36
+ const ui = {
37
+ state: null,
38
+ connections: null,
39
+ dragging: null,
40
+ pending: null,
41
+ view: null,
42
+ detailId: null,
43
+ offline: 0,
44
+ prefs: { ...DEFAULT_PREFERENCES },
45
+ timer: null,
46
+ };
47
+
48
+ /** Browser storage, or `null` where reading it throws. */
49
+ const storage = (() => {
50
+ try {
51
+ return window.localStorage;
52
+ } catch {
53
+ return null;
54
+ }
55
+ })();
56
+
57
+ /** The header button that opens each named panel view. */
58
+ const PANEL_BUTTONS = {
59
+ environments: 'panel-environments-button',
60
+ connections: 'panel-connections-button',
61
+ settings: 'panel-settings-button',
62
+ };
63
+
64
+ function el(tag, attrs = {}, ...children) {
65
+ const node = document.createElement(tag);
66
+ for (const [name, value] of Object.entries(attrs)) {
67
+ if (value !== null && value !== undefined) node.setAttribute(name, value);
68
+ }
69
+ for (const child of children) {
70
+ if (child !== null && child !== undefined) node.append(child);
71
+ }
72
+ return node;
73
+ }
74
+
75
+ /** A `type="button"` button that runs `onClick`. */
76
+ function button(attrs, text, onClick) {
77
+ const node = el('button', { type: 'button', ...attrs }, text);
78
+ node.addEventListener('click', onClick);
79
+ return node;
80
+ }
81
+
82
+ let toastTimer = null;
83
+ function toast(text, isError = false) {
84
+ const box = $('toast');
85
+ box.textContent = text;
86
+ box.classList.toggle('error', isError);
87
+ box.hidden = false;
88
+ clearTimeout(toastTimer);
89
+ toastTimer = setTimeout(() => {
90
+ box.hidden = true;
91
+ }, 5000);
92
+ }
93
+
94
+ async function api(path, body) {
95
+ const init = { headers: { 'X-Mandrel-Token': token } };
96
+ if (body) {
97
+ init.method = 'POST';
98
+ init.headers['Content-Type'] = 'application/json';
99
+ init.body = JSON.stringify(body);
100
+ }
101
+ const res = await fetch(path, init);
102
+ return { status: res.status, json: await res.json() };
103
+ }
104
+
105
+ function storyById(id) {
106
+ return ui.state?.stories.find((s) => s.id === id) ?? null;
107
+ }
108
+
109
+ /** Copy a command for the operator to run; it is never run here. */
110
+ async function copyCommand(command) {
111
+ try {
112
+ await navigator.clipboard.writeText(command);
113
+ toast(`Copied ${command}`);
114
+ } catch {
115
+ toast(`Run ${command}`);
116
+ }
117
+ }
118
+
119
+ async function perform(story, verdict) {
120
+ if (verdict.kind === 'none') return;
121
+ if (verdict.kind === 'refused') {
122
+ toast(verdict.text, true);
123
+ return;
124
+ }
125
+ if (verdict.kind === 'copy') {
126
+ await copyCommand(verdict.text);
127
+ return;
128
+ }
129
+ if (verdict.kind === 'confirm') {
130
+ ui.pending = { story, verdict };
131
+ $('confirm-text').textContent = `#${story.id}: ${verdict.text}.`;
132
+ $('confirm-stop').showModal();
133
+ return;
134
+ }
135
+ await post(story, verdict);
136
+ }
137
+
138
+ async function post(story, verdict) {
139
+ try {
140
+ const { status, json } = await api('/api/action', {
141
+ action: verdict.action,
142
+ storyId: story.id,
143
+ environment: verdict.environment,
144
+ });
145
+ if (status === 200) toast(`#${story.id}: ${verdict.text}`);
146
+ else toast(json.detail ?? json.reason ?? json.error ?? 'Refused', true);
147
+ } catch {
148
+ toast('The board server did not answer.', true);
149
+ }
150
+ await refresh();
151
+ }
152
+
153
+ /** A link that opens in a new tab; `href` is already a GitHub URL or null. */
154
+ function link(href, text, testId = null) {
155
+ return el(
156
+ 'a',
157
+ {
158
+ href,
159
+ target: '_blank',
160
+ rel: 'noopener noreferrer',
161
+ 'data-testid': testId,
162
+ },
163
+ text,
164
+ );
165
+ }
166
+
167
+ function statusRow(card) {
168
+ const { stage, steps, liveness, running, updated } = card.status;
169
+ const stepper = el('ol', {
170
+ class: 'stepper',
171
+ 'aria-label': 'Delivery stage',
172
+ });
173
+ for (const step of steps) {
174
+ stepper.append(
175
+ el('li', { class: `step ${step.state}`, title: step.name }, step.name),
176
+ );
177
+ }
178
+ const badge = el(
179
+ 'span',
180
+ {
181
+ class: `liveness ${liveness.state} ${liveness.warning ? 'warning' : ''}`,
182
+ 'data-testid': `card-liveness-${card.id}`,
183
+ },
184
+ el('span', { class: 'dot', 'aria-hidden': 'true' }),
185
+ liveness.text,
186
+ );
187
+ const times = [stage, running, updated].filter(Boolean).join(' · ');
188
+ return el(
189
+ 'div',
190
+ { class: 'card-status', 'data-testid': `card-status-${card.id}` },
191
+ stepper,
192
+ el('div', { class: 'card-meta' }, badge, el('span', {}, times)),
193
+ );
194
+ }
195
+
196
+ function prParts(pr) {
197
+ if (!pr) return [];
198
+ return [link(pr.url, `PR #${pr.number}`), el('span', {}, pr.checks)];
199
+ }
200
+
201
+ function cardMeta(card) {
202
+ const meta = el('div', { class: 'card-meta' }, ...prParts(card.pr));
203
+ if (card.unmet.length > 0) {
204
+ const ids = card.unmet.map((id) => `#${id}`).join(', ');
205
+ meta.append(el('span', { class: 'unmet' }, `waits on ${ids}`));
206
+ }
207
+ if (card.environment) meta.append(el('span', {}, card.environment));
208
+ return meta;
209
+ }
210
+
211
+ /** One button per allowed move: the Move menu and the detail view share it. */
212
+ function moveButtons(story, role = null) {
213
+ const options = moveOptions(story, dropZones(ui.state));
214
+ if (options.length === 0) return [el('span', {}, 'No allowed moves')];
215
+ return options.map((option) => {
216
+ const item = el('button', { type: 'button', role }, option.text);
217
+ item.addEventListener('click', () => perform(story, option));
218
+ return item;
219
+ });
220
+ }
221
+
222
+ function moveMenu(story, holder) {
223
+ const menu = el('div', { class: 'move-menu', role: 'menu' });
224
+ menu.append(...moveButtons(story, 'menuitem'));
225
+ holder.append(menu);
226
+ }
227
+
228
+ function setPanelView(view) {
229
+ ui.view = view;
230
+ ui.detailId = storyOfView(view);
231
+ for (const [name, id] of Object.entries(PANEL_BUTTONS)) {
232
+ $(id).setAttribute('aria-expanded', String(view === name));
233
+ }
234
+ $('side-panel').hidden = view === null;
235
+ document.body.classList.toggle('panel-open', view !== null);
236
+ if (view === null) $('side-panel-view').replaceChildren();
237
+ }
238
+
239
+ function closePanel() {
240
+ setPanelView(null);
241
+ }
242
+
243
+ /** Open a view; asking for the open one again closes the panel. */
244
+ function togglePanel(view) {
245
+ setPanelView(nextPanelView(ui.view, view));
246
+ if (ui.detailId !== null) {
247
+ $('side-panel-view').replaceChildren(el('p', {}, 'Loading…'));
248
+ refreshDetail();
249
+ } else if (ui.view === 'settings') {
250
+ renderSettings();
251
+ } else {
252
+ renderPanel();
253
+ if (ui.view === 'connections') refreshConnections();
254
+ }
255
+ }
256
+
257
+ const toggleDetail = (id) => togglePanel(`story-${id}`);
258
+
259
+ /** `[term, value, testId?]` rows as a definition list. */
260
+ function definitionList(rows) {
261
+ return el(
262
+ 'dl',
263
+ {},
264
+ ...rows.flatMap(([term, value, testId]) => [
265
+ el('dt', {}, term),
266
+ el('dd', { 'data-testid': testId }, String(value)),
267
+ ]),
268
+ );
269
+ }
270
+
271
+ function resumeBlock(command) {
272
+ if (!command) return null;
273
+ const copy = button(
274
+ { 'data-testid': 'story-detail-resume-copy' },
275
+ 'Copy',
276
+ () => copyCommand(command),
277
+ );
278
+ return el('div', { class: 'resume' }, el('code', {}, command), copy);
279
+ }
280
+
281
+ /** `build(items)` under a heading, or `empty` when there are none. */
282
+ function section(title, items, build, empty) {
283
+ return [
284
+ el('h3', {}, title),
285
+ items.length > 0 ? build(items) : el('p', {}, empty),
286
+ ];
287
+ }
288
+
289
+ const listOf = (lines) =>
290
+ el('ol', { class: 'timeline' }, ...lines.map((line) => el('li', {}, line)));
291
+
292
+ function detailElement(detail) {
293
+ const view = storyDetailView(detail);
294
+ const story = storyById(detail.id);
295
+ const issue = view.issueUrl
296
+ ? link(view.issueUrl, 'GitHub issue', 'story-detail-issue-link')
297
+ : null;
298
+ return el(
299
+ 'section',
300
+ { class: 'panel-view', 'data-testid': 'story-detail' },
301
+ el('h2', {}, view.heading),
302
+ el('p', { class: 'detail-links' }, issue, ...prParts(view.pr)),
303
+ definitionList(view.facts),
304
+ el('h3', {}, 'Actions'),
305
+ el(
306
+ 'div',
307
+ { class: 'detail-actions' },
308
+ ...(story ? moveButtons(story) : []),
309
+ ),
310
+ ...section('Stage timeline', view.timeline, listOf, 'No stage published.'),
311
+ ...section(
312
+ 'Dispatch record',
313
+ view.record,
314
+ definitionList,
315
+ 'No dispatch record.',
316
+ ),
317
+ resumeBlock(view.resumeCommand),
318
+ el('h3', {}, 'Log'),
319
+ el('pre', { class: 'log', 'data-testid': 'story-detail-log' }, view.log),
320
+ );
321
+ }
322
+
323
+ async function refreshDetail() {
324
+ const id = ui.detailId;
325
+ if (id === null) return;
326
+ try {
327
+ const { status, json } = await api(
328
+ `/api/story?id=${id}&done=${ui.prefs.doneLimit}`,
329
+ );
330
+ if (ui.detailId !== id) return;
331
+ $('side-panel-view').replaceChildren(
332
+ status === 200
333
+ ? detailElement(json)
334
+ : el('p', {}, `Story #${id} is not on the board.`),
335
+ );
336
+ } catch {
337
+ toast('Could not read the Story detail.', true);
338
+ }
339
+ }
340
+
341
+ /** Double-click, the context menu, Enter or Space open the detail view. */
342
+ function bindDetailTriggers(node, id) {
343
+ node.addEventListener('dblclick', (event) => {
344
+ if (!event.target.closest('button, a')) toggleDetail(id);
345
+ });
346
+ node.addEventListener('contextmenu', (event) => {
347
+ event.preventDefault();
348
+ toggleDetail(id);
349
+ });
350
+ node.addEventListener('keydown', (event) => {
351
+ if (event.target !== node) return;
352
+ if (event.key !== 'Enter' && event.key !== ' ') return;
353
+ event.preventDefault();
354
+ toggleDetail(id);
355
+ });
356
+ }
357
+
358
+ function cardElement(card) {
359
+ const story = storyById(card.id);
360
+ const node = el('article', {
361
+ class: `card ${story.column === 'blocked' ? 'blocked' : ''} ${story.column === 'delivering' ? 'live' : ''}`,
362
+ draggable: 'true',
363
+ tabindex: '0',
364
+ 'aria-label': `#${card.id} ${card.title}`,
365
+ 'data-testid': `card-${card.id}`,
366
+ });
367
+ const details = el(
368
+ 'button',
369
+ {
370
+ type: 'button',
371
+ 'aria-label': `Details for #${card.id}`,
372
+ 'data-testid': `card-details-${card.id}`,
373
+ },
374
+ '⋯',
375
+ );
376
+ details.addEventListener('click', () => toggleDetail(card.id));
377
+ const move = el(
378
+ 'button',
379
+ {
380
+ type: 'button',
381
+ 'data-testid': `card-move-${card.id}`,
382
+ 'aria-expanded': 'false',
383
+ },
384
+ 'Move',
385
+ );
386
+ move.addEventListener('click', () => {
387
+ const open = node.querySelector('.move-menu');
388
+ if (open) open.remove();
389
+ else moveMenu(story, node);
390
+ move.setAttribute('aria-expanded', String(!open));
391
+ });
392
+ node.append(
393
+ el('div', { class: 'card-id' }, `#${card.id}`),
394
+ el('p', { class: 'card-title' }, card.title),
395
+ ...(card.status ? [statusRow(card)] : []),
396
+ cardMeta(card),
397
+ el('div', { class: 'card-actions' }, move, details),
398
+ );
399
+ bindDetailTriggers(node, card.id);
400
+ node.addEventListener('dragstart', (event) => {
401
+ ui.dragging = story;
402
+ event.dataTransfer.setData('text/plain', String(story.id));
403
+ showHints(story);
404
+ });
405
+ node.addEventListener('dragend', clearHints);
406
+ return node;
407
+ }
408
+
409
+ function laneElement(lane) {
410
+ const meta = lane.target ? `${lane.target} · ${lane.header}` : lane.header;
411
+ const node = el(
412
+ 'div',
413
+ {
414
+ class: `lane ${lane.online ? '' : 'offline'}`,
415
+ 'data-testid': `lane-${lane.name}`,
416
+ 'data-zone': lane.name,
417
+ },
418
+ el('h3', {}, lane.name, el('span', { class: 'zone-hint' })),
419
+ el('div', { class: 'lane-meta' }, meta),
420
+ );
421
+ const cards = el('div', { class: 'cards' });
422
+ cards.append(...lane.cards.map(cardElement));
423
+ node.append(cards);
424
+ bindZone(node);
425
+ return node;
426
+ }
427
+
428
+ function renderBoard() {
429
+ const board = buildBoard(ui.state, ui.connections);
430
+ for (const column of board.columns) {
431
+ const section = document.querySelector(
432
+ `[data-testid="column-${column.id}"]`,
433
+ );
434
+ if (column.lanes) {
435
+ section
436
+ .querySelector('.lanes')
437
+ .replaceChildren(...column.lanes.map(laneElement));
438
+ } else {
439
+ section
440
+ .querySelector('.cards')
441
+ .replaceChildren(...column.cards.map(cardElement));
442
+ }
443
+ }
444
+ ui.offline = board.offline;
445
+ }
446
+
447
+ function environmentsElement() {
448
+ const cards = environmentCards(ui.state, ui.connections).map((env) =>
449
+ el(
450
+ 'article',
451
+ {
452
+ class: `env-card ${env.online ? '' : 'offline'}`,
453
+ 'data-testid': `env-card-${env.name}`,
454
+ },
455
+ el('h3', { class: 'mono' }, env.name),
456
+ definitionList(environmentRows(env)),
457
+ ),
458
+ );
459
+ return el(
460
+ 'section',
461
+ { class: 'panel-view', 'data-testid': 'environments-view' },
462
+ el('h2', {}, 'Environments'),
463
+ ...cards,
464
+ );
465
+ }
466
+
467
+ function connectionsElement() {
468
+ const view = connectionsView(ui.connections);
469
+ return el(
470
+ 'section',
471
+ { class: 'panel-view', 'data-testid': 'connections-view' },
472
+ el('h2', {}, 'Connections'),
473
+ ...view.sections.flatMap((s) => [
474
+ el('h3', {}, s.title),
475
+ definitionList(s.rows),
476
+ ]),
477
+ ...section('Environments', view.environments, listOf, 'None configured.'),
478
+ el('p', { class: 'note' }, view.note),
479
+ );
480
+ }
481
+
482
+ const PANEL_VIEWS = {
483
+ environments: environmentsElement,
484
+ connections: connectionsElement,
485
+ };
486
+
487
+ const optionElement = (o) => el('option', { value: o.value }, o.text);
488
+
489
+ function settingSelect(field) {
490
+ const select = el(
491
+ 'select',
492
+ { id: field.testId, 'data-testid': field.testId },
493
+ ...field.options.map(optionElement),
494
+ );
495
+ select.value = field.value;
496
+ select.addEventListener('change', () =>
497
+ setPreferences(withPreference(ui.prefs, field.name, select.value)),
498
+ );
499
+ return select;
500
+ }
501
+
502
+ function settingControl(field) {
503
+ return el(
504
+ 'div',
505
+ { class: 'setting' },
506
+ el('label', { for: field.testId }, field.label),
507
+ settingSelect(field),
508
+ );
509
+ }
510
+
511
+ /** The Settings view; polls never rebuild it, so a control keeps focus. */
512
+ function renderSettings() {
513
+ const reset = button(
514
+ { 'data-testid': 'settings-reset' },
515
+ 'Reset to defaults',
516
+ resetSettings,
517
+ );
518
+ $('side-panel-view').replaceChildren(
519
+ el(
520
+ 'section',
521
+ { class: 'panel-view', 'data-testid': 'settings-view' },
522
+ el('h2', {}, 'Settings'),
523
+ ...settingsFields(ui.prefs).map(settingControl),
524
+ reset,
525
+ el('p', { class: 'note' }, 'Saved in this browser only.'),
526
+ ),
527
+ );
528
+ }
529
+
530
+ function resetSettings() {
531
+ setPreferences({ ...DEFAULT_PREFERENCES });
532
+ renderSettings();
533
+ }
534
+
535
+ /** Re-render an open Environments or Connections view. */
536
+ function renderPanel() {
537
+ const build = PANEL_VIEWS[ui.view];
538
+ if (build) $('side-panel-view').replaceChildren(build());
539
+ }
540
+
541
+ function labelButton(id, label) {
542
+ $(id).setAttribute('aria-label', label);
543
+ $(id).setAttribute('title', label);
544
+ }
545
+
546
+ function renderHeader(offline) {
547
+ const view = headerView(offline, ui.connections);
548
+ const badge = $('panel-environments-offline-count');
549
+ badge.textContent = view.badge;
550
+ badge.hidden = view.badge === '';
551
+ labelButton('panel-environments-button', view.environments);
552
+ labelButton('panel-connections-button', view.connections);
553
+ $('panel-connections-button').classList.toggle('ok', view.ok);
554
+ $('panel-connections-button').classList.toggle('bad', !view.ok);
555
+ }
556
+
557
+ function render() {
558
+ if (ui.state) {
559
+ $('repository').textContent = ui.state.repository ?? '';
560
+ $('done-count').textContent = doneHeader(ui.state);
561
+ renderBoard();
562
+ }
563
+ renderHeader(ui.offline);
564
+ renderPanel();
565
+ }
566
+
567
+ function zoneNodes() {
568
+ return document.querySelectorAll('[data-zone]');
569
+ }
570
+
571
+ function showHints(story) {
572
+ for (const zone of zoneNodes()) {
573
+ const verdict = zoneVerdict(story, zone.dataset.zone);
574
+ zone.querySelector('.zone-hint').textContent =
575
+ verdict.kind === 'none' ? '' : verdict.text;
576
+ zone.classList.toggle('zone-ok', ['action', 'copy'].includes(verdict.kind));
577
+ zone.classList.toggle('zone-confirm', verdict.kind === 'confirm');
578
+ zone.classList.toggle('zone-refused', verdict.kind === 'refused');
579
+ }
580
+ }
581
+
582
+ function clearHints() {
583
+ ui.dragging = null;
584
+ $('zone-verdict').textContent = '';
585
+ for (const zone of zoneNodes()) {
586
+ zone.querySelector('.zone-hint').textContent = '';
587
+ zone.classList.remove('zone-ok', 'zone-confirm', 'zone-refused');
588
+ }
589
+ }
590
+
591
+ /** The innermost zone wins: a lane inside Delivering, not the column. */
592
+ function bindZone(node) {
593
+ node.addEventListener('dragover', (event) => {
594
+ if (!ui.dragging) return;
595
+ event.preventDefault();
596
+ event.stopPropagation();
597
+ $('zone-verdict').textContent = zoneVerdict(
598
+ ui.dragging,
599
+ node.dataset.zone,
600
+ ).text;
601
+ });
602
+ node.addEventListener('drop', (event) => {
603
+ event.preventDefault();
604
+ event.stopPropagation();
605
+ const story = ui.dragging;
606
+ clearHints();
607
+ if (story) perform(story, zoneVerdict(story, node.dataset.zone));
608
+ });
609
+ }
610
+
611
+ async function refresh() {
612
+ try {
613
+ const { status, json } = await api(`/api/state?done=${ui.prefs.doneLimit}`);
614
+ if (status !== 200) throw new Error(json.error);
615
+ ui.state = json;
616
+ render();
617
+ } catch {
618
+ toast('Could not read the board state.', true);
619
+ }
620
+ await refreshDetail();
621
+ }
622
+
623
+ async function refreshConnections() {
624
+ try {
625
+ ui.connections = (await api('/api/connections')).json;
626
+ } catch {
627
+ ui.connections = null;
628
+ }
629
+ render();
630
+ }
631
+
632
+ /** The theme, and polling on the chosen cadence. */
633
+ function applyPreferences() {
634
+ document.documentElement.dataset.theme = ui.prefs.theme;
635
+ clearInterval(ui.timer);
636
+ ui.timer = setInterval(refresh, ui.prefs.refreshSeconds * 1000);
637
+ }
638
+
639
+ /** Save and apply `prefs`; a new Done limit re-reads the board now. */
640
+ function setPreferences(prefs) {
641
+ const reread = prefs.doneLimit !== ui.prefs.doneLimit;
642
+ ui.prefs = prefs;
643
+ savePreferences(storage, prefs);
644
+ applyPreferences();
645
+ if (reread) refresh();
646
+ }
647
+
648
+ function wire() {
649
+ for (const column of document.querySelectorAll('.column[data-zone]'))
650
+ bindZone(column);
651
+ for (const [view, id] of Object.entries(PANEL_BUTTONS)) {
652
+ $(id).addEventListener('click', () => togglePanel(view));
653
+ }
654
+ $('confirm-cancel').addEventListener('click', () => {
655
+ ui.pending = null;
656
+ $('confirm-stop').close();
657
+ });
658
+ $('confirm-ok').addEventListener('click', () => {
659
+ const pending = ui.pending;
660
+ ui.pending = null;
661
+ $('confirm-stop').close();
662
+ if (pending) post(pending.story, pending.verdict);
663
+ });
664
+ $('side-panel-close').addEventListener('click', closePanel);
665
+ document.addEventListener('keydown', (event) => {
666
+ if (event.key !== 'Escape' || ui.view === null) return;
667
+ // An open dialog takes Esc for itself.
668
+ if (!document.querySelector('dialog[open]')) closePanel();
669
+ });
670
+ }
671
+
672
+ ui.prefs = loadPreferences(storage);
673
+ applyPreferences();
674
+ wire();
675
+ refresh();
676
+ refreshConnections();