tailcat-webui 0.0.0-stage → 0.2.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/LICENSE +21 -0
  2. package/NOTICE +29 -0
  3. package/README.en.md +112 -0
  4. package/README.md +101 -2
  5. package/bin/tailcat-webui.js +12 -0
  6. package/docs/CORE-API.md +343 -0
  7. package/docs/DESIGN.md +398 -0
  8. package/docs/assets/character.jpg +0 -0
  9. package/docs/assets/portrait.jpg +0 -0
  10. package/docs/screenshots/01-overview-light.png +0 -0
  11. package/docs/screenshots/02-overview-dark.png +0 -0
  12. package/docs/screenshots/03-ticket-light.png +0 -0
  13. package/docs/screenshots/04-ticket-qr-light.png +0 -0
  14. package/docs/screenshots/05-settings-light.png +0 -0
  15. package/docs/screenshots/06-diagnostics-light.png +0 -0
  16. package/docs/screenshots/07-mobile-overview.png +0 -0
  17. package/docs/screenshots/08-install-missing.png +0 -0
  18. package/docs/screenshots/09-keeper.png +0 -0
  19. package/package.json +56 -4
  20. package/src/cli.js +238 -0
  21. package/src/config.js +167 -0
  22. package/src/devices.js +163 -0
  23. package/src/errors.js +24 -0
  24. package/src/index.js +19 -0
  25. package/src/installer.js +907 -0
  26. package/src/manager.js +647 -0
  27. package/src/parse.js +207 -0
  28. package/src/paths.js +97 -0
  29. package/src/preferences.js +190 -0
  30. package/src/qr.js +37 -0
  31. package/src/relay.js +76 -0
  32. package/src/runtime/node.js +281 -0
  33. package/src/serve-targets.js +127 -0
  34. package/src/server.js +674 -0
  35. package/src/settings.js +262 -0
  36. package/src/supervisor.js +833 -0
  37. package/vendor/qrcode.mjs +2289 -0
  38. package/web/api.js +114 -0
  39. package/web/assets/portrait.jpg +0 -0
  40. package/web/boot.js +29 -0
  41. package/web/components.js +558 -0
  42. package/web/dict.js +663 -0
  43. package/web/drawer-access.js +318 -0
  44. package/web/drawer-address.js +271 -0
  45. package/web/drawer-automation.js +223 -0
  46. package/web/drawer-relay.js +128 -0
  47. package/web/drawer-tunnel.js +309 -0
  48. package/web/drawer.js +302 -0
  49. package/web/format.js +252 -0
  50. package/web/icon.png +0 -0
  51. package/web/index.html +29 -0
  52. package/web/main.js +471 -0
  53. package/web/room-diagnostics.js +426 -0
  54. package/web/room-overview.js +438 -0
  55. package/web/room-settings.js +71 -0
  56. package/web/store.js +168 -0
  57. package/web/styles.css +2873 -0
  58. package/web/ticket.js +212 -0
package/web/main.js ADDED
@@ -0,0 +1,471 @@
1
+ /**
2
+ * The shell: top bar, room rail, theme and language, polling, and notices.
3
+ *
4
+ * Rooms are built once per language and then updated in place on every poll, so
5
+ * a running poll never rebuilds an input the user is typing into. The page owns
6
+ * exactly two pieces of persistent UI state: the selected room and the draft
7
+ * state the rooms keep in `ui`.
8
+ */
9
+
10
+ import { createApi } from './api.js';
11
+ import { createStore } from './store.js';
12
+ import { resolveLang, translate } from './dict.js';
13
+ import {
14
+ h,
15
+ icon,
16
+ button,
17
+ noticeBar,
18
+ skeleton,
19
+ alertBox,
20
+ } from './components.js';
21
+ import { phaseOf, phaseKey, phaseTone } from './format.js';
22
+ import { createOverviewRoom } from './room-overview.js';
23
+ import { createSettingsRoom } from './room-settings.js';
24
+ import { createDiagnosticsRoom } from './room-diagnostics.js';
25
+
26
+ const THEME_KEY = 'tailcat-webui.theme';
27
+ const LANG_KEY = 'tailcat-webui.lang';
28
+ const ROOMS = [
29
+ { id: 'overview', titleKey: 'tabOverview', testid: 'room-overview' },
30
+ { id: 'settings', titleKey: 'tabSettings', testid: 'room-settings' },
31
+ { id: 'diagnostics', titleKey: 'tabDiagnostics', testid: 'room-diagnostics' },
32
+ ];
33
+ const THEME_CYCLE = ['system', 'light', 'dark'];
34
+ const THEME_ICONS = { system: 'system', light: 'sun', dark: 'moon' };
35
+ const THEME_LABEL_KEYS = { system: 'themeSystem', light: 'themeLight', dark: 'themeDark' };
36
+
37
+ const api = createApi();
38
+ const store = createStore({ api, onChange: requestRender });
39
+
40
+ const shell = {
41
+ /** Room id currently shown. */
42
+ room: readRoomFromHash() || 'overview',
43
+ theme: readTheme(),
44
+ lang: readLang(),
45
+ };
46
+
47
+ const ui = {
48
+ overviewUi: {},
49
+ ticketUi: {},
50
+ settingsUi: { drawers: {} },
51
+ };
52
+
53
+ const content = document.getElementById('content');
54
+ const rail = document.getElementById('room-rail');
55
+ const topbarActions = document.getElementById('topbar-actions');
56
+
57
+ let t = makeTranslator(shell.lang);
58
+ let toastSignature = '';
59
+ let scheduled = 0;
60
+ const railButtons = new Map();
61
+ const rooms = new Map();
62
+ let loadingPanel = null;
63
+ let errorPanel = null;
64
+ let errorText = null;
65
+ let pollBanner = null;
66
+ let pollBannerText = null;
67
+ let noticeHost = null;
68
+ let statusPill = null;
69
+ let statusPillText = null;
70
+ let themeButton = null;
71
+ let langButton = null;
72
+
73
+ /* Theme ------------------------------------------------------------------ */
74
+
75
+ const darkQuery = typeof window.matchMedia === 'function' ? window.matchMedia('(prefers-color-scheme: dark)') : null;
76
+
77
+ /**
78
+ * @returns {'system' | 'light' | 'dark'} stored preference.
79
+ */
80
+ function readTheme() {
81
+ try {
82
+ const stored = localStorage.getItem(THEME_KEY);
83
+ if (stored === 'light' || stored === 'dark' || stored === 'system') return stored;
84
+ } catch {
85
+ // Storage can be blocked; the system preference still applies.
86
+ }
87
+ return 'system';
88
+ }
89
+
90
+ /**
91
+ * @returns {'zh' | 'en'} stored language, or the browser language.
92
+ */
93
+ function readLang() {
94
+ let stored = null;
95
+ try {
96
+ stored = localStorage.getItem(LANG_KEY);
97
+ } catch {
98
+ stored = null;
99
+ }
100
+ return resolveLang(navigator, stored);
101
+ }
102
+
103
+ /**
104
+ * @param {'system' | 'light' | 'dark'} preference - theme preference.
105
+ * @returns {'light' | 'dark'} theme that should be painted.
106
+ */
107
+ function resolvedTheme(preference) {
108
+ if (preference === 'light' || preference === 'dark') return preference;
109
+ return darkQuery && darkQuery.matches ? 'dark' : 'light';
110
+ }
111
+
112
+ function paintTheme() {
113
+ const resolved = resolvedTheme(shell.theme);
114
+ document.documentElement.setAttribute('data-theme', resolved);
115
+ document.documentElement.setAttribute('data-theme-mode', shell.theme);
116
+ if (themeButton) {
117
+ const modeLabel = t(THEME_LABEL_KEYS[shell.theme]);
118
+ themeButton.replaceChildren(icon(THEME_ICONS[shell.theme]));
119
+ themeButton.setAttribute('aria-label', t('themeToggle'));
120
+ themeButton.title = t('themeCurrent', { mode: modeLabel });
121
+ }
122
+ }
123
+
124
+ function cycleTheme() {
125
+ const index = THEME_CYCLE.indexOf(shell.theme);
126
+ shell.theme = THEME_CYCLE[(index + 1) % THEME_CYCLE.length];
127
+ try {
128
+ localStorage.setItem(THEME_KEY, shell.theme);
129
+ } catch {
130
+ // Ignore: the choice still applies to this page.
131
+ }
132
+ paintTheme();
133
+ }
134
+
135
+ /* Language --------------------------------------------------------------- */
136
+
137
+ /**
138
+ * @param {'zh' | 'en'} lang - language code.
139
+ * @returns {Function} translator with a `lang` property.
140
+ */
141
+ function makeTranslator(lang) {
142
+ const translator = (key, params) => translate(lang, key, params);
143
+ translator.lang = lang;
144
+ return translator;
145
+ }
146
+
147
+ function paintLanguage() {
148
+ t = makeTranslator(shell.lang);
149
+ document.documentElement.lang = shell.lang;
150
+ document.title = t('documentTitle');
151
+ }
152
+
153
+ function toggleLanguage() {
154
+ const focusOnLang = document.activeElement === langButton;
155
+ const focusInRail = rail.contains(document.activeElement);
156
+ shell.lang = shell.lang === 'zh' ? 'en' : 'zh';
157
+ try {
158
+ localStorage.setItem(LANG_KEY, shell.lang);
159
+ } catch {
160
+ // Ignore: the choice still applies to this page.
161
+ }
162
+ paintLanguage();
163
+ // A notice is stored as already-translated text, so it must not survive the
164
+ // language switch and leave one sentence in the old language on the page.
165
+ store.dismissNotice();
166
+ buildShell();
167
+ render();
168
+ // The toggle rebuilds its own controls; keep keyboard focus where it was.
169
+ if (focusOnLang && langButton) langButton.focus();
170
+ else if (focusInRail) {
171
+ const tab = railButtons.get(shell.room);
172
+ if (tab) tab.focus();
173
+ }
174
+ }
175
+
176
+ /* Routing ---------------------------------------------------------------- */
177
+
178
+ /**
179
+ * @returns {string | null} a valid room id from the URL hash.
180
+ */
181
+ function readRoomFromHash() {
182
+ const raw = typeof location.hash === 'string' ? location.hash.replace('#', '') : '';
183
+ return ROOMS.some((room) => room.id === raw) ? raw : null;
184
+ }
185
+
186
+ /**
187
+ * @param {string} id - room id.
188
+ * @param {{ updateHash?: boolean, scroll?: boolean }} [options] - routing side effects.
189
+ */
190
+ function goRoom(id, options = {}) {
191
+ const room = ROOMS.find((entry) => entry.id === id);
192
+ if (!room) return;
193
+ const changed = shell.room !== id;
194
+ shell.room = id;
195
+ if (changed) {
196
+ paintRooms();
197
+ if (options.updateHash !== false) {
198
+ try {
199
+ history.replaceState(null, '', `#${id}`);
200
+ } catch {
201
+ // A sandboxed page can refuse history writes; in-page routing still works.
202
+ }
203
+ }
204
+ if (options.scroll !== false) window.scrollTo({ top: 0 });
205
+ }
206
+ }
207
+
208
+ /**
209
+ * @param {string} id - drawer id.
210
+ * @param {{ focus?: boolean }} [options] - focus inside the drawer.
211
+ */
212
+ function openDrawer(id, options = {}) {
213
+ if (shell.room !== 'settings') goRoom('settings', { scroll: false });
214
+ const room = rooms.get('settings');
215
+ if (room) room.openDrawer(id, options);
216
+ }
217
+
218
+ /* Notifications ---------------------------------------------------------- */
219
+
220
+ /**
221
+ * @param {'ok' | 'error' | 'info'} kind - tone.
222
+ * @param {string} text - already translated text.
223
+ */
224
+ function notify(kind, text) {
225
+ store.setNotice(kind, text);
226
+ }
227
+
228
+ /**
229
+ * @param {Error & { code?: string }} error - read failure.
230
+ * @returns {string} localized message.
231
+ */
232
+ function messageOf(error) {
233
+ if (!error) return t('unknown');
234
+ if (error.code === 'network') return t('networkError');
235
+ return error.message || t('unknown');
236
+ }
237
+
238
+ /* Shell construction ----------------------------------------------------- */
239
+
240
+ /**
241
+ * @returns {object} context shared with the rooms.
242
+ */
243
+ function roomContext() {
244
+ return {
245
+ t,
246
+ lang: shell.lang,
247
+ state: () => store.snapshot,
248
+ act: (action, payload) => store.act(action, payload),
249
+ refresh: () => store.refresh(),
250
+ busy: () => store.busy,
251
+ notify,
252
+ goRoom,
253
+ openDrawer,
254
+ overviewUi: ui.overviewUi,
255
+ ticketUi: ui.ticketUi,
256
+ settingsUi: ui.settingsUi,
257
+ };
258
+ }
259
+
260
+ function buildRail() {
261
+ railButtons.clear();
262
+ rail.setAttribute('aria-label', t('roomNavAria'));
263
+ const buttons = ROOMS.map((room) => {
264
+ const buttonEl = h('button', {
265
+ type: 'button',
266
+ class: 'room-tab',
267
+ role: 'tab',
268
+ id: `tab-${room.id}`,
269
+ 'aria-controls': `room-${room.id}`,
270
+ 'aria-selected': 'false',
271
+ 'data-testid': room.testid,
272
+ // Roving tabindex: only the selected tab is in the tab order.
273
+ tabindex: '-1',
274
+ },
275
+ h('span', { class: 'room-dot', 'aria-hidden': 'true' }),
276
+ h('span', { class: 'room-label' }, t(room.titleKey)));
277
+ buttonEl.addEventListener('click', () => goRoom(room.id));
278
+ railButtons.set(room.id, buttonEl);
279
+ return buttonEl;
280
+ });
281
+ rail.replaceChildren(...buttons);
282
+ }
283
+
284
+ function buildTopbar() {
285
+ statusPill = h('span', { class: 'status-pill', 'data-testid': 'status-pill' },
286
+ h('span', { class: 'status-dot', 'aria-hidden': 'true' }),
287
+ h('span', { class: 'status-pill-text' }));
288
+ statusPillText = statusPill.querySelector('.status-pill-text');
289
+
290
+ langButton = h('button', {
291
+ type: 'button',
292
+ class: 'topbar-btn topbar-lang',
293
+ 'aria-label': t('langToggle'),
294
+ title: t('langToggle'),
295
+ 'data-testid': 'lang-toggle',
296
+ }, shell.lang === 'zh' ? 'EN' : '中');
297
+ langButton.addEventListener('click', toggleLanguage);
298
+
299
+ themeButton = h('button', {
300
+ type: 'button',
301
+ class: 'topbar-btn topbar-theme',
302
+ 'aria-label': t('themeToggle'),
303
+ title: t('themeToggle'),
304
+ 'data-testid': 'theme-toggle',
305
+ });
306
+ themeButton.addEventListener('click', cycleTheme);
307
+
308
+ topbarActions.replaceChildren(statusPill, langButton, themeButton);
309
+ paintTheme();
310
+ }
311
+
312
+ /**
313
+ * Build the loading, error, and notice hosts plus the three rooms.
314
+ */
315
+ function buildContent() {
316
+ toastSignature = '';
317
+ for (const room of rooms.values()) {
318
+ if (typeof room.dispose === 'function') room.dispose();
319
+ }
320
+ rooms.clear();
321
+ loadingPanel = h('div', { class: 'state-panel' },
322
+ h('span', { class: 'sr-only' }, t('loading')),
323
+ skeleton());
324
+ errorText = h('p', { class: 'error-text' });
325
+ errorPanel = h('div', { class: 'state-panel', hidden: true },
326
+ alertBox({ tone: 'danger', title: t('loadErrorTitle'), children: h('p', null, t('loadErrorBody')) }),
327
+ errorText,
328
+ button({ label: t('retry'), variant: 'secondary', testid: 'load-retry', onClick: () => void store.refresh() }));
329
+ pollBannerText = h('p', { class: 'notice-text' });
330
+ pollBanner = h('div', { class: 'notice notice-error', role: 'status', 'aria-live': 'polite', hidden: true },
331
+ h('span', { class: 'notice-icon', 'aria-hidden': 'true' }, icon('caution')),
332
+ pollBannerText,
333
+ button({ label: t('retry'), variant: 'ghost', testid: 'poll-retry', onClick: () => void store.refresh() }));
334
+ noticeHost = h('div', { class: 'notice-host', hidden: true });
335
+
336
+ const ctx = roomContext();
337
+ const overview = createOverviewRoom(ctx);
338
+ const settings = createSettingsRoom(ctx);
339
+ const diagnostics = createDiagnosticsRoom(ctx);
340
+ rooms.set('overview', overview);
341
+ rooms.set('settings', settings);
342
+ rooms.set('diagnostics', diagnostics);
343
+
344
+ content.replaceChildren(pollBanner, noticeHost, loadingPanel, errorPanel, overview.el, settings.el, diagnostics.el);
345
+ paintRooms();
346
+ }
347
+
348
+ function buildShell() {
349
+ buildRail();
350
+ buildTopbar();
351
+ buildContent();
352
+ }
353
+
354
+ /**
355
+ * Show only the selected room panel and keep the tab state in sync.
356
+ */
357
+ function paintRooms() {
358
+ const ready = Boolean(store.snapshot);
359
+ for (const room of ROOMS) {
360
+ const tab = railButtons.get(room.id);
361
+ const selected = room.id === shell.room;
362
+ if (tab) {
363
+ tab.setAttribute('aria-selected', selected ? 'true' : 'false');
364
+ tab.tabIndex = selected ? 0 : -1;
365
+ tab.classList.toggle('is-selected', selected);
366
+ }
367
+ const panel = rooms.get(room.id);
368
+ if (panel) panel.el.hidden = !selected || !ready;
369
+ }
370
+ }
371
+
372
+ /* Rendering -------------------------------------------------------------- */
373
+
374
+ function requestRender() {
375
+ if (scheduled !== 0) return;
376
+ scheduled = requestAnimationFrame(() => {
377
+ scheduled = 0;
378
+ render();
379
+ });
380
+ }
381
+
382
+ function renderStatusPill(state) {
383
+ if (!statusPill) return;
384
+ const phase = state ? phaseOf(state) : 'unknown';
385
+ statusPill.setAttribute('data-phase', phaseTone(phase));
386
+ if (statusPillText) statusPillText.textContent = t(phaseKey(phase));
387
+ }
388
+
389
+ function renderNotice() {
390
+ if (!noticeHost) return;
391
+ const notice = store.notice;
392
+ if (!notice) {
393
+ noticeHost.hidden = true;
394
+ if (noticeHost.firstChild) noticeHost.replaceChildren();
395
+ toastSignature = '';
396
+ return;
397
+ }
398
+ const signature = `${notice.id}`;
399
+ if (signature === toastSignature) {
400
+ noticeHost.hidden = false;
401
+ return;
402
+ }
403
+ toastSignature = signature;
404
+ noticeHost.replaceChildren(noticeBar({
405
+ kind: notice.kind,
406
+ text: notice.text,
407
+ t,
408
+ onClose: () => store.dismissNotice(),
409
+ }));
410
+ noticeHost.hidden = false;
411
+ }
412
+
413
+ function render() {
414
+ const state = store.snapshot;
415
+ const error = store.error;
416
+
417
+ renderStatusPill(state);
418
+
419
+ if (loadingPanel) loadingPanel.hidden = Boolean(state) || Boolean(error);
420
+ if (errorPanel) {
421
+ const show = !state && Boolean(error);
422
+ errorPanel.hidden = !show;
423
+ if (show && errorText) errorText.textContent = messageOf(error);
424
+ }
425
+ if (pollBanner) {
426
+ const show = Boolean(state) && Boolean(error);
427
+ pollBanner.hidden = !show;
428
+ if (show && pollBannerText) pollBannerText.textContent = t('pollErrorText', { message: messageOf(error) });
429
+ }
430
+
431
+ for (const room of rooms.values()) room.update(state);
432
+ paintRooms();
433
+ renderNotice();
434
+ }
435
+
436
+ /* Events ---------------------------------------------------------------- */
437
+
438
+ rail.addEventListener('keydown', (event) => {
439
+ const keys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End'];
440
+ if (!keys.includes(event.key)) return;
441
+ const ids = ROOMS.map((room) => room.id);
442
+ const current = ids.indexOf(shell.room);
443
+ let next = current;
444
+ if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') next = (current - 1 + ids.length) % ids.length;
445
+ else if (event.key === 'ArrowDown' || event.key === 'ArrowRight') next = (current + 1) % ids.length;
446
+ else if (event.key === 'Home') next = 0;
447
+ else if (event.key === 'End') next = ids.length - 1;
448
+ event.preventDefault();
449
+ const id = ids[next];
450
+ goRoom(id);
451
+ const tab = railButtons.get(id);
452
+ if (tab) tab.focus();
453
+ });
454
+
455
+ window.addEventListener('hashchange', () => {
456
+ const id = readRoomFromHash();
457
+ if (id && id !== shell.room) goRoom(id, { updateHash: false, scroll: false });
458
+ });
459
+
460
+ if (darkQuery && typeof darkQuery.addEventListener === 'function') {
461
+ darkQuery.addEventListener('change', () => {
462
+ if (shell.theme === 'system') paintTheme();
463
+ });
464
+ }
465
+
466
+ /* Boot ------------------------------------------------------------------ */
467
+
468
+ paintLanguage();
469
+ buildShell();
470
+ store.start();
471
+ render();