Repository navigation
Expand file tree
/
Copy pathload.js
More file actions
312 lines (281 loc) · 11.2 KB
/
Copy pathload.js
File metadata and controls
312 lines (281 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* @import { View } from "./view.js"
* @import { Row } from "./row.js"
* @import { Queue } from "./screen.js"
*/
import {
addedLabel,
assignability,
hasLabel,
incomplete,
isLogin,
outdated,
query,
searchLimit,
summarize,
} from "./pull-request.js";
import { describe, github, graphql, notFound, signedIn, signedInUser } from "./github.js";
import { fillDetails } from "./details.js";
import { forget, restore, store } from "./queues.js";
import { fill, mark, summaries, unmark } from "./row.js";
import { viewOf } from "./view.js";
import {
hideFailure,
showDelta,
showFailure,
showLoaded,
showLoading,
showNotice,
} from "./status.js";
import {
countsAs,
hideQueue,
onScreen,
queue,
sameQueue,
showQueue,
showSkeleton,
} from "./screen.js";
let status = document.querySelector("#status");
let progress = document.querySelector("progress");
let signIn = document.querySelector("#sign-in");
let controls = document.querySelector("#controls");
let repoForm = document.querySelector("#repo-form");
/** @type {AbortController | undefined} Aborts the load in flight, if any */
export let loading;
/**
* Load the queue the URL asks for, and stop a load still in flight.
* Rows of the same repo, user and role stay until the new ones arrive, e.g. on a refresh or a new filter, and those of the same counts update in place.
*/
export async function load () {
loading?.abort();
loading = new AbortController();
let { signal } = loading;
let view = viewOf();
let refreshing = onScreen(view);
// Without the marks, a row with nothing new equals its next render, so it stays and keeps focus
for (let { tr } of queue?.rows ?? []) {
unmark(tr);
}
// Another view shows at once what it can, e.g. the rows with a label just added, or what it showed on the last visit
if (!refreshing) {
// NOTE: A stored queue brings its own answer of who GitHub lets be assigned, which may be older than the one on screen
let next = narrowed(view) ?? restore(view, queue?.rows);
if (next) {
showQueue(next);
}
}
// A refresh keeps the last delta, dimmed, until it ends. Another view has another count, e.g. one narrowed by a label.
// A retry keeps the last failure in place until it ends, so the queue under it doesn’t move. Another view’s rows didn’t fail
if (!refreshing) {
showDelta();
hideFailure();
}
// A load of another view, e.g. a new filter, finds PRs that aren’t new, so it compares with nothing.
// By now a narrowed or restored queue is of this view too
showLoading({ comparing: onScreen(view) && Boolean(queue.seen) });
// Bars stand in for rows of another queue, e.g. another repo’s, user’s or role’s PRs, which would sit under this view’s names.
// They also stand in for no rows at all, e.g. on a first visit. Rows under another filter stay until the search replaces them.
// A stored token signs in without a card, so bars show during the sign-in. Without one, they would flash before the sign-in card
// NOTE: An expired token still shows them before the card
if (!sameQueue(view) && github.isAuthenticated()) {
showSkeleton(view);
}
try {
await login(view.repo, signal);
signal.throwIfAborted();
controls.inert = false;
let user = view.user || signedInUser();
// Bars stand in for the rows while nothing is on screen yet, e.g. after the sign-in card, or while bars wait for the user’s name.
// Bars also replace a stored queue of whoever was signed in then, when that was someone else.
// A failed search hides them
if (!queue || !isLogin(queue.user, user)) {
showSkeleton(view, user);
}
let stale = await find(view, user, signal);
let [failed, assignable] = await Promise.all([
fillDetails(stale, queue, signal),
findAssignable(queue, signal),
]);
signal.throwIfAborted();
finish(failed, assignable);
}
catch (error) {
// The newer load shows its own outcome
if (signal.aborted) {
return;
}
fail(view, error);
}
loading = undefined;
showLoaded();
}
/**
* The queue on screen narrowed to a label added to the filter, e.g. by a click on it, if any.
* The rows on screen that have the label stay, since the search would find just those.
* @param {View} view
* @returns {Queue | undefined}
*/
function narrowed (view) {
// A search that found its maximum of 100 PRs may have missed others with the label, so then the rows wait for the search
let label =
countsAs(view) && queue.rows.length < searchLimit
? addedLabel(queue.view.filter, view.filter)
: undefined;
if (label === undefined) {
return;
}
let rows = queue.rows.filter(({ pr }) => hasLabel(pr, label));
// The load marks what changed against the last finished load’s rows that stay on screen. Before the first load finishes there are none
// NOTE: A PR that a refresh in flight already dropped isn’t among them, so it doesn’t count as left
let seen =
queue.seen &&
new Map(queue.seen.entries().filter(([id]) => rows.some(row => row.pr.id === id)));
return { ...queue, view, rows, seen };
}
/**
* Search for the PRs of a view, and show them with the fields the search returns.
* Their slow fields wait for {@link fillDetails}.
* @param {View} view
* @param {string} user - The view’s, or the signed-in user when it names none
* @param {AbortSignal} signal - The load’s
* @returns {Promise<Row[]>} The rows whose details to load
*/
async function find (view, user, signal) {
let { role } = view;
// Rows of another queue, e.g. before a role switch, stay on screen until the search replaces them, but don’t update
let kept = countsAs(view) ? queue.rows : [];
// Who GitHub let be assigned in the repo stays until it answers again, e.g. under another pattern
let assignable = sameQueue(view) ? queue.assignable : undefined;
let { repository, search: found } = await graphql(query({ ...view, user }), signal);
signal.throwIfAborted();
let previous = new Map(kept.map(row => [row.pr.id, row]));
let stale = [];
let rows = found.nodes.map(pr => {
let row = previous.get(pr.id) ?? {};
// The search asks for the repository once. turn() reads it on each PR, as `pr.repository` in GitHub’s model
row.pr = { ...pr, repository };
// Without details the summary has no slow fields, so a refreshed row keeps its old ones, dimmed, until they arrive
fill(row, { ...row.summary, ...summarize(pr, user, role) });
// The details are checked against their own copy. A load stopped before they came, e.g. by a label click, already replaced `pr`.
// An author’s details are checked on every load, since a finished check run changes neither `updatedAt` nor the head commit
if (
!row.detailed ||
role === "author" ||
incomplete(row.summary) ||
outdated(row.detailed, pr)
) {
stale.push(row);
if (row.detailed) {
row.tr.classList.add("stale");
}
}
return row;
});
// What the last finished load showed, and when, holds only for its own view, e.g. not after a role switch
let { at, seen } = onScreen(view) ? queue : {};
showQueue({ view, user, rows, at, seen, assignable });
return stale;
}
/**
* Ask GitHub which authors of the queue it lets be assigned, so they count among the collaborators.
* Each load asks again, e.g. after someone lost access. Until it answers, the queue keeps the last answer, stored with it.
* @param {Queue} queue - The load’s
* @param {AbortSignal} signal - The load’s
* @returns {Promise<string[] | undefined>} Their logins, or nothing when GitHub didn’t answer
*/
async function findAssignable ({ view: { repo, role }, rows }, signal) {
// An author’s view lists no authors, and an empty queue has none to ask about
if (role === "author" || rows.length === 0) {
return;
}
try {
let logins = [...new Set(rows.map(row => row.summary.author))];
let { repository } = await graphql(assignability(repo, logins), signal);
signal.throwIfAborted();
// Shown by finish(). Shown sooner, the rows would take their places before finish() slides them there
return Object.values(repository).flatMap(({ nodes }) => nodes.map(({ login }) => login));
}
catch (error) {
signal.throwIfAborted();
// The last answer, if any, and the roles still tell most collaborators apart
console.warn("Couldn’t ask GitHub who can be assigned.", error);
}
}
/**
* Show the queue on screen as the load leaves it, with what changed since the last one, and store it.
* A queue with failed details shows the failure beside Refresh instead, and isn’t stored
* @param {PromiseRejectedResult} [failed] - The first failure of {@link fillDetails}, if any
* @param {string[]} [assignable] - What {@link findAssignable} found, if GitHub answered
*/
function finish (failed, assignable = queue.assignable) {
let { rows, seen: last } = queue;
// The user has seen what the search found, even when details failed
showQueue({ ...queue, at: Date.now(), seen: summaries(rows), assignable }, { animate: true });
// Rows whose details didn’t arrive, e.g. failed or of a deleted PR, show what they had, undimmed.
// The next load asks for them again. The loop goes over `rows`, not the table, since rows of authors not picked are off the page
for (let { tr } of rows) {
if (tr.classList.contains("stale")) {
unmark(tr);
}
}
if (last) {
// NOTE: Counts the PRs of authors not picked and hidden drafts too, e.g. “+1” for a new PR that doesn’t show.
// A draft turned ready for review, which shows only then, is marked changed, not new
showDelta(mark(rows, last));
}
// The search did refresh the queue, so a failure shows beside Refresh, never with the form
if (failed) {
showFailure(describe(failed.reason));
}
else {
hideFailure();
store(queue);
}
}
/**
* Say why a load failed, beside Refresh or with the form.
* @param {View} view
* @param {unknown} error - What the load threw
*/
function fail (view, error) {
// Rows of another view, or of no finished load, go. They would sit under this view’s labels, filter and time.
// A step back then shows the stored queue, as a visit does
if (!onScreen(view) || !queue.at) {
hideQueue();
}
showDelta();
// A repo or user that GitHub can’t find needs the form to fix them.
// So does a failure with no finished load on screen, e.g. on a first visit, where most failures come from the repo or the user
if (queue && !notFound.test(error?.message)) {
showFailure(describe(error));
}
else {
hideFailure();
showNotice(
repoForm,
`Couldn’t load the pull requests in ${view.repo}`,
describe(error, "Check the repository and the user, then try again."),
);
}
}
async function login (repo, signal) {
if (await signedIn(signal)) {
return;
}
// No stored token. The sign-in popup needs a click, or the browser blocks it.
// Whoever signs in may be someone else, e.g. after a token expired, so the queues of the last one go, stored and on screen
// NOTE: The same user signed in again loses the marks of the next load, and its details load anew
forget();
hideQueue();
hideFailure();
status.hidden = false;
status.textContent = `Sign in to see the pull requests in ${repo}.`;
signIn.querySelector(".repo").textContent = repo;
progress.hidden = true;
signIn.hidden = false;
// Waits for the sign-in, not the click. Madata’s login() never settles if the popup is closed, so the button stays for another try
await new Promise(resolve => github.addEventListener("mv-login", resolve, { once: true }));
signIn.hidden = true;
showLoading();
}