agl 42.0.0 → 42.1.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 (58) hide show
  1. package/changelog.md +6 -0
  2. package/dist_serve/bundle.js +2493 -2143
  3. package/dist_serve/bundle.js.map +2 -2
  4. package/dist_ts/00_commitinfo_data.js +1 -1
  5. package/dist_ts/aglapi/aglapisurface.document.js +2 -2
  6. package/dist_ts/classes.authstore.d.ts +2 -0
  7. package/dist_ts/classes.authstore.js +50 -17
  8. package/dist_ts/classes.controller.d.ts +71 -2
  9. package/dist_ts/classes.controller.js +500 -113
  10. package/dist_ts/classes.controllerdevices.d.ts +128 -0
  11. package/dist_ts/classes.controllerdevices.js +472 -0
  12. package/dist_ts/classes.sessionfeed.d.ts +10 -1
  13. package/dist_ts/classes.sessionfeed.js +12 -2
  14. package/dist_ts/classes.validation.d.ts +2 -0
  15. package/dist_ts/classes.validation.js +2 -2
  16. package/dist_ts/functions.deviceapi.d.ts +28 -0
  17. package/dist_ts/functions.deviceapi.js +246 -0
  18. package/dist_ts/functions.deviceproof.d.ts +33 -0
  19. package/dist_ts/functions.deviceproof.js +93 -0
  20. package/dist_ts/functions.remotecli.d.ts +1 -1
  21. package/dist_ts/functions.remotecli.js +12 -7
  22. package/dist_ts/interfaces.auth.d.ts +1 -1
  23. package/dist_ts/interfaces.auth.js +6 -1
  24. package/dist_ts_interfaces/devices.d.ts +331 -0
  25. package/dist_ts_interfaces/devices.js +34 -0
  26. package/dist_ts_interfaces/index.d.ts +2 -0
  27. package/dist_ts_interfaces/index.js +3 -1
  28. package/dist_ts_interfaces/interfaces.d.ts +4 -2
  29. package/dist_ts_interfaces/interfaces.js +3 -2
  30. package/dist_ts_interfaces/mcprequests.d.ts +1 -1
  31. package/dist_ts_interfaces/requests.d.ts +2 -1
  32. package/dist_ts_interfaces/requests.devices.d.ts +89 -0
  33. package/dist_ts_interfaces/requests.devices.js +2 -0
  34. package/dist_ts_interfaces/requests.js +1 -1
  35. package/package.json +1 -1
  36. package/readme.md +36 -4
  37. package/ts/00_commitinfo_data.ts +1 -1
  38. package/ts/aglapi/aglapisurface.document.ts +2 -2
  39. package/ts/classes.authstore.ts +46 -14
  40. package/ts/classes.controller.ts +697 -167
  41. package/ts/classes.controllerdevices.ts +469 -0
  42. package/ts/classes.sessionfeed.ts +13 -1
  43. package/ts/classes.validation.ts +1 -1
  44. package/ts/functions.deviceapi.ts +295 -0
  45. package/ts/functions.deviceproof.ts +118 -0
  46. package/ts/functions.remotecli.ts +11 -5
  47. package/ts/interfaces.auth.ts +5 -0
  48. package/ts_interfaces/devices.schema.json +890 -0
  49. package/ts_interfaces/devices.ts +392 -0
  50. package/ts_interfaces/index.ts +2 -0
  51. package/ts_interfaces/interfaces.ts +4 -1
  52. package/ts_interfaces/mcprequests.ts +1 -1
  53. package/ts_interfaces/requests.devices.ts +95 -0
  54. package/ts_interfaces/requests.ts +10 -0
  55. package/ts_web/00_commitinfo_data.ts +1 -1
  56. package/ts_web/classes.controllersocketclient.ts +27 -0
  57. package/ts_web/elements.devices.ts +752 -0
  58. package/ts_web/elements.harnesscontrollerapp.ts +18 -1
@@ -0,0 +1,752 @@
1
+ import * as plugins from './plugins.js';
2
+ import type * as interfaces from '../ts_interfaces/index.js';
3
+ import { maximumDevices } from '../ts_interfaces/devices.js';
4
+ import type { ControllerSocketClient } from './classes.controllersocketclient.js';
5
+ import { isControllerDevice } from './classes.controllersocketclient.js';
6
+
7
+ /**
8
+ * Settings → Devices: the phones and computers paired with this controller. The owner pairs one by letting it scan
9
+ * the QR code of a single-use code, or by pasting the same code as a pairing link on a device without a camera,
10
+ * compares the key fingerprint the device shows, and revokes a device, which ends its connections at once. The screen
11
+ * reads the controller on open and whenever the controller announces `devices.changed`; it never polls. Its only
12
+ * timer is the pairing code's countdown, which runs while the pairing dialog waits. The code reaches the clipboard
13
+ * only when the owner presses "Copy pairing link" beside the QR code, as the QR code's own payload; nothing copies it
14
+ * on its own, and closing the dialog withdraws it on the controller.
15
+ */
16
+
17
+ type TDevicesRead =
18
+ | { state: 'pending' }
19
+ | { state: 'answered'; devices: interfaces.IControllerDevice[]; pairing: interfaces.TControllerDevicePairingAvailability }
20
+ | { state: 'failed'; message: string };
21
+
22
+ /** What the last "Copy pairing link" of a waiting code did: nothing yet, the link is on the clipboard, or it was refused. */
23
+ type TPairingCopy = 'none' | 'copied' | 'refused';
24
+
25
+ /** The pairing dialog: making a code, waiting for a device to scan it, the device that paired, or why there is none. */
26
+ type TPairingView =
27
+ | { state: 'starting' }
28
+ | { state: 'waiting'; pairingId: string; payload: string; expiresAt: number; copy: TPairingCopy }
29
+ | { state: 'paired'; device: interfaces.IControllerDevice }
30
+ | { state: 'expired' }
31
+ | { state: 'refused'; message: string };
32
+
33
+ type TDevicesDialog = { kind: 'pair' } | { kind: 'revoke'; deviceId: string };
34
+
35
+ interface IDevicesNotice {
36
+ type: plugins.deesCatalog.DeesHint['type'];
37
+ text: string;
38
+ }
39
+
40
+ const errorText = (errorArg: unknown, fallbackArg: string): string => (
41
+ errorArg instanceof Error && errorArg.message.trim() ? errorArg.message.trim() : fallbackArg
42
+ );
43
+
44
+ const dateTime = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' });
45
+
46
+ /** Minutes and seconds left, as `4:59`. */
47
+ const countdown = (millisecondsArg: number): string => {
48
+ const seconds = Math.max(0, Math.ceil(millisecondsArg / 1000));
49
+ return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
50
+ };
51
+
52
+ /** Why no device can pair now, in the owner's words; empty while one can. */
53
+ const pairingBlockedReason = (pairingArg: interfaces.TControllerDevicePairingAvailability): string => {
54
+ if (pairingArg.available) return '';
55
+ return pairingArg.reason === 'deviceLimit'
56
+ ? `This controller holds its maximum of ${maximumDevices} devices. Revoke one to pair another.`
57
+ : 'A device reaches AGL through its public HTTPS origin, and this controller is reachable on this machine only'
58
+ + ' (local mode). Move it behind its gateway with “agl remote set” to pair devices.';
59
+ };
60
+
61
+ const deviceStatus = (deviceArg: interfaces.IControllerDevice): { type: 'success' | 'default'; text: string } => (
62
+ deviceArg.connected
63
+ ? { type: 'success', text: 'Connected' }
64
+ : { type: 'default', text: deviceArg.lastSeenAt === undefined ? 'Not connected yet' : 'Offline' }
65
+ );
66
+
67
+ @plugins.deesElement.customElement('agl-devices')
68
+ export class AglDevices extends plugins.deesElement.DeesElement {
69
+ @plugins.deesElement.property({ attribute: false })
70
+ accessor client: ControllerSocketClient | undefined;
71
+
72
+ @plugins.deesElement.state()
73
+ private accessor read: TDevicesRead = { state: 'pending' };
74
+
75
+ @plugins.deesElement.state()
76
+ private accessor reading = false;
77
+
78
+ /** The device whose page is open; empty for the list. */
79
+ @plugins.deesElement.state()
80
+ private accessor openId = '';
81
+
82
+ @plugins.deesElement.state()
83
+ private accessor dialog: TDevicesDialog | null = null;
84
+
85
+ @plugins.deesElement.state()
86
+ private accessor pairing: TPairingView = { state: 'starting' };
87
+
88
+ /** The action in flight, if any; cleared on every outcome. */
89
+ @plugins.deesElement.state()
90
+ private accessor busy = '';
91
+
92
+ /** What the last action that was not done said; the next action replaces it. */
93
+ @plugins.deesElement.state()
94
+ private accessor notice: IDevicesNotice | null = null;
95
+
96
+ /** The clock the pairing countdown reads; ticks once a second while a code waits. */
97
+ @plugins.deesElement.state()
98
+ private accessor now = Date.now();
99
+
100
+ private ticker: ReturnType<typeof setInterval> | undefined;
101
+ private readGeneration = 0;
102
+ private latestRead: Promise<void> = Promise.resolve();
103
+ private configuredClient: ControllerSocketClient | undefined;
104
+
105
+ public static styles = [
106
+ plugins.deesCatalog.themeDefaultStyles,
107
+ plugins.deesElement.cssManager.defaultStyles,
108
+ plugins.deesElement.css`
109
+ :host {
110
+ display: block;
111
+ min-width: 0;
112
+ color: var(--dees-color-text-primary);
113
+ font-family: var(--dees-font-family);
114
+ }
115
+
116
+ .screen { display: grid; gap: var(--dees-spacing-lg); }
117
+
118
+ .bar {
119
+ display: flex;
120
+ flex-wrap: wrap;
121
+ align-items: center;
122
+ justify-content: space-between;
123
+ gap: var(--dees-spacing-md);
124
+ }
125
+
126
+ .count { color: var(--dees-color-text-muted); font-size: var(--dees-font-footnote-size); }
127
+ .tools, .actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--dees-spacing-xs); }
128
+
129
+ .card {
130
+ display: grid;
131
+ overflow: hidden;
132
+ border: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle);
133
+ border-radius: var(--dees-radius-xl);
134
+ background: var(--dees-color-bg-primary);
135
+ }
136
+
137
+ .card > * + * { border-top: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle); }
138
+
139
+ .row {
140
+ display: grid;
141
+ grid-template-columns: auto minmax(0, 1fr) auto auto;
142
+ align-items: center;
143
+ gap: var(--dees-spacing-md);
144
+ width: 100%;
145
+ min-height: var(--dees-control-height-touch, 44px);
146
+ padding: var(--dees-spacing-sm) var(--dees-spacing-lg);
147
+ border: 0;
148
+ background: var(--dees-color-bg-primary);
149
+ color: inherit;
150
+ font: inherit;
151
+ text-align: start;
152
+ cursor: pointer;
153
+ }
154
+
155
+ .row:hover { background: var(--dees-color-hover); }
156
+ .row:active { background: var(--dees-color-active); }
157
+
158
+ .row:focus-visible, .back:focus-visible {
159
+ outline: var(--dees-line-width-focus-ring, 2px) solid var(--dees-color-focus-ring);
160
+ outline-offset: 2px;
161
+ }
162
+
163
+ .glyph {
164
+ display: grid;
165
+ place-items: center;
166
+ width: 30px;
167
+ height: 30px;
168
+ border-radius: var(--dees-radius-md);
169
+ background: var(--dees-color-fill-tertiary, var(--dees-color-hover));
170
+ color: var(--dees-color-text-secondary);
171
+ }
172
+
173
+ .identity { display: grid; min-width: 0; }
174
+
175
+ .name {
176
+ overflow: hidden;
177
+ font-size: var(--dees-font-control-size, 13px);
178
+ font-weight: 500;
179
+ text-overflow: ellipsis;
180
+ white-space: nowrap;
181
+ }
182
+
183
+ .meta {
184
+ overflow: hidden;
185
+ color: var(--dees-color-text-muted);
186
+ font-size: var(--dees-font-caption1-size);
187
+ text-overflow: ellipsis;
188
+ white-space: nowrap;
189
+ }
190
+
191
+ .chevron { color: var(--dees-color-label-quaternary, var(--dees-color-text-muted)); }
192
+
193
+ .empty {
194
+ display: grid;
195
+ justify-items: center;
196
+ gap: var(--dees-spacing-sm);
197
+ padding: var(--dees-spacing-xl) var(--dees-spacing-lg);
198
+ border: var(--dees-line-width-hairline) dashed var(--dees-color-border-subtle);
199
+ border-radius: var(--dees-radius-xl);
200
+ color: var(--dees-color-text-secondary);
201
+ font-size: var(--dees-font-footnote-size);
202
+ text-align: center;
203
+ }
204
+
205
+ .empty .lead { color: var(--dees-color-text-primary); font-weight: 500; }
206
+
207
+ .page { display: grid; gap: var(--dees-spacing-lg); }
208
+
209
+ .back {
210
+ justify-self: start;
211
+ display: inline-flex;
212
+ align-items: center;
213
+ gap: 2px;
214
+ padding: 0;
215
+ border: 0;
216
+ background: none;
217
+ color: var(--dees-color-accent-primary, var(--dees-color-text-link));
218
+ font: inherit;
219
+ font-size: var(--dees-font-footnote-size);
220
+ cursor: pointer;
221
+ }
222
+
223
+ .pageHead {
224
+ display: grid;
225
+ grid-template-columns: auto minmax(0, 1fr) auto;
226
+ align-items: center;
227
+ gap: var(--dees-spacing-md);
228
+ }
229
+
230
+ .pageTitle { margin: 0; font-size: 17px; font-weight: 600; }
231
+ .section { display: grid; gap: var(--dees-spacing-xs); padding: var(--dees-spacing-md) var(--dees-spacing-lg); }
232
+
233
+ .facts {
234
+ display: grid;
235
+ grid-template-columns: 150px minmax(0, 1fr);
236
+ gap: 6px 12px;
237
+ margin: 0;
238
+ font-size: var(--dees-font-caption1-size);
239
+ }
240
+
241
+ .facts dt { color: var(--dees-color-text-secondary); }
242
+ .facts dd { margin: 0; overflow-wrap: anywhere; }
243
+
244
+ .fingerprint {
245
+ font-family: var(--dees-font-family-mono, monospace);
246
+ font-size: var(--dees-font-footnote-size);
247
+ letter-spacing: .02em;
248
+ overflow-wrap: anywhere;
249
+ }
250
+
251
+ .text {
252
+ margin: 0;
253
+ color: var(--dees-color-text-secondary);
254
+ font-size: var(--dees-font-footnote-size);
255
+ line-height: 1.5;
256
+ }
257
+
258
+ dialog {
259
+ width: min(470px, calc(100vw - 32px));
260
+ padding: 0;
261
+ border: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle);
262
+ border-radius: var(--dees-radius-xl);
263
+ background: var(--dees-color-bg-elevated, var(--dees-color-bg-primary));
264
+ color: var(--dees-color-text-primary);
265
+ box-shadow: 0 28px 72px rgb(0 0 0 / 0.5);
266
+ }
267
+
268
+ dialog::backdrop { background: rgb(0 0 0 / 0.45); }
269
+ dialog:focus { outline: none; }
270
+ .dialogBody { display: grid; gap: var(--dees-spacing-md); padding: var(--dees-spacing-lg) var(--dees-spacing-xl); }
271
+ .dialogTitle { margin: 0; font-size: 15px; font-weight: 600; }
272
+
273
+ .dialogFoot {
274
+ display: flex;
275
+ justify-content: flex-end;
276
+ gap: var(--dees-spacing-sm);
277
+ padding: var(--dees-spacing-md) var(--dees-spacing-xl);
278
+ border-top: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle);
279
+ }
280
+
281
+ .qr { display: grid; justify-items: center; gap: var(--dees-spacing-sm); }
282
+
283
+ .expiry {
284
+ color: var(--dees-color-text-secondary);
285
+ font-size: var(--dees-font-footnote-size);
286
+ font-variant-numeric: tabular-nums;
287
+ }
288
+
289
+ .paired {
290
+ display: grid;
291
+ overflow: hidden;
292
+ border: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle);
293
+ border-radius: var(--dees-radius-lg);
294
+ background: var(--dees-color-bg-primary);
295
+ }
296
+
297
+ .pairedHead {
298
+ display: grid;
299
+ grid-template-columns: auto minmax(0, 1fr);
300
+ align-items: center;
301
+ gap: var(--dees-spacing-sm);
302
+ padding: var(--dees-spacing-md) var(--dees-spacing-lg);
303
+ }
304
+
305
+ .pairedHead .ok { color: var(--dees-color-accent-success); }
306
+
307
+ .fingerprintBlock {
308
+ display: grid;
309
+ gap: 4px;
310
+ padding: var(--dees-spacing-md) var(--dees-spacing-lg);
311
+ border-top: var(--dees-line-width-hairline) solid var(--dees-color-border-subtle);
312
+ background: var(--dees-color-fill-tertiary, var(--dees-color-hover));
313
+ }
314
+
315
+ .label { color: var(--dees-color-text-secondary); font-size: var(--dees-font-caption1-size); }
316
+ /* Eight groups on one line, as the device shows them; a narrow dialog breaks between groups only. */
317
+ .fingerprint.large { font-size: 14px; letter-spacing: .03em; overflow-wrap: normal; word-break: keep-all; }
318
+ .section .actions { margin-top: var(--dees-spacing-xs); }
319
+
320
+ @media (max-width: 720px) {
321
+ .facts { grid-template-columns: minmax(0, 1fr); gap: 1px; }
322
+ .facts dd { margin-bottom: 5px; }
323
+ .row .chevron { display: none; }
324
+ }
325
+ `,
326
+ ];
327
+
328
+ public async connectedCallback(): Promise<void> {
329
+ await super.connectedCallback();
330
+ this.adoptClient();
331
+ }
332
+
333
+ public async disconnectedCallback(): Promise<void> {
334
+ await super.disconnectedCallback();
335
+ // A code nobody scanned is withdrawn with the screen that showed it.
336
+ this.leavePairing();
337
+ this.stopTicker();
338
+ }
339
+
340
+ public updated(changesArg: Map<PropertyKey, unknown>): void {
341
+ super.updated(changesArg);
342
+ this.adoptClient();
343
+ const dialog = this.shadowRoot?.querySelector<HTMLDialogElement>('dialog[data-dialog]');
344
+ if (dialog && !dialog.open && dialog.isConnected) dialog.showModal();
345
+ }
346
+
347
+ /** Reads once per client, whichever lifecycle callback sees it first. */
348
+ private adoptClient(): void {
349
+ if (this.configuredClient === this.client) return;
350
+ this.configuredClient = this.client;
351
+ this.read = { state: 'pending' };
352
+ this.notice = null;
353
+ this.openId = '';
354
+ this.closeDialog();
355
+ void this.refresh();
356
+ }
357
+
358
+ /**
359
+ * Reads the devices again, as the app asks on every `devices.changed`. The change a pairing caused names its code: the
360
+ * pairing dialog that shows that code turns into the device that used it.
361
+ */
362
+ public async refresh(pairingArg?: interfaces.IControllerDevicePairingOutcome): Promise<void> {
363
+ if (pairingArg && this.pairing.state === 'waiting' && this.pairing.pairingId === pairingArg.pairingId) {
364
+ this.pairing = { state: 'paired', device: pairingArg.device };
365
+ this.stopTicker();
366
+ }
367
+ let read = this.readOnce();
368
+ this.latestRead = read;
369
+ await read;
370
+ while (this.latestRead !== read) {
371
+ read = this.latestRead;
372
+ await read;
373
+ }
374
+ }
375
+
376
+ private async readOnce(): Promise<void> {
377
+ const client = this.client;
378
+ if (!client) return;
379
+ const generation = ++this.readGeneration;
380
+ this.reading = true;
381
+ try {
382
+ const answer = await client.fire<interfaces.IReq_ControllerDevicesList>('controller.devices.list', {}, { maxRetries: 0 });
383
+ if (generation !== this.readGeneration) return;
384
+ if (!Array.isArray(answer?.devices) || !answer.devices.every(isControllerDevice)
385
+ || typeof answer.pairing?.available !== 'boolean') {
386
+ this.read = { state: 'failed', message: 'The controller returned an invalid device list.' };
387
+ return;
388
+ }
389
+ this.read = { state: 'answered', devices: answer.devices, pairing: answer.pairing };
390
+ // A page whose device is gone (revoked here or elsewhere) returns to the list.
391
+ if (this.openId && !answer.devices.some((deviceArg) => deviceArg.id === this.openId)) this.openId = '';
392
+ } catch (errorArg) {
393
+ if (generation !== this.readGeneration) return;
394
+ this.read = { state: 'failed', message: errorText(errorArg, 'The devices could not be read.') };
395
+ } finally {
396
+ if (generation === this.readGeneration) this.reading = false;
397
+ }
398
+ }
399
+
400
+ private startTicker(): void {
401
+ this.stopTicker();
402
+ this.now = Date.now();
403
+ this.ticker = setInterval(() => {
404
+ this.now = Date.now();
405
+ if (this.pairing.state === 'waiting' && this.pairing.expiresAt <= this.now) {
406
+ this.pairing = { state: 'expired' };
407
+ this.stopTicker();
408
+ }
409
+ }, 1_000);
410
+ }
411
+
412
+ private stopTicker(): void {
413
+ if (this.ticker !== undefined) clearInterval(this.ticker);
414
+ this.ticker = undefined;
415
+ }
416
+
417
+ private async beginPairing(): Promise<void> {
418
+ const client = this.client;
419
+ if (!client) return;
420
+ this.pairing = { state: 'starting' };
421
+ try {
422
+ const answer = await client.fire<interfaces.IReq_ControllerDevicesPairingBegin>(
423
+ 'controller.devices.pairing.begin', {}, { maxRetries: 0 },
424
+ );
425
+ if (this.dialog?.kind !== 'pair') {
426
+ // The dialog closed while the code was made: nobody can scan it, so it goes at once.
427
+ void this.cancelPairing(answer.pairingId);
428
+ return;
429
+ }
430
+ this.pairing = {
431
+ state: 'waiting', pairingId: answer.pairingId, payload: answer.payload, expiresAt: answer.expiresAt, copy: 'none',
432
+ };
433
+ this.startTicker();
434
+ } catch (errorArg) {
435
+ if (this.dialog?.kind !== 'pair') return;
436
+ this.pairing = { state: 'refused', message: errorText(errorArg, 'The controller made no pairing code.') };
437
+ }
438
+ }
439
+
440
+ private async cancelPairing(pairingIdArg: string): Promise<void> {
441
+ try {
442
+ await this.client?.fire<interfaces.IReq_ControllerDevicesPairingCancel>(
443
+ 'controller.devices.pairing.cancel', { pairingId: pairingIdArg }, { maxRetries: 0 },
444
+ );
445
+ } catch (errorArg) {
446
+ this.notice = {
447
+ type: 'warning',
448
+ text: `The pairing code could not be withdrawn (${errorText(errorArg, 'no answer')}); it stops working five minutes after it was made.`,
449
+ };
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Puts the waiting code's pairing link, the QR code's own payload, on the clipboard: only from the owner's press of
455
+ * "Copy pairing link", never on its own. The answer is shown only while the same code still waits.
456
+ */
457
+ private async copyPairingLink(pairingIdArg: string, payloadArg: string): Promise<void> {
458
+ const copied = await plugins.deesCatalog.harnessCopyText(payloadArg);
459
+ if (this.pairing.state !== 'waiting' || this.pairing.pairingId !== pairingIdArg) return;
460
+ this.pairing = { ...this.pairing, copy: copied ? 'copied' : 'refused' };
461
+ }
462
+
463
+ /** Withdraws the code of the pairing dialog while nobody used it. */
464
+ private leavePairing(): void {
465
+ if (this.pairing.state === 'waiting') void this.cancelPairing(this.pairing.pairingId);
466
+ this.pairing = { state: 'starting' };
467
+ this.stopTicker();
468
+ }
469
+
470
+ private openDialog(dialogArg: TDevicesDialog): void {
471
+ this.closeDialog();
472
+ this.dialog = dialogArg;
473
+ if (dialogArg.kind === 'pair') void this.beginPairing();
474
+ }
475
+
476
+ private closeDialog(): void {
477
+ const open = this.shadowRoot?.querySelector<HTMLDialogElement>('dialog[data-dialog]');
478
+ if (open?.open) open.close();
479
+ if (this.dialog?.kind === 'pair') this.leavePairing();
480
+ this.dialog = null;
481
+ }
482
+
483
+ private async revoke(deviceIdArg: string): Promise<void> {
484
+ const client = this.client;
485
+ if (!client || this.busy) return;
486
+ this.busy = 'revoke';
487
+ try {
488
+ const answer = await client.fire<interfaces.IReq_ControllerDevicesRevoke>(
489
+ 'controller.devices.revoke', { deviceId: deviceIdArg }, { maxRetries: 0 },
490
+ );
491
+ this.notice = answer.revoked
492
+ ? { type: 'success', text: 'The device was revoked. Its connections ended; it must pair again to connect.' }
493
+ : { type: 'info', text: 'The device was revoked already.' };
494
+ this.closeDialog();
495
+ if (this.openId === deviceIdArg) this.openId = '';
496
+ await this.refresh();
497
+ } catch (errorArg) {
498
+ this.notice = { type: 'error', text: errorText(errorArg, 'The device could not be revoked.') };
499
+ this.closeDialog();
500
+ } finally {
501
+ this.busy = '';
502
+ }
503
+ }
504
+
505
+ private devices(): interfaces.IControllerDevice[] {
506
+ return this.read.state === 'answered' ? this.read.devices : [];
507
+ }
508
+
509
+ private renderRow(deviceArg: interfaces.IControllerDevice): plugins.deesElement.TemplateResult {
510
+ const status = deviceStatus(deviceArg);
511
+ return plugins.deesElement.html`
512
+ <div role="listitem">
513
+ <button class="row" type="button" data-device-row=${deviceArg.id} @click=${() => { this.openId = deviceArg.id; this.notice = null; }}>
514
+ <span class="glyph"><dees-icon icon="lucide:Smartphone" iconSize="16" aria-hidden="true"></dees-icon></span>
515
+ <span class="identity">
516
+ <span class="name">${deviceArg.name}</span>
517
+ <span class="meta">${deviceArg.model} · paired ${dateTime.format(deviceArg.pairedAt)}</span>
518
+ </span>
519
+ <dees-badge data-status .type=${status.type} .text=${status.text}></dees-badge>
520
+ <dees-icon class="chevron" icon="lucide:ChevronRight" aria-hidden="true"></dees-icon>
521
+ </button>
522
+ </div>
523
+ `;
524
+ }
525
+
526
+ private renderList(): plugins.deesElement.TemplateResult {
527
+ const read = this.read;
528
+ if (read.state === 'pending') {
529
+ return plugins.deesElement.html`<p class="text" data-devices-pending>Reading the paired devices…</p>`;
530
+ }
531
+ if (read.state === 'failed') {
532
+ return plugins.deesElement.html`<dees-hint data-devices-failed .type=${'error'} .text=${read.message}></dees-hint>`;
533
+ }
534
+ if (read.devices.length === 0) {
535
+ return plugins.deesElement.html`
536
+ <div class="empty" data-devices-empty>
537
+ <dees-icon icon="lucide:Smartphone" iconSize="22" aria-hidden="true"></dees-icon>
538
+ <span class="lead">No device is paired</span>
539
+ <span>A paired phone shows the sessions of this controller, sends messages to them and answers their
540
+ permissions and questions.</span>
541
+ </div>
542
+ `;
543
+ }
544
+ return plugins.deesElement.html`
545
+ <div class="card" role="list" aria-label="Paired devices">${read.devices.map((deviceArg) => this.renderRow(deviceArg))}</div>
546
+ `;
547
+ }
548
+
549
+ private renderFacts(deviceArg: interfaces.IControllerDevice): plugins.deesElement.TemplateResult {
550
+ return plugins.deesElement.html`
551
+ <dl class="facts">
552
+ <dt>Model</dt><dd data-fact="model">${deviceArg.model}</dd>
553
+ <dt>Key fingerprint</dt><dd class="fingerprint" data-fact="fingerprint">${deviceArg.fingerprint}</dd>
554
+ <dt>Paired</dt><dd data-fact="paired">${dateTime.format(deviceArg.pairedAt)}</dd>
555
+ <dt>Last connected</dt>
556
+ <dd data-fact="lastSeen">${deviceArg.connected ? 'Connected now'
557
+ : deviceArg.lastSeenAt === undefined ? 'Never' : dateTime.format(deviceArg.lastSeenAt)}</dd>
558
+ <dt>Device id</dt><dd class="fingerprint" data-fact="id">${deviceArg.id}</dd>
559
+ </dl>
560
+ `;
561
+ }
562
+
563
+ private renderPage(deviceArg: interfaces.IControllerDevice): plugins.deesElement.TemplateResult {
564
+ const status = deviceStatus(deviceArg);
565
+ return plugins.deesElement.html`
566
+ <div class="page" data-device-page=${deviceArg.id}>
567
+ <button class="back" type="button" data-action="back" @click=${() => { this.openId = ''; }}>
568
+ <dees-icon icon="lucide:ChevronLeft" iconSize="14" aria-hidden="true"></dees-icon> Devices
569
+ </button>
570
+ <div class="pageHead">
571
+ <span class="glyph"><dees-icon icon="lucide:Smartphone" iconSize="16" aria-hidden="true"></dees-icon></span>
572
+ <h3 class="pageTitle">${deviceArg.name}</h3>
573
+ <dees-badge .type=${status.type} .text=${status.text}></dees-badge>
574
+ </div>
575
+ <div class="card">
576
+ <div class="section">${this.renderFacts(deviceArg)}</div>
577
+ <div class="section">
578
+ <p class="text">The device shows the same fingerprint under its pairing. Revoking it ends its connections at
579
+ once; it must scan a new code to connect again.</p>
580
+ <div class="actions">
581
+ <dees-button size="sm" type="destructive" text="Revoke…" icon="lucide:Ban" data-action="revoke"
582
+ .disabled=${this.busy !== ''}
583
+ @clicked=${() => this.openDialog({ kind: 'revoke', deviceId: deviceArg.id })}></dees-button>
584
+ </div>
585
+ </div>
586
+ </div>
587
+ </div>
588
+ `;
589
+ }
590
+
591
+ private dialogShell(
592
+ kindArg: TDevicesDialog['kind'],
593
+ titleArg: string,
594
+ bodyArg: unknown,
595
+ footArg: plugins.deesElement.TemplateResult,
596
+ ): plugins.deesElement.TemplateResult {
597
+ return plugins.deesElement.html`
598
+ <dialog data-dialog=${kindArg} aria-labelledby="devicesDialogTitle" autofocus
599
+ @keydown=${(eventArg: KeyboardEvent) => {
600
+ if (eventArg.key !== 'Escape' || eventArg.defaultPrevented || eventArg.isComposing) return;
601
+ // Escape closes this dialog, and only this one: the Settings dialog around the screen must not see it.
602
+ eventArg.preventDefault();
603
+ eventArg.stopPropagation();
604
+ this.closeDialog();
605
+ }}
606
+ @cancel=${(eventArg: Event) => { eventArg.preventDefault(); this.closeDialog(); }}>
607
+ <div class="dialogBody">
608
+ <h2 class="dialogTitle" id="devicesDialogTitle">${titleArg}</h2>
609
+ ${bodyArg}
610
+ </div>
611
+ ${footArg}
612
+ </dialog>
613
+ `;
614
+ }
615
+
616
+ private renderPairingBody(): unknown {
617
+ const pairing = this.pairing;
618
+ switch (pairing.state) {
619
+ case 'starting':
620
+ return plugins.deesElement.html`<p class="text" data-pairing="starting">Making a single-use pairing code…</p>`;
621
+ case 'refused':
622
+ return plugins.deesElement.html`<dees-hint data-pairing="refused" .type=${'warning'} .text=${pairing.message}></dees-hint>`;
623
+ case 'expired':
624
+ return plugins.deesElement.html`<p class="text" data-pairing="expired">The code expired before a device used it.
625
+ Make a new one to pair.</p>`;
626
+ case 'waiting':
627
+ return plugins.deesElement.html`
628
+ <div class="qr" data-pairing="waiting">
629
+ <dees-qrcode .value=${pairing.payload} .size=${240} label="Pairing code for a new device"></dees-qrcode>
630
+ <span class="expiry" data-expiry>Expires in ${countdown(pairing.expiresAt - this.now)}</span>
631
+ ${plugins.deesCatalog.harnessCanCopy()
632
+ ? plugins.deesElement.html`<dees-button size="sm" type="ghost" text="Copy pairing link" icon="lucide:Copy"
633
+ data-action="copy-link"
634
+ @clicked=${() => void this.copyPairingLink(pairing.pairingId, pairing.payload)}></dees-button>`
635
+ : ''}
636
+ </div>
637
+ ${pairing.copy === 'copied'
638
+ ? plugins.deesElement.html`<p class="text" data-copy="copied">The pairing link is on the clipboard. Paste it
639
+ into the app on the device you pair, and nowhere else.</p>`
640
+ : pairing.copy === 'refused'
641
+ ? plugins.deesElement.html`<dees-hint data-copy="refused" .type=${'warning'}
642
+ .text=${'The clipboard refused the pairing link. Scan the QR code instead.'}></dees-hint>`
643
+ : ''}
644
+ <p class="text">Scan the code with the app on the device you pair, or copy the pairing link and paste it into
645
+ the app there. The code works once; anyone who has it can pair until it expires, so give it to that device
646
+ only. Once the device pairs, compare the key fingerprint it shows with the one shown here.</p>
647
+ `;
648
+ case 'paired':
649
+ return plugins.deesElement.html`
650
+ <div class="paired" data-pairing="paired">
651
+ <div class="pairedHead">
652
+ <dees-icon class="ok" icon="lucide:CircleCheck" iconSize="20" aria-hidden="true"></dees-icon>
653
+ <span class="identity">
654
+ <span class="name">${pairing.device.name}</span>
655
+ <span class="meta">${pairing.device.model}</span>
656
+ </span>
657
+ </div>
658
+ <div class="fingerprintBlock">
659
+ <span class="label">Key fingerprint</span>
660
+ <span class="fingerprint large" data-paired-fingerprint>${pairing.device.fingerprint}</span>
661
+ </div>
662
+ </div>
663
+ <p class="text">Compare the fingerprint with the one the device shows under its pairing. If they differ, revoke
664
+ the device now.</p>
665
+ `;
666
+ }
667
+ }
668
+
669
+ private renderPairingFoot(): plugins.deesElement.TemplateResult {
670
+ const pairing = this.pairing;
671
+ return plugins.deesElement.html`
672
+ <div class="dialogFoot">
673
+ ${pairing.state === 'expired' || pairing.state === 'refused'
674
+ ? plugins.deesElement.html`<dees-button size="sm" type="accent" text="New code" icon="lucide:RefreshCw"
675
+ data-action="new-code" @clicked=${() => void this.beginPairing()}></dees-button>`
676
+ : ''}
677
+ ${pairing.state === 'paired'
678
+ ? plugins.deesElement.html`<dees-button size="sm" type="destructive" text="Revoke" icon="lucide:Ban"
679
+ data-action="revoke-paired" .disabled=${this.busy !== ''}
680
+ @clicked=${() => void this.revoke(pairing.device.id)}></dees-button>`
681
+ : ''}
682
+ <dees-button size="sm" .type=${pairing.state === 'paired' ? 'accent' : 'ghost'}
683
+ .text=${pairing.state === 'paired' ? 'Done' : 'Cancel'} data-action="close"
684
+ @clicked=${() => this.closeDialog()}></dees-button>
685
+ </div>
686
+ `;
687
+ }
688
+
689
+ private renderDialog(): unknown {
690
+ const dialog = this.dialog;
691
+ if (!dialog) return '';
692
+ if (dialog.kind === 'pair') {
693
+ return this.dialogShell(
694
+ 'pair',
695
+ this.pairing.state === 'paired' ? `Paired ${this.pairing.device.name}` : 'Pair a device',
696
+ this.renderPairingBody(),
697
+ this.renderPairingFoot(),
698
+ );
699
+ }
700
+ const device = this.devices().find((deviceArg) => deviceArg.id === dialog.deviceId);
701
+ if (!device) return '';
702
+ return this.dialogShell(
703
+ 'revoke',
704
+ `Revoke ${device.name}?`,
705
+ plugins.deesElement.html`<p class="text">${device.name} (${device.model}) is disconnected at once and can no longer
706
+ reach this controller. To use it again, pair it with a new code.</p>`,
707
+ plugins.deesElement.html`
708
+ <div class="dialogFoot">
709
+ <dees-button size="sm" type="ghost" text="Cancel" data-action="close" @clicked=${() => this.closeDialog()}></dees-button>
710
+ <dees-button size="sm" type="destructive" text="Revoke" icon="lucide:Ban" data-action="confirm-revoke"
711
+ .disabled=${this.busy !== ''} @clicked=${() => void this.revoke(device.id)}></dees-button>
712
+ </div>
713
+ `,
714
+ );
715
+ }
716
+
717
+ public render(): plugins.deesElement.TemplateResult {
718
+ const read = this.read;
719
+ const devices = this.devices();
720
+ const page = this.openId ? devices.find((deviceArg) => deviceArg.id === this.openId) : undefined;
721
+ const blocked = read.state === 'answered' ? pairingBlockedReason(read.pairing) : 'The devices are not read yet.';
722
+ return plugins.deesElement.html`
723
+ <section class="screen" aria-label="Devices">
724
+ <div class="bar">
725
+ <span class="count" data-device-count>${read.state === 'answered'
726
+ ? `${devices.length} of ${maximumDevices} devices paired` : ''}</span>
727
+ <div class="tools">
728
+ <dees-button size="sm" type="ghost" icon="lucide:RefreshCw" data-action="refresh" title="Read the devices again"
729
+ aria-label="Read the devices again" .disabled=${this.reading} @clicked=${() => void this.refresh()}></dees-button>
730
+ <dees-button size="sm" type="ghost" text="Pair a device" icon="lucide:QrCode" data-action="pair"
731
+ .disabled=${blocked !== ''} title=${blocked || 'Pair a device'}
732
+ @clicked=${() => { if (!blocked) this.openDialog({ kind: 'pair' }); }}></dees-button>
733
+ </div>
734
+ </div>
735
+ ${read.state === 'answered' && !read.pairing.available && read.pairing.reason === 'publicOriginRequired'
736
+ ? plugins.deesElement.html`<dees-hint data-pairing-unavailable .type=${'info'} .text=${blocked}></dees-hint>`
737
+ : ''}
738
+ ${this.notice
739
+ ? plugins.deesElement.html`<dees-hint data-notice .type=${this.notice.type} .text=${this.notice.text}></dees-hint>`
740
+ : ''}
741
+ ${page ? this.renderPage(page) : this.renderList()}
742
+ ${this.renderDialog()}
743
+ </section>
744
+ `;
745
+ }
746
+ }
747
+
748
+ declare global {
749
+ interface HTMLElementTagNameMap {
750
+ 'agl-devices': AglDevices;
751
+ }
752
+ }