@popcorn245/cornfield 0.54.0 → 0.55.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.
- package/core/contrast.js +17 -0
- package/core/schema.js +18 -0
- package/core/strings.js +1 -0
- package/explorer/engine/layers.js +5 -2
- package/explorer/engine/map-style.js +5 -1
- package/explorer/engine/render.js +2 -1
- package/explorer/forge-panel.js +1 -0
- package/explorer/index.html +6 -3
- package/explorer/layer-model.js +1 -0
- package/explorer/page.js +3 -1
- package/explorer/pieces/layers.js +1 -1
- package/explorer/pieces/node-panel.js +18 -0
- package/explorer/pieces/search.js +9 -3
- package/explorer/pieces/work-markup.js +2 -3
- package/explorer/pieces/work-paint.js +3 -8
- package/explorer/pieces/work-threads.js +2 -2
- package/explorer/pieces/work.js +12 -0
- package/explorer/scene.js +17 -8
- package/explorer/search-match.js +3 -2
- package/explorer/tokens.css +7 -0
- package/explorer/view.js +8 -1
- package/explorer/work-model.js +681 -0
- package/explorer/work-view.css +51 -0
- package/explorer/work-view.js +542 -0
- package/package.json +1 -1
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/work-model
|
|
3
|
+
*
|
|
4
|
+
* The pure half of the Work view (spec 0012 D1 to D5, D8): the map of work, from the server's answer to `GET /api/forge/work-graph`
|
|
5
|
+
* (server/work-graph.js: `{ ok, nodes, edges, problems }`) to nodes the scene draws, where they sit, what a layer hides, what the node panel
|
|
6
|
+
* says of one, what search finds and what an address asks for. No DOM, no state, no fetch: work-view.js (the page's half) holds those.
|
|
7
|
+
*
|
|
8
|
+
* - `workScene(graph, mapNodes)` the nodes and edges of the Work view: the work, the code it touches (copies of the map's nodes, and the planned
|
|
9
|
+
* ones a proposal adds), and the lines between them with their words
|
|
10
|
+
* - `layoutWork(nodes, edges, { phone })` where each node sits, and the lanes behind them; writes `x`, `y` and `r` on the nodes it is given
|
|
11
|
+
* - `applyFilters(nodes, edges, { layers, waiting })` sets `hidden` (a layer is off) and `dim` ("Waiting on you" is on) on the nodes
|
|
12
|
+
* - `layerRowsOf(nodes)` the Layers menu's rows in the Work view, each with its count in words
|
|
13
|
+
* - `panelOf(node, nodes, edges)` what the node panel says of a work node (kind, pills, the connected list, the words saying what comes next)
|
|
14
|
+
* - `workAddress(search)` what `?view=work`, `?pr=`, `?issue=`, `?run=`, `?thread=` and `?spec=` ask for
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { NODE_THEMES } from '../core/schema.js';
|
|
18
|
+
import { WORK_KINDS, WORK_THEMES, workType } from '../core/schema.js';
|
|
19
|
+
import { relativeTime } from './review-model.js';
|
|
20
|
+
import { SPEC_STATE_WORDS } from './waiting-model.js';
|
|
21
|
+
import { cornState } from './corn-state.js';
|
|
22
|
+
import { escapeHtml as esc } from '../core/text.js';
|
|
23
|
+
import { safeUrl } from './pieces/work-common.js';
|
|
24
|
+
|
|
25
|
+
/** The words of a kind of work, singular and as a layer */
|
|
26
|
+
export const KIND_WORDS = Object.freeze({ thread: 'Thread', issue: 'Issue', proposal: 'Proposal', run: 'Agent run', pull: 'Pull request', deploy: 'Deploy' });
|
|
27
|
+
export const LAYER_WORDS = Object.freeze({ thread: 'Threads', issue: 'Issues', proposal: 'Proposals', run: 'Agent runs', pull: 'Pull requests', deploy: 'Deploys' });
|
|
28
|
+
/** The key of the one layer that holds the code the work touches, and a kind's key (the node type it has in the Work view) */
|
|
29
|
+
export const CODE_LAYER = 'work-code';
|
|
30
|
+
/** The layer of the proposals that are only files in the checkout (`state: 'local'`): nobody has asked anyone to decide them, so it starts off (spec 0012 D4) */
|
|
31
|
+
export const DRAFT_LAYER = 'work-drafts';
|
|
32
|
+
export const layerKey = workType;
|
|
33
|
+
/** The rows of the Layers menu, in order (the mock's: threads, issues, proposals, pull requests, agent runs, deploys; then the drafts and the code) */
|
|
34
|
+
export const KIND_ORDER = Object.freeze(['thread', 'issue', 'proposal', 'pull', 'run', 'deploy']);
|
|
35
|
+
export const LAYER_KEYS = Object.freeze([...KIND_ORDER.map(workType), DRAFT_LAYER, CODE_LAYER]);
|
|
36
|
+
export const CODE_LAYER_WORDS = 'Code they touch';
|
|
37
|
+
export const DRAFT_LAYER_WORDS = 'Drafts';
|
|
38
|
+
/** A proposal that is only a file in the checkout: no pull request holds it, so nobody has been asked to decide it */
|
|
39
|
+
export const isDraft = (item) => !!item && item.kind === 'proposal' && item.state === 'local';
|
|
40
|
+
|
|
41
|
+
/** The lanes, in order: the stage each holds (server/work-graph.js STAGES) and its title */
|
|
42
|
+
export const LANES = Object.freeze([
|
|
43
|
+
{ key: 'talk', title: 'Talk' },
|
|
44
|
+
{ key: 'decide', title: 'Decide' },
|
|
45
|
+
{ key: 'build', title: 'Build' },
|
|
46
|
+
{ key: 'ship', title: 'Ship' },
|
|
47
|
+
]);
|
|
48
|
+
/** The local drafts, when their layer is on, have a lane of their own beside Decide (they are the Decide stage's, and not many people have been asked about them) */
|
|
49
|
+
export const DRAFT_LANE_TITLE = 'Drafts';
|
|
50
|
+
export const CODE_LANE_TITLE = 'Code it touches';
|
|
51
|
+
|
|
52
|
+
/** The words beside a line: the rel as the line reads from its start, and as the node at its end reads it (a connection listed in the node panel) */
|
|
53
|
+
export const REL_WORDS = Object.freeze({
|
|
54
|
+
became: ['became', 'came from'],
|
|
55
|
+
discusses: ['discusses', 'discussed in'],
|
|
56
|
+
'agent-on': ['agent on it', 'works on'],
|
|
57
|
+
opened: ['opened', 'opened by'],
|
|
58
|
+
closes: ['closes', 'closed by'],
|
|
59
|
+
implements: ['implements', 'built by'],
|
|
60
|
+
shipped: ['shipped in', 'shipped'],
|
|
61
|
+
changes: ['changes', 'changed by'],
|
|
62
|
+
adds: ['adds', 'added by'],
|
|
63
|
+
mentions: ['mentions', 'mentioned in'],
|
|
64
|
+
restarts: ['restarts', 'restarted by'],
|
|
65
|
+
});
|
|
66
|
+
/** The word on the map for a line (once, from its start): the same as `REL_WORDS[rel][0]` */
|
|
67
|
+
export const relWord = (rel) => (Object.hasOwn(REL_WORDS, rel) ? REL_WORDS[rel][0] : String(rel));
|
|
68
|
+
|
|
69
|
+
/** The most characters of a title in a node's name (the whole title is the node panel's), and of the whole text drawn under a node on a desktop (its tag and the start of its title: a lane is not much wider
|
|
70
|
+
* than that on screen); on a phone a piece of work is drawn as its tag alone */
|
|
71
|
+
export const LABEL_CHARS = 34;
|
|
72
|
+
export const DRAWN_CHARS = 28;
|
|
73
|
+
const clip = (s, n) => (s.length > n ? `${s.slice(0, n - 1).trimEnd()}…` : s);
|
|
74
|
+
/** The same, cut at a word when one is near the end of what fits */
|
|
75
|
+
const clipWords = (s, n) => { if (s.length <= n) return s; const cut = s.slice(0, n - 1); const sp = cut.lastIndexOf(' '); return `${(sp > n * 0.6 ? cut.slice(0, sp) : cut).trimEnd()}…`; };
|
|
76
|
+
const text = (v) => (typeof v === 'string' ? v : '');
|
|
77
|
+
const isWork = (kind) => typeof kind === 'string' && Object.hasOwn(WORK_THEMES, kind);
|
|
78
|
+
|
|
79
|
+
/** The number of a spec: the leading digits of its slug ("0100-flow" is "0100"), else the slug */
|
|
80
|
+
const specNumber = (slug) => { const m = /^(\d{2,6})(?:-|$)/.exec(slug); return m ? m[1] : slug; };
|
|
81
|
+
|
|
82
|
+
/** What a piece of work is called on the map, short: "PR #27", "Issue #14", "Thread #31", "Spec 0100", "Run abc123", "Deploy" */
|
|
83
|
+
export function tagOf(item) {
|
|
84
|
+
switch (item.kind) {
|
|
85
|
+
case 'pull': return `PR #${item.number}`;
|
|
86
|
+
case 'issue': return `Issue #${item.number}`;
|
|
87
|
+
case 'thread': return `Thread #${item.number}`;
|
|
88
|
+
case 'proposal': return `Spec ${specNumber(text(item.slug))}`;
|
|
89
|
+
case 'run': return `Run ${text(item.runId).slice(-6)}`;
|
|
90
|
+
default: return 'Deploy';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What a search reads of a piece of work besides its label: `#21`, `PR 21`, `pull request 21`, the slug, the run id, the title */
|
|
95
|
+
export function termsOf(item) {
|
|
96
|
+
const terms = [tagOf(item), text(item.title), KIND_WORDS[item.kind]];
|
|
97
|
+
const n = item.number;
|
|
98
|
+
if (Number.isInteger(n)) terms.push(`#${n}`, String(n), `pr ${n}`, `pull ${n}`, `pull request ${n}`, `issue ${n}`, `thread ${n}`);
|
|
99
|
+
if (item.slug) terms.push(item.slug, specNumber(item.slug), `spec ${specNumber(item.slug)}`);
|
|
100
|
+
if (item.runId) terms.push(item.runId);
|
|
101
|
+
if (item.name) terms.push(item.name);
|
|
102
|
+
return terms.filter((t) => typeof t === 'string' && t !== '');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A node the graph names that the map does not hold: a planned one, by its type (an unknown type reads as a page) */
|
|
106
|
+
const plannedType = (type) => (typeof type === 'string' && Object.hasOwn(NODE_THEMES, type) ? type : 'page');
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The scene of the Work view.
|
|
110
|
+
* @param {{ nodes?: object[], edges?: { from: string, to: string, rel: string }[] } | null} graph the server's answer
|
|
111
|
+
* @param {{ id: string, label?: string, type?: string }[]} mapNodes the map's nodes (the code a piece of work touches is found among them by id)
|
|
112
|
+
* @returns {{ nodes: object[], edges: object[], counts: { work: number, code: number, waiting: number } }}
|
|
113
|
+
*/
|
|
114
|
+
export function workScene(graph, mapNodes) {
|
|
115
|
+
const items = Array.isArray(graph && graph.nodes) ? graph.nodes : [];
|
|
116
|
+
const links = Array.isArray(graph && graph.edges) ? graph.edges : [];
|
|
117
|
+
const onMap = new Map((mapNodes || []).map((n) => [n.id, n]));
|
|
118
|
+
const work = new Map();
|
|
119
|
+
const planned = new Map();
|
|
120
|
+
for (const item of items) {
|
|
121
|
+
if (!item || typeof item.id !== 'string') continue;
|
|
122
|
+
if (item.kind === 'code') { if (item.planned === true) planned.set(item.id, item); continue; }
|
|
123
|
+
if (isWork(item.kind)) work.set(item.id, item);
|
|
124
|
+
}
|
|
125
|
+
// The edges that join two things on this map: work to work, or work to code the map holds or a proposal plans
|
|
126
|
+
const edges = [];
|
|
127
|
+
const seen = new Set();
|
|
128
|
+
for (const e of links) {
|
|
129
|
+
if (!e || typeof e.from !== 'string' || typeof e.to !== 'string' || !Object.hasOwn(REL_WORDS, e.rel)) continue;
|
|
130
|
+
const from = work.has(e.from) ? 'work' : null;
|
|
131
|
+
const to = work.has(e.to) ? 'work' : onMap.has(e.to) || planned.has(e.to) ? 'code' : null;
|
|
132
|
+
if (!from || !to) continue;
|
|
133
|
+
const key = `${e.from}->${e.to}|${e.rel}`;
|
|
134
|
+
if (seen.has(key)) continue;
|
|
135
|
+
seen.add(key);
|
|
136
|
+
edges.push({ from: e.from, to: e.to, kind: e.rel, tone: to === 'work' ? 'work' : 'code', word: relWord(e.rel) });
|
|
137
|
+
}
|
|
138
|
+
// The code that work touches, and by how many pieces of it
|
|
139
|
+
const touched = new Map();
|
|
140
|
+
for (const e of edges) if (e.tone === 'code') touched.set(e.to, (touched.get(e.to) || new Set()).add(e.from));
|
|
141
|
+
|
|
142
|
+
const degree = new Map();
|
|
143
|
+
for (const e of edges) { degree.set(e.from, (degree.get(e.from) || 0) + 1); degree.set(e.to, (degree.get(e.to) || 0) + 1); }
|
|
144
|
+
const nodes = [];
|
|
145
|
+
for (const item of work.values()) {
|
|
146
|
+
const theme = WORK_THEMES[item.kind];
|
|
147
|
+
const title = text(item.title) || tagOf(item);
|
|
148
|
+
const draft = isDraft(item);
|
|
149
|
+
nodes.push({
|
|
150
|
+
id: item.id, type: workType(item.kind), work: item.kind, shape: theme.shape, label: item.kind === 'deploy' ? `Deploy · ${clip(title, LABEL_CHARS)}` : `${tagOf(item)} · ${clip(title, LABEL_CHARS)}`,
|
|
151
|
+
title, item, r: theme.r, color: theme.color, letters: theme.letters, terms: termsOf(item), groupName: draft ? DRAFT_LAYER_WORDS : LAYER_WORDS[item.kind],
|
|
152
|
+
draft, waiting: item.waiting === true, updated: text(item.updated), stage: text(item.stage), degree: degree.get(item.id) || 0, x: 0, y: 0,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
let code = 0;
|
|
156
|
+
for (const [id, by] of touched) {
|
|
157
|
+
const base = onMap.get(id);
|
|
158
|
+
const plan = planned.get(id);
|
|
159
|
+
if (!base && !plan) continue;
|
|
160
|
+
code++;
|
|
161
|
+
nodes.push(base
|
|
162
|
+
? { ...base, x: 0, y: 0, r: CODE_R, touches: by.size, degree: by.size, hidden: false, dim: false }
|
|
163
|
+
: { id, type: plannedType(plan.type), label: text(plan.label) || id, planned: true, r: CODE_R, touches: by.size, degree: by.size, x: 0, y: 0, groupName: CODE_LAYER_WORDS });
|
|
164
|
+
}
|
|
165
|
+
const waiting = nodes.filter((n) => n.waiting).length;
|
|
166
|
+
return { nodes, edges, counts: { work: work.size, code, waiting } };
|
|
167
|
+
}
|
|
168
|
+
/** The colours of the lines of the Work view: a work-to-work link, a link to code, and what a proposal adds */
|
|
169
|
+
const WORK_EDGE_COLORS = { work: '#818cf8', code: '#cbd5e1', adds: '#2dd4bf' };
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* One line of the Work view for the scene (spec 0012 D2, D3): a work-to-work link solid, `mentions` dotted, `adds` dashed; the word of its rel beside a lit one. The scene calls it
|
|
173
|
+
* for every line (`state.workEdge`, put there by `showWork`), so the map's own code carries no table of the Work view's lines.
|
|
174
|
+
* @param {object} edge the line, `{ from, to, kind, tone, word }`
|
|
175
|
+
* @param {object} source the node it starts at
|
|
176
|
+
* @param {object} target the node it ends at
|
|
177
|
+
* @param {boolean} connected whether it joins the selection's neighbourhood
|
|
178
|
+
* @param {boolean} focused whether a node is selected
|
|
179
|
+
* @param {string} key the line's key
|
|
180
|
+
* @param {{ PROPOSED_DASH: number[], USAGE_DASH: number[] }} dashes the map's two dash patterns
|
|
181
|
+
*/
|
|
182
|
+
export function workEdge(edge, source, target, connected, focused, key, dashes) {
|
|
183
|
+
const lit = focused && connected;
|
|
184
|
+
const color = WORK_EDGE_COLORS[edge.kind === 'adds' ? 'adds' : edge.tone === 'work' ? 'work' : 'code'];
|
|
185
|
+
const faded = source.dim === true && target.dim === true; // "Waiting on you" is on and neither end waits
|
|
186
|
+
return {
|
|
187
|
+
key, from: edge.from, to: edge.to, kind: edge.kind,
|
|
188
|
+
x1: source.x, y1: source.y, x2: target.x, y2: target.y, r1: source.r, r2: target.r,
|
|
189
|
+
color, width: lit ? 2.4 : 1.4, alpha: faded ? 0.12 : focused ? (connected ? 1 : 0.08) : 0.35,
|
|
190
|
+
dash: edge.kind === 'adds' ? dashes.PROPOSED_DASH : edge.kind === 'mentions' ? dashes.USAGE_DASH : null,
|
|
191
|
+
ticks: 0, curve: null, arrow: false, end: null, glow: 0, glowColor: null,
|
|
192
|
+
label: lit ? { text: typeof edge.word === 'string' && edge.word ? edge.word : edge.kind, x: (source.x + target.x) / 2, y: (source.y + target.y) / 2, color, italic: false } : null,
|
|
193
|
+
packet: null, mark: 'none', live: false,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The radius of a node of code in the Work view: small beside the work it is touched by */
|
|
198
|
+
export const CODE_R = 9;
|
|
199
|
+
|
|
200
|
+
// ---------------------------------------------------------------------------
|
|
201
|
+
// Where it sits
|
|
202
|
+
// ---------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
/** Waiting first, then the newest, then by id: the order within a lane (spec 0012 D3) */
|
|
205
|
+
export const byLane = (a, b) => (a.waiting === b.waiting ? 0 : a.waiting ? -1 : 1) || (a.updated < b.updated ? 1 : a.updated > b.updated ? -1 : 0) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
206
|
+
/** Most touched first, then by name: the order of the code */
|
|
207
|
+
export const byTouch = (a, b) => (b.touches || 0) - (a.touches || 0) || String(a.label).localeCompare(String(b.label)) || (a.id < b.id ? -1 : 1);
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The sizes the layout works in. World units, laid out for the zoom the first fit will have, so a name under a node (11 px at least, whatever the zoom: labels.js) has
|
|
211
|
+
* the room on screen it needs: a cell is wide enough for a name, a row tall enough for a disc and a name under it, and lanes next to each other are staggered by half a row
|
|
212
|
+
* so a name that runs past its lane lands between the next lane's. `margin` is what the first fit leaves on screen around the lanes, in px (the names at the edge, and the toggles at the top).
|
|
213
|
+
*/
|
|
214
|
+
const DESK = { cell: 200, row: 70, codeCell: 190, codeRow: 44, gap: 14, pad: 6, margin: { x: 100, top: 56, bottom: 24 }, caps: [100, 64, 48, 40, 32, 28, 24, 20, 18, 16, 14, 12, 10, 9, 8, 7, 6, 5] };
|
|
215
|
+
const PHONE = { cell: 104, row: 84, codeCell: 150, codeRow: 40, gap: 12, pad: 4, margin: { x: 36, top: 112, bottom: 16 } };
|
|
216
|
+
/** A lane's title (screen px) the first node sits under, and a name under a node (its gap and its line, px) */
|
|
217
|
+
const TITLE_PX = 40;
|
|
218
|
+
const NAME_PX = 22;
|
|
219
|
+
const LANE_FILL = 'rgba(66, 153, 225, 0.05)';
|
|
220
|
+
const LANE_STROKE = 'rgba(66, 153, 225, 0.18)';
|
|
221
|
+
|
|
222
|
+
/** A lane behind the nodes (`SceneLane`: scene.js, with no text of its own: its title is a line of the page, in screen space, work-view.js), and what the title says */
|
|
223
|
+
const lane = (key, title, count, x, y, w, h) => ({ key, name: title, count, x, y, w, h, radius: 14, fill: LANE_FILL, stroke: LANE_STROKE, strokeWidth: 1, title: null, note: null, arrow: null });
|
|
224
|
+
|
|
225
|
+
/** The text drawn under a node: a piece of work's tag and the start of its title (a phone: the tag), the name of a node of code (a phone: the start of it) */
|
|
226
|
+
function nameNodes(nodes, phone) {
|
|
227
|
+
for (const n of nodes) {
|
|
228
|
+
if (n.work) { const tag = tagOf(n.item); n.labelText = phone ? tag : `${tag} · ${clipWords(n.title, Math.max(8, DRAWN_CHARS - tag.length - 3))}`; }
|
|
229
|
+
else n.labelText = clip(String(n.label), phone ? 12 : 32);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The zoom that shows `w` by `h` world units with the margins left around them, on a screen `v.width` by `v.height`; at most 1 */
|
|
234
|
+
const zoomFor = (w, h, v, m) => Math.min(1, Math.max(1, v.width - 2 * m.x) / Math.max(1, w), Math.max(1, v.height - m.top - m.bottom) / Math.max(1, h));
|
|
235
|
+
|
|
236
|
+
/** The box the camera fits (engine/render.js `fitTarget`): the lanes, and around them the margins at the zoom that shows them; the engine's own padding `p` is taken out of it */
|
|
237
|
+
function fitBoxOf(w, h, z, m, p, v) {
|
|
238
|
+
const minY = -(m.top - p) / z;
|
|
239
|
+
// as tall as the screen shows at that zoom, so the lanes stay under the toggles when they are shorter than the screen (the fit centres the box)
|
|
240
|
+
return { minX: -(m.x - p) / z, maxX: w + (m.x - p) / z, minY, maxY: Math.max(h + (m.bottom - p) / z, minY + (v.height - 2 * p) / z), zoom: z };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Where the nodes sit: four lanes of work, then the code it touches (spec 0012 D3), made for the screen it is on. A desktop's lanes run left to right, and a lane (the
|
|
245
|
+
* drafts' and the code's most of all) is cut into as many columns as show the most of it at once; a phone's run top to bottom (Talk, Decide, Build and Ship in one), with the code in
|
|
246
|
+
* columns beside them. Only what is not hidden is laid out. Writes `x`, `y` and `labelText` on the nodes and returns the lanes behind them and the box the first fit shows.
|
|
247
|
+
* @param {object[]} nodes the nodes of `workScene`
|
|
248
|
+
* @param {{ phone?: boolean, width?: number, height?: number, padding?: number }} [opts] the map's size in px, and the engine's padding around a fit
|
|
249
|
+
* @returns {{ lanes: object[], order: string[], bounds: { x: number, y: number, w: number, h: number }, fit: { minX: number, maxX: number, minY: number, maxY: number, zoom: number } }}
|
|
250
|
+
*/
|
|
251
|
+
export function layoutWork(nodes, { phone = false, width = 1200, height = 800, padding = phone ? 44 : 96 } = {}) {
|
|
252
|
+
nameNodes(nodes, phone);
|
|
253
|
+
const shown = nodes.filter((n) => !n.hidden);
|
|
254
|
+
const mine = (key) => shown.filter((n) => n.work && !n.draft && n.stage === key).sort(byLane);
|
|
255
|
+
const stages = LANES.map((l) => ({ ...l, nodes: mine(l.key) }));
|
|
256
|
+
// A kind of work the server gave no stage still has a lane: the kind's own
|
|
257
|
+
const known = new Set(stages.flatMap((s) => s.nodes.map((n) => n.id)));
|
|
258
|
+
for (const n of shown.filter((x) => x.work && !x.draft && !known.has(x.id))) stages.find((s) => s.key === { thread: 'talk', issue: 'talk', proposal: 'decide', run: 'build', pull: 'build', deploy: 'ship' }[n.work]).nodes.push(n);
|
|
259
|
+
stages.forEach((s) => s.nodes.sort(byLane));
|
|
260
|
+
const drafts = shown.filter((n) => n.draft).sort(byLane);
|
|
261
|
+
const code = shown.filter((n) => !n.work).sort(byTouch);
|
|
262
|
+
const v = { width: Math.max(160, width), height: Math.max(160, height) };
|
|
263
|
+
return phone ? layoutPhone(stages, drafts, code, v, padding) : layoutDesk(stages, drafts, code, v, padding);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** How many columns a lane of `n` nodes has when a column holds at most `cap`, and how tall they are */
|
|
267
|
+
const cut = (n, cap) => { const cols = Math.max(1, Math.ceil(n / cap)); return { cols, rows: Math.max(1, Math.ceil(n / cols)) }; };
|
|
268
|
+
|
|
269
|
+
function layoutDesk(stages, drafts, code, v, p) {
|
|
270
|
+
const c = DESK;
|
|
271
|
+
// Left to right: Talk, Decide, the drafts (when they are shown), Build, Ship, then the code
|
|
272
|
+
const lanes = [stages[0], stages[1], ...(drafts.length ? [{ key: 'drafts', title: DRAFT_LANE_TITLE, nodes: drafts }] : []), stages[2], stages[3], ...(code.length ? [{ key: 'code', title: CODE_LANE_TITLE, nodes: code, code: true }] : [])];
|
|
273
|
+
const plan = (z, cap) => {
|
|
274
|
+
const room = TITLE_PX / z;
|
|
275
|
+
const below = NAME_PX / z;
|
|
276
|
+
let left = 0;
|
|
277
|
+
let tall = 0;
|
|
278
|
+
const parts = lanes.map((l, i) => {
|
|
279
|
+
const cell = l.code ? c.codeCell : c.cell;
|
|
280
|
+
const row = l.code ? c.codeRow : c.row;
|
|
281
|
+
const r = l.code ? CODE_R : 20;
|
|
282
|
+
const { cols, rows } = cut(l.nodes.length, cap);
|
|
283
|
+
const w = cols * cell + 2 * c.pad;
|
|
284
|
+
const stagger = cols > 1 || i % 2 ? 0.5 : 0;
|
|
285
|
+
const h = room + 2 * r + (rows - 1 + stagger) * row + below + 8;
|
|
286
|
+
tall = Math.max(tall, h);
|
|
287
|
+
const part = { l, i, cell, row, r, cols, rows, x: left, w, h };
|
|
288
|
+
left += w + c.gap;
|
|
289
|
+
return part;
|
|
290
|
+
});
|
|
291
|
+
return { parts, w: left - c.gap, h: tall, room };
|
|
292
|
+
};
|
|
293
|
+
let z = 0.6;
|
|
294
|
+
let best = null;
|
|
295
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
296
|
+
best = null;
|
|
297
|
+
for (const cap of c.caps) {
|
|
298
|
+
const g = plan(z, cap);
|
|
299
|
+
const fit = zoomFor(g.w, g.h, v, c.margin);
|
|
300
|
+
if (!best || fit > best.fit * 1.01) best = { g, fit, cap };
|
|
301
|
+
}
|
|
302
|
+
z = best.fit;
|
|
303
|
+
}
|
|
304
|
+
const { g } = best;
|
|
305
|
+
const out = [];
|
|
306
|
+
for (const part of g.parts) {
|
|
307
|
+
const { l, i, cell, row, r, cols, rows } = part;
|
|
308
|
+
l.nodes.forEach((n, j) => {
|
|
309
|
+
const col = Math.floor(j / rows);
|
|
310
|
+
n.x = part.x + c.pad + (col + 0.5) * cell;
|
|
311
|
+
n.y = g.room + r + (j % rows + ((i + col) % 2) * 0.5) * row;
|
|
312
|
+
});
|
|
313
|
+
out.push(lane(l.key, l.title, l.nodes.length, part.x, 0, part.w, g.h));
|
|
314
|
+
}
|
|
315
|
+
return { lanes: out, order: g.parts.flatMap((part) => part.l.nodes.map((n) => n.id)), bounds: { x: 0, y: 0, w: g.w, h: g.h }, fit: fitBoxOf(g.w, g.h, z, c.margin, p, v) };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function layoutPhone(stages, drafts, code, v, p) {
|
|
319
|
+
const c = PHONE;
|
|
320
|
+
const bands = [stages[0], stages[1], ...(drafts.length ? [{ key: 'drafts', title: DRAFT_LANE_TITLE, nodes: drafts }] : []), { key: 'build-ship', title: 'Build · Ship', nodes: [...stages[2].nodes, ...stages[3].nodes] }];
|
|
321
|
+
const plan = (z, perRow, codeCols) => {
|
|
322
|
+
const room = TITLE_PX / z;
|
|
323
|
+
const below = NAME_PX / z;
|
|
324
|
+
const wBand = perRow * c.cell + 2 * c.pad;
|
|
325
|
+
let y = 0;
|
|
326
|
+
const parts = bands.map((b) => {
|
|
327
|
+
const rows = Math.max(1, Math.ceil(b.nodes.length / perRow));
|
|
328
|
+
const h = room + 40 + (rows - 1 + (perRow > 1 ? 0.5 : 0)) * c.row + below + 8;
|
|
329
|
+
const part = { b, rows, y, h };
|
|
330
|
+
y += h + c.gap;
|
|
331
|
+
return part;
|
|
332
|
+
});
|
|
333
|
+
const hBands = y - c.gap;
|
|
334
|
+
const codeRows = Math.max(1, Math.ceil(code.length / codeCols));
|
|
335
|
+
const wCode = code.length ? codeCols * c.codeCell + 2 * c.pad : 0;
|
|
336
|
+
const hCode = code.length ? room + 2 * CODE_R + (codeRows - 1 + (codeCols > 1 ? 0.5 : 0)) * c.codeRow + below + 8 : 0;
|
|
337
|
+
return { parts, wBand, wCode, hBands, hCode, codeRows, room, w: wBand + (code.length ? c.gap + wCode : 0), h: Math.max(hBands, hCode) };
|
|
338
|
+
};
|
|
339
|
+
let z = 0.5;
|
|
340
|
+
let best = null;
|
|
341
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
342
|
+
best = null;
|
|
343
|
+
for (const perRow of [2, 3, 4]) {
|
|
344
|
+
for (const codeCols of code.length ? [1, 2, 3] : [1]) {
|
|
345
|
+
const g = plan(z, perRow, codeCols);
|
|
346
|
+
const fit = zoomFor(g.w, g.h, v, c.margin);
|
|
347
|
+
if (!best || fit > best.fit * 1.01) best = { g, fit, perRow, codeCols };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
z = best.fit;
|
|
351
|
+
}
|
|
352
|
+
const { g, perRow, codeCols } = best;
|
|
353
|
+
const out = [];
|
|
354
|
+
for (const part of g.parts) {
|
|
355
|
+
part.b.nodes.forEach((n, j) => { n.x = c.pad + (j % perRow + 0.5) * c.cell; n.y = part.y + g.room + 20 + (Math.floor(j / perRow) + ((j % perRow) % 2) * 0.5) * c.row; });
|
|
356
|
+
out.push(lane(part.b.key, part.b.title, part.b.nodes.length, 0, part.y, g.wBand, part.h));
|
|
357
|
+
}
|
|
358
|
+
if (code.length) {
|
|
359
|
+
const x0 = g.wBand + c.gap;
|
|
360
|
+
code.forEach((n, j) => { const col = Math.floor(j / g.codeRows); n.x = x0 + c.pad + (col + 0.5) * c.codeCell; n.y = g.room + CODE_R + (j % g.codeRows + (col % 2) * 0.5) * c.codeRow; });
|
|
361
|
+
out.push(lane('code', CODE_LANE_TITLE, code.length, x0, 0, g.wCode, g.hCode));
|
|
362
|
+
}
|
|
363
|
+
return { lanes: out, order: [...g.parts.flatMap((part) => part.b.nodes.map((n) => n.id)), ...code.map((n) => n.id)], bounds: { x: 0, y: 0, w: g.w, h: g.h }, fit: fitBoxOf(g.w, g.h, z, c.margin, p, v) };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ---------------------------------------------------------------------------
|
|
367
|
+
// Layers and "Waiting on you"
|
|
368
|
+
// ---------------------------------------------------------------------------
|
|
369
|
+
|
|
370
|
+
/** The layers as the Work view starts them: every one on but the drafts (the local proposals nobody has been asked to decide) */
|
|
371
|
+
export const startLayers = () => Object.fromEntries(LAYER_KEYS.map((k) => [k, k !== DRAFT_LAYER]));
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Sets `hidden` and `dim` on the nodes. A piece of work is hidden when its layer is off; code when its layer is off or every piece of work that touches it is
|
|
375
|
+
* hidden (only code that work touches is drawn). With "Waiting on you" on, everything that does not wait is dimmed.
|
|
376
|
+
*/
|
|
377
|
+
export function applyFilters(nodes, edges, { layers = startLayers(), waiting = false } = {}) {
|
|
378
|
+
const on = (key) => layers[key] !== false;
|
|
379
|
+
const shown = new Set();
|
|
380
|
+
for (const n of nodes) { n.hidden = n.draft ? !on(DRAFT_LAYER) : n.work ? !on(workType(n.work)) : false; if (n.work && !n.hidden) shown.add(n.id); }
|
|
381
|
+
const reached = new Set();
|
|
382
|
+
for (const e of edges) if (e.tone === 'code' && shown.has(e.from)) reached.add(e.to);
|
|
383
|
+
for (const n of nodes) {
|
|
384
|
+
if (!n.work) n.hidden = !on(CODE_LAYER) || !reached.has(n.id);
|
|
385
|
+
n.dim = waiting && !n.waiting;
|
|
386
|
+
}
|
|
387
|
+
return nodes;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** The Layers menu's rows in the Work view: `{ key, label, color, desc, countText }`, the six kinds, the local drafts and the code they touch */
|
|
391
|
+
export function layerRowsOf(nodes) {
|
|
392
|
+
const of = (kind) => nodes.filter((n) => n.work === kind && !n.draft);
|
|
393
|
+
const word = (n, w) => `${n} ${w}`;
|
|
394
|
+
const rows = KIND_ORDER.map((kind) => {
|
|
395
|
+
const list = of(kind);
|
|
396
|
+
const open = list.filter((n) => n.item.open === true).length;
|
|
397
|
+
const waiting = list.filter((n) => n.waiting).length;
|
|
398
|
+
const countText = kind === 'proposal' ? (waiting ? word(waiting, 'to decide') : String(list.length))
|
|
399
|
+
: kind === 'run' ? (waiting ? word(waiting, 'waiting') : String(list.length))
|
|
400
|
+
: kind === 'pull' || kind === 'issue' ? word(open, 'open')
|
|
401
|
+
: String(list.length);
|
|
402
|
+
return { key: workType(kind), label: LAYER_WORDS[kind], color: WORK_THEMES[kind].color, desc: `${KIND_WORDS[kind]} nodes: ${list.length}`, countText, count: list.length };
|
|
403
|
+
});
|
|
404
|
+
const drafts = nodes.filter((n) => n.draft).length;
|
|
405
|
+
rows.push({ key: DRAFT_LAYER, label: DRAFT_LAYER_WORDS, color: WORK_THEMES.proposal.color, desc: 'Proposals that are only files in the checkout: no pull request holds them, nobody has been asked to decide them', countText: word(drafts, 'local'), count: drafts });
|
|
406
|
+
const codeCount = nodes.filter((n) => !n.work).length;
|
|
407
|
+
rows.push({ key: CODE_LAYER, label: CODE_LAYER_WORDS, color: NODE_THEMES.page.color, desc: 'The code the work touches', countText: String(codeCount), count: codeCount });
|
|
408
|
+
return rows;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** How many wait on the maintainer in this answer (spec 0010 D1's number: the server worked it out) */
|
|
412
|
+
export const waitingCountOf = (nodes) => nodes.filter((n) => n.waiting).length;
|
|
413
|
+
|
|
414
|
+
// ---------------------------------------------------------------------------
|
|
415
|
+
// The node panel
|
|
416
|
+
// ---------------------------------------------------------------------------
|
|
417
|
+
|
|
418
|
+
const PILL_OF = { open: 'open', closed: 'closed', merged: 'merged', success: 'success', failure: 'failure', running: 'running', queued: 'queued', waiting: 'waiting', done: 'done', failed: 'failed', stopped: 'stopped' };
|
|
419
|
+
|
|
420
|
+
/** A word for a piece of work's own state: "Open", "Merged", "To decide", "Failed" */
|
|
421
|
+
export function stateWordsOf(item) {
|
|
422
|
+
if (item.kind === 'proposal') return SPEC_STATE_WORDS[item.state] || text(item.state) || 'Proposal';
|
|
423
|
+
if (item.kind === 'pull') return item.state === 'open' ? (item.draft ? 'Draft' : 'Open') : item.state === 'merged' ? 'Merged' : 'Closed';
|
|
424
|
+
if (item.kind === 'deploy') return ({ success: 'Succeeded', failure: 'Failed', running: 'Running', queued: 'Waiting', cancelled: 'Cancelled', skipped: 'Skipped', neutral: 'Finished' })[item.state] || text(item.state);
|
|
425
|
+
if (item.kind === 'run') return text(item.state).replace(/-/g, ' ').replace(/^./, (c) => c.toUpperCase());
|
|
426
|
+
return item.open ? 'Open' : 'Closed';
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** The public `data-corn-state` word of a piece of work's state (corn-state.js), or '' */
|
|
430
|
+
export function pillStateOf(item) {
|
|
431
|
+
if (item.waiting) return 'waiting';
|
|
432
|
+
if (item.kind === 'pull') return item.draft ? 'draft' : PILL_OF[item.state] || '';
|
|
433
|
+
if (item.kind === 'proposal') return ({ 'to-decide': 'waiting', changed: 'waiting', approved: 'success', 'sent-back': 'sent-back', 'draft-pr': 'draft' })[item.state] || '';
|
|
434
|
+
if (item.kind === 'run') return ({ requested: 'requested', claimed: 'claimed', working: 'working', waiting: 'waiting', quiet: 'waiting', done: 'done', failed: 'failed', stopped: 'stopped' })[item.state] || '';
|
|
435
|
+
return PILL_OF[item.state] || (item.open ? 'open' : 'closed');
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** What the page says of what comes next, under the connections: what the action in the Work panel will do (the actions move here in step 3) */
|
|
439
|
+
export function saysOf(item) {
|
|
440
|
+
switch (item.kind) {
|
|
441
|
+
case 'proposal': return item.waiting ? 'This waits on you. Open it in the Work panel to open the review, approve or send it back.' : 'Open it in the Work panel for the review.';
|
|
442
|
+
case 'pull': return item.state === 'open' ? 'Open it in the Work panel to merge when the checks pass, or run the failed ones again.' : 'Nothing waits on you.';
|
|
443
|
+
case 'issue': return item.open ? 'Open it in the Work panel to start an agent on it.' : 'Nothing waits on you.';
|
|
444
|
+
case 'run': return item.waiting ? 'This waits on your answer. Open it in the Work panel to answer, continue or stop it.' : item.open ? 'Open it in the Work panel to follow it or stop it.' : 'Nothing waits on you.';
|
|
445
|
+
case 'thread': return 'Open it in the Work panel to read it and reply.';
|
|
446
|
+
default: return 'Open it in the Work panel for its logs, or to run it again.';
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* What the node panel says of a piece of work (the page draws it: work-view.js).
|
|
452
|
+
* @param {object} node a node of the Work view (a piece of work)
|
|
453
|
+
* @param {object[]} nodes every node of the view
|
|
454
|
+
* @param {object[]} edges every edge of the view
|
|
455
|
+
* @param {{ now?: number }} [opts]
|
|
456
|
+
*/
|
|
457
|
+
export function panelOf(node, nodes, edges, { now = Date.now() } = {}) {
|
|
458
|
+
const item = node.item;
|
|
459
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
460
|
+
const pills = [{ text: stateWordsOf(item), state: pillStateOf(item) }];
|
|
461
|
+
if (item.waiting) pills.push({ text: 'Waiting on you', state: 'waiting' });
|
|
462
|
+
if (item.kind === 'proposal' && Number.isInteger(item.number)) pills.push({ text: `PR #${item.number}`, state: '' });
|
|
463
|
+
if (item.kind === 'run' && item.runKind) pills.push({ text: text(item.runKind), state: '' });
|
|
464
|
+
const meta = [item.author ? text(item.author) : '', relativeTime(item.updated, now)].filter(Boolean).join(' · ');
|
|
465
|
+
const links = [];
|
|
466
|
+
for (const e of edges) {
|
|
467
|
+
const out = e.from === node.id;
|
|
468
|
+
const other = byId.get(out ? e.to : e.from);
|
|
469
|
+
if (!other || (!out && e.to !== node.id)) continue;
|
|
470
|
+
links.push({ rel: REL_WORDS[e.kind][out ? 0 : 1], id: other.id, label: other.label, work: other.work || '', code: !other.work, planned: other.planned === true });
|
|
471
|
+
}
|
|
472
|
+
// Work first (in the order of the lanes), then code; the same words together
|
|
473
|
+
const rank = (l) => (l.work ? LANES.findIndex((x) => x.key === (byId.get(l.id).stage || '')) : 9);
|
|
474
|
+
links.sort((a, b) => rank(a) - rank(b) || a.rel.localeCompare(b.rel) || a.label.localeCompare(b.label));
|
|
475
|
+
return { kind: KIND_WORDS[item.kind], tag: tagOf(item), title: node.title, color: WORK_THEMES[item.kind].color, pills, meta, links, says: saysOf(item), url: text(item.url) };
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// ---------------------------------------------------------------------------
|
|
479
|
+
// The panel's markup: every word that came from the forge is escaped, every link goes through `safeUrl` (an http or https address, else no link)
|
|
480
|
+
// ---------------------------------------------------------------------------
|
|
481
|
+
const pill = (p) => `<span class="hx-forge-state${p.state ? ` hx-forge-state--${esc(p.state)}` : ''}" data-corn="state-pill"${cornState(p.state) ? ` data-corn-state="${esc(cornState(p.state))}"` : ''}>${esc(p.text)}</span>`;
|
|
482
|
+
const dotOf = (kind, color) => { const c = typeof color === 'function' ? color(kind) : ''; return `<span class="hx-work-dot" data-corn="swatch" data-work-kind="${esc(kind || 'code')}" aria-hidden="true"${c ? ` data-style="background-color: ${esc(c)}"` : ''}></span>`; };
|
|
483
|
+
const linkButton = (l) => `<button type="button" class="hx-linkbtn" data-corn="node-link" data-action="select-node" data-id="${esc(l.id)}">${esc(l.label)}</button>`;
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* The node panel's markup for a piece of work (`panelOf`'s answer): its state, what it is connected to (each a button that selects that node), the checks of a pull request,
|
|
487
|
+
* the words saying what the action will do, "Open in Work" (the actions move to the node panel in step 3), and a link to the forge when the address is a web one.
|
|
488
|
+
* @param {ReturnType<typeof panelOf>} p
|
|
489
|
+
* @param {object} node
|
|
490
|
+
* @param {{ color?: (kind: string) => string, checks?: string }} [opts] `color`: a kind's colour as the map draws it; `checks`: the words of a pull request's checks
|
|
491
|
+
*/
|
|
492
|
+
export function workPanelHtml(p, node, { color = () => '', checks = '' } = {}) {
|
|
493
|
+
const url = safeUrl(p.url);
|
|
494
|
+
const links = p.links.length
|
|
495
|
+
? `<div class="hx-fact" data-corn="section"><div class="hx-fact-k">Connected</div><div class="hx-fact-v"><ul class="hx-work-links" data-corn="work-links">${p.links.map((l) => `<li data-corn="work-link"><span class="hx-work-rel">${esc(l.rel)}</span>${dotOf(l.work, color)}${linkButton(l)}${l.planned ? ' <span class="hx-faint">planned</span>' : ''}</li>`).join('')}</ul></div></div>`
|
|
496
|
+
: '<div class="hx-fact" data-corn="section"><div class="hx-fact-k">Connected</div><div class="hx-fact-v hx-faint">Nothing it is linked to is drawn.</div></div>';
|
|
497
|
+
const checked = node.item.kind === 'pull' ? `<div class="hx-fact" data-corn="section"><div class="hx-fact-k">Checks</div><div class="hx-fact-v" id="work-checks" data-corn="work-checks">${esc(checks)}</div></div>` : '';
|
|
498
|
+
const actions = `<div class="hx-work-actions" data-corn="work-actions"><p class="hx-faint">${esc(p.says)}</p><button type="button" class="hx-btn hx-btn--primary hx-foot-btn" data-corn-kind="primary" data-work-action="open-in-work" data-id="${esc(node.id)}">Open in Work</button>${url ? `<a class="hx-btn hx-btn--quiet hx-foot-btn" data-corn-kind="quiet" data-corn="open-link" href="${esc(url)}" target="_blank" rel="noreferrer">Open on the forge ↗</a>` : ''}</div>`;
|
|
499
|
+
return `<div class="hx-work-head" data-corn="work-head"><p class="hx-work-pills" data-corn="work-pills">${p.pills.map(pill).join(' ')}</p><p class="hx-faint">${esc(p.tag)}${p.meta ? ` · ${esc(p.meta)}` : ''}</p></div>${links}${checked}${actions}`;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** The node panel's section for a node of code in the Work view: the work that touches it, and "Show on the map" (a planned node is not on the map yet) */
|
|
503
|
+
export function codePanelHtml(node, touching, { color = () => '' } = {}) {
|
|
504
|
+
const list = touching.length
|
|
505
|
+
? `<ul class="hx-work-links" data-corn="work-links">${touching.map((l) => `<li data-corn="work-link"><span class="hx-work-rel">${esc(l.rel)}</span>${dotOf(l.work, color)}${linkButton(l)}</li>`).join('')}</ul>`
|
|
506
|
+
: '<p class="hx-faint">No work touches it now.</p>';
|
|
507
|
+
const show = node.planned ? '' : `<button type="button" class="hx-btn hx-btn--secondary hx-foot-btn" data-corn-kind="secondary" data-work-action="show-on-map" data-id="${esc(node.id)}">Show on the map</button>`;
|
|
508
|
+
return `<div class="hx-fact" data-corn="section"><div class="hx-fact-k">Work</div><div class="hx-fact-v">${node.planned ? '<p class="hx-faint">Planned: a proposal adds it, and the map does not hold it yet.</p>' : ''}${list}${show}</div></div>`;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** The work that touches a node of code, as the node panel lists it: `{ rel, id, label, work }` */
|
|
512
|
+
export function touchedBy(node, nodes, edges) {
|
|
513
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
514
|
+
const out = [];
|
|
515
|
+
for (const e of edges) {
|
|
516
|
+
if (e.to !== node.id) continue;
|
|
517
|
+
const from = byId.get(e.from);
|
|
518
|
+
if (from && from.work) out.push({ rel: REL_WORDS[e.kind][1], id: from.id, label: from.label, work: from.work, waiting: from.waiting });
|
|
519
|
+
}
|
|
520
|
+
return out.sort((a, b) => Number(b.waiting) - Number(a.waiting) || a.label.localeCompare(b.label));
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** The checks line of a pull request, from `GET /api/forge/pulls/<n>`'s `checks` ("1 failed, 1 still running", "All 3 passed", "No checks") */
|
|
524
|
+
export { checksWords } from './forge-panel-model.js';
|
|
525
|
+
|
|
526
|
+
// ---------------------------------------------------------------------------
|
|
527
|
+
// The keyboard (N and P step through the lanes; an arrow goes to the nearest node that way)
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
|
|
530
|
+
/** The node an arrow goes to from `from`: the nearest shown node that way, a little more for each step aside; null when there is none (and the first of `order` when nothing is selected) */
|
|
531
|
+
export function arrowTarget(nodes, from, key) {
|
|
532
|
+
const dir = { ArrowRight: [1, 0], ArrowLeft: [-1, 0], ArrowDown: [0, 1], ArrowUp: [0, -1] }[key];
|
|
533
|
+
if (!dir) return null;
|
|
534
|
+
const shown = nodes.filter((n) => !n.hidden && n.x !== undefined);
|
|
535
|
+
if (!from) return null;
|
|
536
|
+
let best = null;
|
|
537
|
+
let bestScore = Infinity;
|
|
538
|
+
for (const n of shown) {
|
|
539
|
+
if (n === from || n.id === from.id) continue;
|
|
540
|
+
const along = (n.x - from.x) * dir[0] + (n.y - from.y) * dir[1];
|
|
541
|
+
if (along < 1) continue;
|
|
542
|
+
const aside = Math.abs((n.x - from.x) * dir[1]) + Math.abs((n.y - from.y) * dir[0]);
|
|
543
|
+
if (aside > (dir[0] ? 2 * along + 60 : 0.6 * along + 40)) continue; // a cone: Down stays in its column, Right may reach a lane whose rows are a little off
|
|
544
|
+
const score = along + 3 * aside;
|
|
545
|
+
if (score < bestScore) { best = n; bestScore = score; }
|
|
546
|
+
}
|
|
547
|
+
return best;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** What a screen reader hears when the keyboard lands on a node of the Work view: its name, its kind and state, what waits, and its place in the lanes */
|
|
551
|
+
export function landingWords(node, index, count) {
|
|
552
|
+
if (!node.work) return `${node.label}, code${node.planned ? ', planned' : ''}, ${node.touches || 0} ${node.touches === 1 ? 'piece' : 'pieces'} of work touch it, ${index + 1} of ${count}`;
|
|
553
|
+
const item = node.item;
|
|
554
|
+
return `${node.label}, ${KIND_WORDS[item.kind].toLowerCase()}${node.draft ? ' (a local draft)' : ''}, ${stateWordsOf(item).toLowerCase()}${node.waiting ? ', waiting on you' : ''}, ${index + 1} of ${count}`;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// ---------------------------------------------------------------------------
|
|
558
|
+
// Addresses and search
|
|
559
|
+
// ---------------------------------------------------------------------------
|
|
560
|
+
|
|
561
|
+
const NUMBER = /^[1-9][0-9]{0,8}$/;
|
|
562
|
+
const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* What an address asks of the Work view (spec 0012 D8): `{ view: true }` for `?view=work`, and which node to select for `?pr=`, `?issue=`, `?thread=`, `?run=`
|
|
566
|
+
* and `?spec=` (a number or a slug). One link asks for one node: the first of them in that order. A value that cannot be a name is `{ bad }`. Null when it asks nothing.
|
|
567
|
+
* @param {string} search
|
|
568
|
+
* @returns {{ view: boolean, kind?: string, key?: string, bad?: string } | null}
|
|
569
|
+
*/
|
|
570
|
+
export function workAddress(search) {
|
|
571
|
+
const params = new URLSearchParams(search || '');
|
|
572
|
+
const asks = [['pr', 'pull'], ['issue', 'issue'], ['thread', 'thread'], ['run', 'run'], ['spec', 'proposal']];
|
|
573
|
+
for (const [param, kind] of asks) {
|
|
574
|
+
if (!params.has(param)) continue;
|
|
575
|
+
const value = params.get(param) || '';
|
|
576
|
+
const good = kind === 'run' ? /^run-[0-9A-Za-z]{26}$/.test(value) : kind === 'proposal' ? SLUG.test(value) : NUMBER.test(value);
|
|
577
|
+
if (!good) return { view: true, kind, bad: `${value ? `"${value.slice(0, 40)}"` : 'That'} is not ${kind === 'pull' ? 'a pull request number' : kind === 'issue' ? 'an issue number' : kind === 'thread' ? 'a thread number' : kind === 'run' ? 'a run id' : 'a spec number or name'}.` };
|
|
578
|
+
return { view: true, kind, key: value };
|
|
579
|
+
}
|
|
580
|
+
return params.get('view') === 'work' ? { view: true } : null;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** The id of the node an address names, among the nodes of the view, or null (a spec by its number, `0100`, or by its whole slug) */
|
|
584
|
+
export function nodeForAddress(ask, nodes) {
|
|
585
|
+
if (!ask || !ask.kind) return null;
|
|
586
|
+
const mine = nodes.filter((n) => n.work === ask.kind);
|
|
587
|
+
if (ask.kind === 'pull' && !mine.some((n) => String(n.item.number) === ask.key)) { const spec = nodes.find((n) => n.work === 'proposal' && String(n.item.number) === ask.key); if (spec) return spec.id; } // a spec's pull request is its proposal
|
|
588
|
+
if (ask.kind === 'issue' && !mine.some((n) => String(n.item.number) === ask.key)) { const idea = nodes.find((n) => n.work === 'thread' && String(n.item.number) === ask.key); if (idea) return idea.id; } // an idea is an issue on the forge, drawn as a thread
|
|
589
|
+
const hit = ask.kind === 'proposal'
|
|
590
|
+
? mine.find((n) => n.item.slug === ask.key) || mine.find((n) => specNumber(text(n.item.slug)) === ask.key)
|
|
591
|
+
: ask.kind === 'run' ? mine.find((n) => n.item.runId === ask.key) : mine.find((n) => String(n.item.number) === ask.key);
|
|
592
|
+
return hit ? hit.id : null;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** The address a node of work has in the page (`?pr=27`), so a link can be given; null for a node that has none (a deploy) */
|
|
596
|
+
export function addressOf(node) {
|
|
597
|
+
const i = node && node.item;
|
|
598
|
+
if (!i) return null;
|
|
599
|
+
if (i.kind === 'pull') return `?pr=${i.number}`;
|
|
600
|
+
if (i.kind === 'issue') return `?issue=${i.number}`;
|
|
601
|
+
if (i.kind === 'thread') return `?thread=${i.number}`;
|
|
602
|
+
if (i.kind === 'run') return `?run=${encodeURIComponent(i.runId)}`;
|
|
603
|
+
if (i.kind === 'proposal') return `?spec=${encodeURIComponent(i.slug)}`;
|
|
604
|
+
return null;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** The words a screen reader hears when the view opens: "Work: 14 pieces of work, 3 waiting on you, and the 9 nodes of code they touch." */
|
|
608
|
+
export function openedWords({ work, code, waiting }) {
|
|
609
|
+
const w = `${work} ${work === 1 ? 'piece' : 'pieces'} of work`;
|
|
610
|
+
const c = code ? `, and the ${code} ${code === 1 ? 'node' : 'nodes'} of code ${code === 1 ? 'it touches' : 'they touch'}` : '';
|
|
611
|
+
return `Work: ${w}${waiting ? `, ${waiting} waiting on you` : ''}${c}.`;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ---------------------------------------------------------------------------
|
|
615
|
+
// The view on the state (the same way house-model.js holds the House view's)
|
|
616
|
+
// ---------------------------------------------------------------------------
|
|
617
|
+
|
|
618
|
+
/** What the Work view keeps on the state (`state.work`): the answer, the scene made of it, the layers, the toggle, and the map it stands in for while it is open */
|
|
619
|
+
export const newWorkState = () => ({ status: 'idle', graph: null, nodes: [], edges: [], lanes: [], counts: { work: 0, code: 0, waiting: 0 }, problems: [], layers: startLayers(), waiting: false, saved: null, bounds: null, fit: null, order: [], updating: false, stale: '', lastError: '' });
|
|
620
|
+
|
|
621
|
+
/** Makes the scene of an answer (`workScene`), keeps the node a selection named, and sets the filters; the layout is `applyWorkLayout`'s */
|
|
622
|
+
export function setWorkGraph(state, graph, mapNodes) {
|
|
623
|
+
const w = state.work || (state.work = newWorkState());
|
|
624
|
+
const scene = workScene(graph, mapNodes);
|
|
625
|
+
w.graph = graph;
|
|
626
|
+
w.nodes = scene.nodes;
|
|
627
|
+
w.edges = scene.edges;
|
|
628
|
+
w.counts = scene.counts;
|
|
629
|
+
w.problems = Array.isArray(graph && graph.problems) ? graph.problems : [];
|
|
630
|
+
applyFilters(w.nodes, w.edges, { layers: w.layers, waiting: w.waiting });
|
|
631
|
+
return w;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** Puts the Work view's nodes on the state, keeping the map's to put back (`leaveWork`) */
|
|
635
|
+
export function enterWork(state) {
|
|
636
|
+
const w = state.work || (state.work = newWorkState());
|
|
637
|
+
if (!w.saved) w.saved = { nodes: state.nodes, edges: state.edges, rings: state.rings };
|
|
638
|
+
showWork(state);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** The Work view's nodes, edges, layers and lanes on the state (the answer changed, or the view opened) */
|
|
642
|
+
export function showWork(state) {
|
|
643
|
+
const w = state.work;
|
|
644
|
+
state.workEdge = workEdge; // the scene draws the Work view's lines with it (scene.js)
|
|
645
|
+
state.nodes = w.nodes;
|
|
646
|
+
state.edges = w.edges;
|
|
647
|
+
state.rings = { rings: [], clusters: [] };
|
|
648
|
+
state.workLayers = w.layers; // set only while the view is open (leaveWork clears it): the layer code reads its presence as the Work view
|
|
649
|
+
state.workRows = layerRowsOf(w.nodes);
|
|
650
|
+
state.workLanes = w.lanes;
|
|
651
|
+
state.selectedNode = state.selectedNode ? w.nodes.find((n) => n.id === state.selectedNode.id) || null : null; // the same id is the same selection
|
|
652
|
+
state.selectedEdge = null;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** The map's nodes, edges and layers as they were */
|
|
656
|
+
export function leaveWork(state) {
|
|
657
|
+
const w = state.work;
|
|
658
|
+
if (!w || !w.saved) return;
|
|
659
|
+
state.nodes = w.saved.nodes;
|
|
660
|
+
state.edges = w.saved.edges;
|
|
661
|
+
state.rings = w.saved.rings;
|
|
662
|
+
w.saved = null;
|
|
663
|
+
state.workLayers = null;
|
|
664
|
+
state.workRows = null;
|
|
665
|
+
state.workLanes = null;
|
|
666
|
+
state.selectedNode = state.selectedNode ? state.nodes.find((n) => n.id === state.selectedNode.id) || null : null;
|
|
667
|
+
state.selectedEdge = null;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Lays the Work view out for the map's size (writes on its nodes), and keeps its lanes for the scene and the box the first fit shows (`state.work.fit`: engine/render.js) */
|
|
671
|
+
export function applyWorkLayout(state, { phone = false } = {}) {
|
|
672
|
+
const w = state.work;
|
|
673
|
+
if (!w) return;
|
|
674
|
+
const laid = layoutWork(w.nodes, { phone, width: state.width || (phone ? 390 : 1200), height: state.height || (phone ? 760 : 800), padding: (state.width || 1200) < 640 ? 44 : 96 }); // the engine's own padding around a fit in this view (engine/render.js `fitPadding`)
|
|
675
|
+
w.lanes = laid.lanes;
|
|
676
|
+
w.order = laid.order;
|
|
677
|
+
w.bounds = laid.bounds;
|
|
678
|
+
w.fit = laid.fit;
|
|
679
|
+
state.workLanes = w.lanes;
|
|
680
|
+
state.workRows = layerRowsOf(w.nodes);
|
|
681
|
+
}
|