@jetpack-desktop/client 0.0.0-stage → 0.1.2-beta.2

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.
@@ -0,0 +1,788 @@
1
+ const DEFAULT_CONTROL_CENTER_URL = 'http://127.0.0.1:9470/#apps';
2
+ const DEFAULT_CONFIRM_TITLE = 'Stop Worker Process?';
3
+ const DEFAULT_CONFIRM_MESSAGE = 'Stopping this worker may discard unsaved work or active tasks. Do you want to continue?';
4
+ const CONTROL_BAR_STYLES = `
5
+ :host {
6
+ all: initial;
7
+ display: block;
8
+ position: sticky;
9
+ top: 0;
10
+ z-index: 2147483647;
11
+ width: 100%;
12
+ font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
13
+ color: #f0f6fc;
14
+ -webkit-font-smoothing: antialiased;
15
+ }
16
+
17
+ *, *::before, *::after {
18
+ box-sizing: border-box;
19
+ }
20
+
21
+ .jp-bar {
22
+ display: flex;
23
+ align-items: center;
24
+ justify-content: space-between;
25
+ gap: 12px;
26
+ min-height: 48px;
27
+ padding: 8px 18px;
28
+ background: rgba(19, 25, 36, 0.96);
29
+ border-bottom: 1px solid #2a3447;
30
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
31
+ backdrop-filter: blur(12px);
32
+ }
33
+
34
+ .jp-left {
35
+ display: flex;
36
+ align-items: center;
37
+ gap: 10px;
38
+ min-width: 0;
39
+ }
40
+
41
+ .jp-brand {
42
+ display: inline-flex;
43
+ align-items: center;
44
+ gap: 7px;
45
+ font-size: 13px;
46
+ font-weight: 700;
47
+ letter-spacing: 0.01em;
48
+ color: #f0f6fc;
49
+ flex-shrink: 0;
50
+ }
51
+
52
+ .jp-logo-svg,
53
+ .jp-logo-img {
54
+ width: 22px;
55
+ height: 22px;
56
+ border-radius: 6px;
57
+ display: block;
58
+ flex-shrink: 0;
59
+ }
60
+
61
+ .jp-divider {
62
+ width: 1px;
63
+ height: 18px;
64
+ background: #2a3447;
65
+ flex-shrink: 0;
66
+ }
67
+
68
+ .jp-app-name {
69
+ font-size: 13px;
70
+ font-weight: 600;
71
+ color: #e6edf3;
72
+ white-space: nowrap;
73
+ overflow: hidden;
74
+ text-overflow: ellipsis;
75
+ }
76
+
77
+ .jp-right {
78
+ display: flex;
79
+ align-items: center;
80
+ gap: 10px;
81
+ flex-shrink: 0;
82
+ }
83
+
84
+ .jp-status-pill {
85
+ display: inline-flex;
86
+ align-items: center;
87
+ gap: 6px;
88
+ padding: 4px 10px;
89
+ border-radius: 999px;
90
+ font-size: 12px;
91
+ font-weight: 600;
92
+ border: 1px solid #30363d;
93
+ background: rgba(255, 255, 255, 0.04);
94
+ color: #c9d1d9;
95
+ }
96
+
97
+ .jp-status-dot {
98
+ width: 8px;
99
+ height: 8px;
100
+ border-radius: 50%;
101
+ background: #8b949e;
102
+ }
103
+
104
+ .jp-status-pill[data-status="Connecting"] {
105
+ border-color: rgba(88, 166, 255, 0.45);
106
+ background: rgba(88, 166, 255, 0.14);
107
+ color: #79c0ff;
108
+ }
109
+
110
+ .jp-status-pill[data-status="Connecting"] .jp-status-dot {
111
+ background: #58a6ff;
112
+ }
113
+
114
+ .jp-status-pill[data-status="Running"] {
115
+ border-color: rgba(63, 185, 80, 0.45);
116
+ background: rgba(63, 185, 80, 0.14);
117
+ color: #3fb950;
118
+ }
119
+
120
+ .jp-status-pill[data-status="Running"] .jp-status-dot {
121
+ background: #3fb950;
122
+ }
123
+
124
+ .jp-status-pill[data-status="Stopped"] {
125
+ border-color: rgba(139, 148, 158, 0.35);
126
+ background: rgba(139, 148, 158, 0.12);
127
+ color: #9ca3af;
128
+ }
129
+
130
+ .jp-status-pill[data-status="Stopped"] .jp-status-dot {
131
+ background: #8b949e;
132
+ }
133
+
134
+ .jp-status-pill[data-status="Error"] {
135
+ border-color: rgba(248, 81, 73, 0.5);
136
+ background: rgba(248, 81, 73, 0.14);
137
+ color: #ff7b72;
138
+ }
139
+
140
+ .jp-status-pill[data-status="Error"] .jp-status-dot {
141
+ background: #f85149;
142
+ }
143
+
144
+ .jp-error-note {
145
+ font-size: 12px;
146
+ color: #ff7b72;
147
+ max-width: 240px;
148
+ white-space: nowrap;
149
+ overflow: hidden;
150
+ text-overflow: ellipsis;
151
+ }
152
+
153
+ .jp-status-note {
154
+ font-size: 12px;
155
+ color: #9fb0c7;
156
+ max-width: 240px;
157
+ white-space: nowrap;
158
+ overflow: hidden;
159
+ text-overflow: ellipsis;
160
+ }
161
+
162
+ .jp-link-btn,
163
+ .jp-relaunch-btn,
164
+ .jp-stop-btn,
165
+ .jp-modal-btn {
166
+ appearance: none;
167
+ font: inherit;
168
+ font-size: 12px;
169
+ font-weight: 600;
170
+ line-height: 1.2;
171
+ border-radius: 7px;
172
+ padding: 6px 12px;
173
+ cursor: pointer;
174
+ text-decoration: none;
175
+ display: inline-flex;
176
+ align-items: center;
177
+ justify-content: center;
178
+ transition: background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
179
+ }
180
+
181
+ .jp-link-btn {
182
+ color: #c9d1d9;
183
+ background: #1b2434;
184
+ border: 1px solid #2e3b52;
185
+ }
186
+
187
+ .jp-link-btn:hover {
188
+ background: #232f44;
189
+ border-color: #445675;
190
+ color: #ffffff;
191
+ }
192
+
193
+ .jp-download-btn,
194
+ .jp-relaunch-btn {
195
+ color: #ffffff;
196
+ background: #1f6feb;
197
+ border: 1px solid #388bfd;
198
+ }
199
+
200
+ .jp-download-btn:hover,
201
+ .jp-relaunch-btn:hover:not(:disabled) {
202
+ background: #388bfd;
203
+ border-color: #58a6ff;
204
+ color: #ffffff;
205
+ }
206
+
207
+ .jp-relaunch-btn:disabled {
208
+ opacity: 0.55;
209
+ cursor: not-allowed;
210
+ }
211
+
212
+ .jp-stop-btn {
213
+ color: #ffdcd9;
214
+ background: rgba(248, 81, 73, 0.18);
215
+ border: 1px solid rgba(248, 81, 73, 0.5);
216
+ }
217
+
218
+ .jp-stop-btn:hover:not(:disabled) {
219
+ background: rgba(248, 81, 73, 0.3);
220
+ border-color: #f85149;
221
+ color: #ffffff;
222
+ }
223
+
224
+ .jp-stop-btn:disabled {
225
+ opacity: 0.45;
226
+ cursor: not-allowed;
227
+ }
228
+
229
+ .jp-modal-backdrop {
230
+ position: fixed;
231
+ inset: 0;
232
+ z-index: 2147483647;
233
+ display: flex;
234
+ align-items: center;
235
+ justify-content: center;
236
+ padding: 16px 16px calc(16px + var(--jetpack-log-terminal-offset, 0px));
237
+ background: rgba(7, 10, 16, 0.72);
238
+ backdrop-filter: blur(6px);
239
+ }
240
+
241
+ .jp-modal-backdrop[hidden] {
242
+ display: none;
243
+ }
244
+
245
+ .jp-modal {
246
+ width: 100%;
247
+ max-width: 420px;
248
+ background: #131924;
249
+ border: 1px solid #2e3b52;
250
+ border-radius: 12px;
251
+ padding: 20px;
252
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55);
253
+ }
254
+
255
+ .jp-modal-title {
256
+ margin: 0 0 8px 0;
257
+ font-size: 16px;
258
+ font-weight: 700;
259
+ color: #f0f6fc;
260
+ }
261
+
262
+ .jp-modal-message {
263
+ margin: 0 0 18px 0;
264
+ font-size: 13px;
265
+ line-height: 1.5;
266
+ color: #9fb0c7;
267
+ }
268
+
269
+ .jp-modal-actions {
270
+ display: flex;
271
+ justify-content: flex-end;
272
+ gap: 8px;
273
+ }
274
+
275
+ .jp-modal-btn-cancel {
276
+ color: #c9d1d9;
277
+ background: #1b2434;
278
+ border: 1px solid #2e3b52;
279
+ }
280
+
281
+ .jp-modal-btn-cancel:hover {
282
+ background: #232f44;
283
+ color: #ffffff;
284
+ }
285
+
286
+ .jp-modal-btn-confirm {
287
+ color: #ffffff;
288
+ background: #da3633;
289
+ border: 1px solid #f85149;
290
+ }
291
+
292
+ .jp-modal-btn-confirm:hover {
293
+ background: #f85149;
294
+ }
295
+
296
+ @media (max-width: 640px) {
297
+ .jp-bar {
298
+ flex-wrap: wrap;
299
+ padding: 8px 12px;
300
+ }
301
+ .jp-error-note {
302
+ display: none;
303
+ }
304
+ }
305
+ `;
306
+ const JETPACK_LOGO_SVG = '<svg class="jp-logo-svg" viewBox="0 0 24 24" fill="none" aria-hidden="true">' +
307
+ '<rect width="24" height="24" rx="6" fill="#1F6FEB"/>' +
308
+ '<path d="M12 4.5L16.5 11H13.5V16.5L9.5 12H11.5V4.5H12Z" fill="#FFFFFF"/>' +
309
+ '<path d="M8.5 16.5L12 20L15.5 16.5H8.5Z" fill="#79C0FF"/>' +
310
+ '</svg>';
311
+ function sanitizeHttpsDownloadUrl(rawUrl) {
312
+ if (typeof rawUrl !== 'string')
313
+ return null;
314
+ const trimmed = rawUrl.trim();
315
+ if (!trimmed || /[\s\u0000-\u001f\u007f]/.test(trimmed)) {
316
+ return null;
317
+ }
318
+ try {
319
+ const parsed = new URL(trimmed);
320
+ if (parsed.protocol !== 'https:' || parsed.username || parsed.password) {
321
+ return null;
322
+ }
323
+ return parsed.toString();
324
+ }
325
+ catch {
326
+ return null;
327
+ }
328
+ }
329
+ /**
330
+ * Shadow-DOM isolated top control bar for standalone and third-party JetPack web applications.
331
+ *
332
+ * Displays the JetPack logo, current app name, live worker status (`Connecting`, `Running`,
333
+ * `Stopped`, `Error`), `"Control Center"` link, `"Stop"` button, and an encapsulated confirmation
334
+ * modal when stopping could lose unsaved work.
335
+ */
336
+ export class JetPackControlBar {
337
+ options;
338
+ status;
339
+ currentDownloadUrl = null;
340
+ stopping = false;
341
+ relaunching = false;
342
+ modalOpen = false;
343
+ host = null;
344
+ shadow = null;
345
+ appNameEl = null;
346
+ statusPillEl = null;
347
+ statusTextEl = null;
348
+ errorNoteEl = null;
349
+ statusNoteEl = null;
350
+ downloadLinkEl = null;
351
+ controlCenterLinkEl = null;
352
+ relaunchBtnEl = null;
353
+ stopBtnEl = null;
354
+ modalBackdropEl = null;
355
+ modalTitleEl = null;
356
+ modalMessageEl = null;
357
+ modalCancelBtnEl = null;
358
+ modalConfirmBtnEl = null;
359
+ keydownHandler = null;
360
+ constructor(options = {}) {
361
+ this.options = { ...options };
362
+ this.status = options.initialStatus ?? 'Connecting';
363
+ this.currentDownloadUrl =
364
+ this.status === 'Error' ? sanitizeHttpsDownloadUrl(options.downloadUrl) : null;
365
+ if (typeof document !== 'undefined' && options.mountTarget !== null) {
366
+ this.mount(options.mountTarget);
367
+ }
368
+ }
369
+ /** Mounts the control bar host element at the top of `target` (or `document.body`). */
370
+ mount(target) {
371
+ if (typeof document === 'undefined') {
372
+ return false;
373
+ }
374
+ const container = target ?? this.options.mountTarget ?? document.body;
375
+ if (!container) {
376
+ return false;
377
+ }
378
+ if (!this.host) {
379
+ this.buildDom();
380
+ }
381
+ if (!this.host) {
382
+ return false;
383
+ }
384
+ if (this.host.parentNode !== container) {
385
+ if (container.firstChild) {
386
+ container.insertBefore(this.host, container.firstChild);
387
+ }
388
+ else {
389
+ container.appendChild(this.host);
390
+ }
391
+ }
392
+ this.syncUi();
393
+ return true;
394
+ }
395
+ /** Unmounts and cleans up the control bar host and event listeners. */
396
+ unmount() {
397
+ this.closeModal();
398
+ if (this.keydownHandler && typeof document !== 'undefined') {
399
+ document.removeEventListener('keydown', this.keydownHandler);
400
+ this.keydownHandler = null;
401
+ }
402
+ if (this.host && this.host.parentNode) {
403
+ this.host.parentNode.removeChild(this.host);
404
+ }
405
+ this.host = null;
406
+ this.shadow = null;
407
+ }
408
+ /** Updates mutable configuration options on the mounted bar. */
409
+ updateOptions(partial) {
410
+ this.options = { ...this.options, ...partial };
411
+ if (this.status === 'Error' && 'downloadUrl' in partial) {
412
+ this.currentDownloadUrl = sanitizeHttpsDownloadUrl(partial.downloadUrl);
413
+ }
414
+ this.syncUi();
415
+ }
416
+ /** Updates the live worker status pill, optional detail text, and optional HTTPS download link. */
417
+ setStatus(status, detail, statusOptions) {
418
+ this.status = status;
419
+ this.currentDownloadUrl =
420
+ status === 'Error'
421
+ ? sanitizeHttpsDownloadUrl(statusOptions?.downloadUrl ?? this.options.downloadUrl)
422
+ : null;
423
+ if (this.errorNoteEl) {
424
+ const msg = status === 'Error' && detail ? detail.trim() : '';
425
+ this.errorNoteEl.textContent = msg;
426
+ this.errorNoteEl.hidden = msg.length === 0;
427
+ }
428
+ if (this.statusNoteEl) {
429
+ const note = status === 'Stopped' && detail ? detail.trim() : '';
430
+ this.statusNoteEl.textContent = note;
431
+ this.statusNoteEl.hidden = note.length === 0;
432
+ }
433
+ if (status !== 'Running') {
434
+ this.closeModal();
435
+ }
436
+ this.syncUi();
437
+ }
438
+ /** Returns the current worker status rendered in the bar. */
439
+ getStatus() {
440
+ return this.status;
441
+ }
442
+ /** Updates the displayed application name in the control bar. */
443
+ setAppName(appName) {
444
+ this.options.appName = appName;
445
+ this.syncUi();
446
+ }
447
+ /** Updates the `"Control Center"` link URL. */
448
+ setControlCenterUrl(url) {
449
+ this.options.controlCenterUrl = url;
450
+ this.syncUi();
451
+ }
452
+ /** Returns the host element mounted into the page, or `null` if unmounted. */
453
+ getHostElement() {
454
+ return this.host;
455
+ }
456
+ /** Returns the open ShadowRoot isolating the control bar styles and DOM. */
457
+ getShadowRoot() {
458
+ return this.shadow;
459
+ }
460
+ /** Returns `true` when the Shadow-DOM stop confirmation modal is open. */
461
+ isModalOpen() {
462
+ return this.modalOpen;
463
+ }
464
+ /**
465
+ * Handles a Relaunch button click while `status === 'Stopped'`.
466
+ * Invokes `options.onRelaunch()` and transitions to `'Running'` on success.
467
+ */
468
+ async triggerRelaunch() {
469
+ if (this.status !== 'Stopped' ||
470
+ this.relaunching ||
471
+ typeof this.options.onRelaunch !== 'function') {
472
+ return false;
473
+ }
474
+ this.relaunching = true;
475
+ this.syncUi();
476
+ try {
477
+ await this.options.onRelaunch();
478
+ this.relaunching = false;
479
+ if (this.status === 'Stopped') {
480
+ this.setStatus('Running');
481
+ }
482
+ else {
483
+ this.syncUi();
484
+ }
485
+ return true;
486
+ }
487
+ catch (err) {
488
+ this.relaunching = false;
489
+ const error = err instanceof Error ? err : new Error(String(err));
490
+ this.setStatus('Error', error.message);
491
+ return false;
492
+ }
493
+ }
494
+ /**
495
+ * Handles a Stop button click: opens the styled Shadow-DOM confirmation modal if
496
+ * `warnOnUnsavedWork` evaluates to `true` or `onBeforeStop` is configured (with
497
+ * `warnOnUnsavedWork` unspecified), otherwise executes the stop flow directly.
498
+ */
499
+ async triggerStop() {
500
+ if (this.status !== 'Running' || this.stopping) {
501
+ return false;
502
+ }
503
+ if (this.shouldShowStopConfirmation()) {
504
+ this.openModal();
505
+ return false;
506
+ }
507
+ return this.executeStop();
508
+ }
509
+ /** Confirms the open stop dialog and proceeds to `onBeforeStop` + worker termination. */
510
+ async confirmStopModal() {
511
+ this.closeModal();
512
+ return this.executeStop();
513
+ }
514
+ /** Cancels and closes the open stop dialog without terminating the worker. */
515
+ cancelStopModal() {
516
+ this.closeModal();
517
+ }
518
+ shouldShowStopConfirmation() {
519
+ const warnSetting = this.options.warnOnUnsavedWork;
520
+ if (typeof warnSetting === 'function') {
521
+ return Boolean(warnSetting());
522
+ }
523
+ if (typeof warnSetting === 'boolean') {
524
+ return warnSetting;
525
+ }
526
+ return typeof this.options.onBeforeStop === 'function';
527
+ }
528
+ openModal() {
529
+ this.modalOpen = true;
530
+ if (this.modalTitleEl) {
531
+ this.modalTitleEl.textContent = this.options.stopConfirmTitle ?? DEFAULT_CONFIRM_TITLE;
532
+ }
533
+ if (this.modalMessageEl) {
534
+ this.modalMessageEl.textContent =
535
+ this.options.stopConfirmMessage ?? DEFAULT_CONFIRM_MESSAGE;
536
+ }
537
+ if (this.modalBackdropEl) {
538
+ this.modalBackdropEl.hidden = false;
539
+ this.modalBackdropEl.setAttribute('data-open', 'true');
540
+ }
541
+ }
542
+ closeModal() {
543
+ this.modalOpen = false;
544
+ if (this.modalBackdropEl) {
545
+ this.modalBackdropEl.hidden = true;
546
+ this.modalBackdropEl.removeAttribute('data-open');
547
+ }
548
+ }
549
+ async executeStop() {
550
+ if (this.stopping) {
551
+ return false;
552
+ }
553
+ this.stopping = true;
554
+ this.syncUi();
555
+ try {
556
+ if (this.options.onBeforeStop) {
557
+ const allow = await this.options.onBeforeStop();
558
+ if (allow === false) {
559
+ this.stopping = false;
560
+ this.syncUi();
561
+ return false;
562
+ }
563
+ }
564
+ if (this.options.onStop) {
565
+ await this.options.onStop();
566
+ }
567
+ this.stopping = false;
568
+ this.setStatus('Stopped');
569
+ await this.options.onAfterStop?.();
570
+ return true;
571
+ }
572
+ catch (err) {
573
+ this.stopping = false;
574
+ const error = err instanceof Error ? err : new Error(String(err));
575
+ this.setStatus('Error', error.message);
576
+ this.options.onStopError?.(error);
577
+ return false;
578
+ }
579
+ }
580
+ buildDom() {
581
+ const host = document.createElement('div');
582
+ host.id = 'jetpack-control-bar-host';
583
+ host.setAttribute('data-jetpack-control-bar', 'true');
584
+ const shadow = host.attachShadow({ mode: 'open' });
585
+ const style = document.createElement('style');
586
+ style.textContent = CONTROL_BAR_STYLES;
587
+ shadow.appendChild(style);
588
+ const header = document.createElement('header');
589
+ header.className = 'jp-bar';
590
+ header.setAttribute('part', 'bar');
591
+ // Left cluster: Brand logo + divider + app name
592
+ const left = document.createElement('div');
593
+ left.className = 'jp-left';
594
+ const brand = document.createElement('span');
595
+ brand.className = 'jp-brand';
596
+ if (this.options.logoUrl) {
597
+ const img = document.createElement('img');
598
+ img.className = 'jp-logo-img';
599
+ img.src = this.options.logoUrl;
600
+ img.alt = 'JetPack';
601
+ brand.appendChild(img);
602
+ }
603
+ else {
604
+ const logoWrap = document.createElement('span');
605
+ logoWrap.innerHTML = JETPACK_LOGO_SVG;
606
+ brand.appendChild(logoWrap);
607
+ }
608
+ const brandLabel = document.createElement('span');
609
+ brandLabel.textContent = 'JetPack';
610
+ brand.appendChild(brandLabel);
611
+ const divider = document.createElement('span');
612
+ divider.className = 'jp-divider';
613
+ divider.setAttribute('aria-hidden', 'true');
614
+ const appNameEl = document.createElement('span');
615
+ appNameEl.className = 'jp-app-name';
616
+ appNameEl.setAttribute('data-jp-field', 'app-name');
617
+ left.appendChild(brand);
618
+ left.appendChild(divider);
619
+ left.appendChild(appNameEl);
620
+ // Right cluster: optional error/status note + live status pill + Control Center link + Relaunch + Stop
621
+ const right = document.createElement('div');
622
+ right.className = 'jp-right';
623
+ const errorNoteEl = document.createElement('span');
624
+ errorNoteEl.className = 'jp-error-note';
625
+ errorNoteEl.setAttribute('data-jp-field', 'error-note');
626
+ errorNoteEl.hidden = true;
627
+ const statusNoteEl = document.createElement('span');
628
+ statusNoteEl.className = 'jp-status-note';
629
+ statusNoteEl.setAttribute('data-jp-field', 'status-note');
630
+ statusNoteEl.hidden = true;
631
+ const statusPillEl = document.createElement('span');
632
+ statusPillEl.className = 'jp-status-pill';
633
+ statusPillEl.setAttribute('data-jp-field', 'status-pill');
634
+ const statusDotEl = document.createElement('span');
635
+ statusDotEl.className = 'jp-status-dot';
636
+ statusDotEl.setAttribute('aria-hidden', 'true');
637
+ const statusTextEl = document.createElement('span');
638
+ statusTextEl.setAttribute('data-jp-field', 'status-text');
639
+ statusPillEl.appendChild(statusDotEl);
640
+ statusPillEl.appendChild(statusTextEl);
641
+ const downloadLink = document.createElement('a');
642
+ downloadLink.className = 'jp-link-btn jp-download-btn';
643
+ downloadLink.setAttribute('data-jp-action', 'download-jetpack');
644
+ downloadLink.target = '_blank';
645
+ downloadLink.rel = 'noopener noreferrer';
646
+ downloadLink.textContent = 'Download JetPack';
647
+ downloadLink.hidden = true;
648
+ const ccLink = document.createElement('a');
649
+ ccLink.className = 'jp-link-btn';
650
+ ccLink.setAttribute('data-jp-action', 'control-center');
651
+ ccLink.target = '_blank';
652
+ ccLink.rel = 'noopener noreferrer';
653
+ ccLink.textContent = 'Control Center';
654
+ const relaunchBtn = document.createElement('button');
655
+ relaunchBtn.type = 'button';
656
+ relaunchBtn.className = 'jp-relaunch-btn';
657
+ relaunchBtn.setAttribute('data-jp-action', 'relaunch');
658
+ relaunchBtn.textContent = 'Relaunch';
659
+ relaunchBtn.hidden = true;
660
+ relaunchBtn.addEventListener('click', () => {
661
+ void this.triggerRelaunch();
662
+ });
663
+ const stopBtn = document.createElement('button');
664
+ stopBtn.type = 'button';
665
+ stopBtn.className = 'jp-stop-btn';
666
+ stopBtn.setAttribute('data-jp-action', 'stop');
667
+ stopBtn.textContent = 'Stop';
668
+ stopBtn.addEventListener('click', () => {
669
+ void this.triggerStop();
670
+ });
671
+ right.appendChild(errorNoteEl);
672
+ right.appendChild(statusNoteEl);
673
+ right.appendChild(downloadLink);
674
+ right.appendChild(statusPillEl);
675
+ right.appendChild(ccLink);
676
+ right.appendChild(relaunchBtn);
677
+ right.appendChild(stopBtn);
678
+ header.appendChild(left);
679
+ header.appendChild(right);
680
+ shadow.appendChild(header);
681
+ // Styled Shadow-DOM confirmation modal
682
+ const backdrop = document.createElement('div');
683
+ backdrop.className = 'jp-modal-backdrop';
684
+ backdrop.setAttribute('data-jp-field', 'stop-modal');
685
+ backdrop.setAttribute('role', 'dialog');
686
+ backdrop.setAttribute('aria-modal', 'true');
687
+ backdrop.hidden = true;
688
+ const modal = document.createElement('div');
689
+ modal.className = 'jp-modal';
690
+ const modalTitle = document.createElement('h2');
691
+ modalTitle.className = 'jp-modal-title';
692
+ modalTitle.setAttribute('data-jp-field', 'modal-title');
693
+ const modalMessage = document.createElement('p');
694
+ modalMessage.className = 'jp-modal-message';
695
+ modalMessage.setAttribute('data-jp-field', 'modal-message');
696
+ const modalActions = document.createElement('div');
697
+ modalActions.className = 'jp-modal-actions';
698
+ const cancelBtn = document.createElement('button');
699
+ cancelBtn.type = 'button';
700
+ cancelBtn.className = 'jp-modal-btn jp-modal-btn-cancel';
701
+ cancelBtn.setAttribute('data-jp-action', 'cancel');
702
+ cancelBtn.textContent = 'Cancel';
703
+ cancelBtn.addEventListener('click', () => {
704
+ this.cancelStopModal();
705
+ });
706
+ const confirmBtn = document.createElement('button');
707
+ confirmBtn.type = 'button';
708
+ confirmBtn.className = 'jp-modal-btn jp-modal-btn-confirm';
709
+ confirmBtn.setAttribute('data-jp-action', 'confirm');
710
+ confirmBtn.textContent = 'Stop Worker';
711
+ confirmBtn.addEventListener('click', () => {
712
+ void this.confirmStopModal();
713
+ });
714
+ modalActions.appendChild(cancelBtn);
715
+ modalActions.appendChild(confirmBtn);
716
+ modal.appendChild(modalTitle);
717
+ modal.appendChild(modalMessage);
718
+ modal.appendChild(modalActions);
719
+ backdrop.appendChild(modal);
720
+ backdrop.addEventListener('click', (ev) => {
721
+ if (ev.target === backdrop) {
722
+ this.cancelStopModal();
723
+ }
724
+ });
725
+ shadow.appendChild(backdrop);
726
+ if (typeof document.addEventListener === 'function') {
727
+ this.keydownHandler = (ev) => {
728
+ if (ev.key === 'Escape' && this.modalOpen) {
729
+ this.cancelStopModal();
730
+ }
731
+ };
732
+ document.addEventListener('keydown', this.keydownHandler);
733
+ }
734
+ this.host = host;
735
+ this.shadow = shadow;
736
+ this.appNameEl = appNameEl;
737
+ this.statusPillEl = statusPillEl;
738
+ this.statusTextEl = statusTextEl;
739
+ this.errorNoteEl = errorNoteEl;
740
+ this.statusNoteEl = statusNoteEl;
741
+ this.downloadLinkEl = downloadLink;
742
+ this.controlCenterLinkEl = ccLink;
743
+ this.relaunchBtnEl = relaunchBtn;
744
+ this.stopBtnEl = stopBtn;
745
+ this.modalBackdropEl = backdrop;
746
+ this.modalTitleEl = modalTitle;
747
+ this.modalMessageEl = modalMessage;
748
+ this.modalCancelBtnEl = cancelBtn;
749
+ this.modalConfirmBtnEl = confirmBtn;
750
+ }
751
+ syncUi() {
752
+ const resolvedName = this.options.appName?.trim() || this.options.appId?.trim() || 'JetPack App';
753
+ if (this.appNameEl) {
754
+ this.appNameEl.textContent = resolvedName;
755
+ }
756
+ if (this.statusPillEl) {
757
+ this.statusPillEl.setAttribute('data-status', this.status);
758
+ }
759
+ if (this.statusTextEl) {
760
+ this.statusTextEl.textContent = this.status;
761
+ }
762
+ if (this.downloadLinkEl) {
763
+ const showDownload = this.status === 'Error' && this.currentDownloadUrl !== null;
764
+ this.downloadLinkEl.hidden = !showDownload;
765
+ this.downloadLinkEl.href = showDownload ? this.currentDownloadUrl : '';
766
+ }
767
+ if (this.controlCenterLinkEl) {
768
+ this.controlCenterLinkEl.href = this.options.controlCenterUrl ?? DEFAULT_CONTROL_CENTER_URL;
769
+ }
770
+ if (this.relaunchBtnEl) {
771
+ const showRelaunch = this.status === 'Stopped' && typeof this.options.onRelaunch === 'function';
772
+ this.relaunchBtnEl.hidden = !showRelaunch;
773
+ this.relaunchBtnEl.disabled = !showRelaunch || this.relaunching;
774
+ this.relaunchBtnEl.textContent = this.relaunching ? 'Relaunching...' : 'Relaunch';
775
+ }
776
+ if (this.stopBtnEl) {
777
+ this.stopBtnEl.disabled = this.status !== 'Running' || this.stopping;
778
+ this.stopBtnEl.textContent = this.stopping ? 'Stopping...' : 'Stop';
779
+ }
780
+ if (this.modalCancelBtnEl) {
781
+ this.modalCancelBtnEl.disabled = this.stopping;
782
+ }
783
+ if (this.modalConfirmBtnEl) {
784
+ this.modalConfirmBtnEl.disabled = this.stopping;
785
+ }
786
+ }
787
+ }
788
+ //# sourceMappingURL=control-bar.js.map