experiments/01-reading-path/study.js

1import { compose, relatedRuns, describe, facts, flowRole } from './composition.js';
2const $ = selector => document.querySelector(selector);
3const controls = $('#controls');
4const tags = { Aside:'section', Group:'div', Headline:'h4', Title:'p', Label:'p', Summary:'p', Account:'p', Text:'span', Status:'p', Fact:'p', Destinations:'div', Action:'a', Links:'nav', Link:'a', Wrapper:'div', Related:'section' };
5
6function render(node) {
7  const element = document.createElement(tags[node.role]);
8  element.dataset.role = node.role;
9  element.dataset.flowRole = flowRole(node);
10  if (node.state) element.dataset.state = node.state;
11  if (node.role === 'Status') {
12    const mark = document.createElement('span');
13    mark.className = 'state-mark'; mark.setAttribute('aria-hidden', 'true');
14    mark.textContent = node.state === 'complete' ? '✓ ' : '◷ ';
15    element.append(mark);
16  }
17  if (node.attention) element.dataset.attention = node.attention;
18  if (node.destination) { element.href = `#${node.destination}`; element.dataset.destination = node.destination; }
19  if (node.role === 'Aside') {
20    const label = document.createElement('h3'); label.className = 'aside-label'; label.textContent = node.label; element.append(label);
21    element.setAttribute('aria-label', node.label);
22  } else if (node.role === 'Fact') {
23    const label = document.createElement('span'); label.textContent = `${node.label} `; element.append(label);
24  }
25  if (node.text) element.append(document.createTextNode(node.text));
26  if (node.role === 'Action') { const arrow = document.createElement('span'); arrow.textContent = ' →'; arrow.setAttribute('aria-hidden','true'); element.append(arrow); }
27  for (const [index, child] of (node.children || []).entries()) {
28    if (node.role === 'Account' && index) element.append(' ');
29    element.append(render(child));
30  }
31  return element;
32}
33
34const run = `<header class="run-header"><p class="run-breadcrumb">Automation / Anomaly alerts</p><h2>Investigate anomaly alerts <span>run</span></h2><p class="run-meta"><span class="complete">Completed</span><span>Nov 18, 10:30 AM</span><span>4m 1s</span></p></header><div class="run-layout"><section class="execution" aria-labelledby="execution-title"><h3 id="execution-title">Execution</h3><p class="muted execution-note">Recorded during this run</p><div class="step"><p><span class="complete-mark" aria-hidden="true">✓</span><strong>Investigate the anomaly</strong><span class="duration">4m 0s</span></p><p class="code">vercelAgent</p></div><details class="run-detail"><summary>Script result <span>empty</span></summary><p>The call completed without a returned script value.</p></details><details class="run-detail"><summary>Run record and journal agree <span>completed</span></summary><p>Both recorded the run as complete.</p></details><div class="run-settings"><h3>What this run used</h3><details class="run-detail"><summary>Trigger and input <span>Event</span></summary><p>An anomaly alert event triggered this run.</p></details><details class="run-detail"><summary>Configuration <span>At start</span></summary><p>This specimen shows the recorded run beside the current investigation.</p></details></div></section><div class="context-slot"></div></div>`;
35$('#product').innerHTML = run;
36
37function state() {
38  const data = new FormData(controls);
39  return Object.fromEntries(['presentation', 'placement', 'density', 'situation', 'width'].map(key => [key, data.get(key)]).concat([['wrapper',data.has('wrapper')], ['neighbor',data.has('neighbor')]]));
40}
41function update() {
42  const current = state();
43  const tree = compose(current);
44  $('#frame').style.width = `${current.width}px`;
45  $('#product').dataset.placement = current.placement;
46  $('#product').dataset.density = current.density;
47  const slot = $('.context-slot');
48  slot.replaceChildren(render(tree), ...(current.neighbor ? [render(relatedRuns())] : []));
49  $('#source').textContent = `${describe(tree)}${current.neighbor ? `\n\n${describe(relatedRuns())}` : ''}`;
50  $('#author-description').textContent = current.presentation === 'editorial'
51    ? 'Compose a finding, an account, its dated fact, and destinations. The words name the objects; the layout carries their reading order.'
52    : 'Compose an investigation group, a remediation group, and destinations. Placement identifies each status and the update time; the labels can be shorter.';
53  if (current.situation === 'missing') $('#author-description').textContent = 'The missing state has no finding, Plan, or update time. Its label becomes the landmark; useful destinations remain.';
54  if (current.wrapper) $('#author-description').textContent += ' The real wrapper forwards the destination role to the parent policy.';
55  const params = new URLSearchParams();
56  for (const [key,value] of Object.entries(current)) params.set(key,String(value));
57  history.replaceState(null,'',`?${params}`);
58  measure();
59}
60function measure() {
61  const actual = Math.round($('#frame').getBoundingClientRect().width);
62  const requested = Number($('#width').value);
63  $('#width-value').textContent = `${actual} px${actual < requested ? ' · fits this window' : ''}`;
64  const columns = getComputedStyle($('.run-layout')).gridTemplateColumns.split(' ').length;
65  $('#arrangement-note').textContent = columns > 1 ? 'Investigation beside execution' : 'Investigation follows execution';
66}
67const params = new URLSearchParams(location.search);
68for (const key of ['presentation','placement','density','situation']) {
69  const field = controls.elements.namedItem(key); const value = params.get(key);
70  if (value && [...controls.querySelectorAll(`[name="${key}"] option, input[name="${key}"]`)].some(option=>option.value===value)) field.value = value;
71}
72if (params.has('width') && Number.isFinite(Number(params.get('width')))) $('#width').value = Math.max(360,Math.min(1120,Number(params.get('width'))));
73for (const key of ['wrapper','neighbor']) controls.elements.namedItem(key).checked = params.get(key)==='true';
74controls.addEventListener('input', update);
75controls.addEventListener('submit', event => event.preventDefault());
76$('#reset-study').addEventListener('click',()=>{ controls.reset(); update(); });
77new ResizeObserver(measure).observe($('#frame'));
78const context = {
79  session: ['Investigation session', `Finding: ${facts.finding}. Investigation completed. Investigation updated ${facts.updated}.`],
80  runs: ['Other runs', 'Earlier completed runs for this alert group: Nov 18, 9:12 AM and Nov 18, 8:46 AM.'],
81  alert: ['Alert in Vercel', 'This is the alert group that the run investigated. The current investigation is independent of the recorded run.'],
82  'earlier-run': ['Earlier run', 'A completed earlier run for this alert group. Its result does not establish the current remediation state.'],
83};
84$('#product').addEventListener('click',event=>{
85  const link=event.target.closest('[data-destination]'); if(!link) return;
86  event.preventDefault(); const [title,body] = context[link.dataset.destination];
87  $('#destination-title').textContent=title; $('#destination-body').textContent=body; $('#destination').showModal();
88});
89$('#return').addEventListener('click',()=>$('#destination').close());
90update();