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.
- package/LICENSE +21 -0
- package/NOTICE +29 -0
- package/README.en.md +112 -0
- package/README.md +101 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +398 -0
- package/docs/assets/character.jpg +0 -0
- package/docs/assets/portrait.jpg +0 -0
- package/docs/screenshots/01-overview-light.png +0 -0
- package/docs/screenshots/02-overview-dark.png +0 -0
- package/docs/screenshots/03-ticket-light.png +0 -0
- package/docs/screenshots/04-ticket-qr-light.png +0 -0
- package/docs/screenshots/05-settings-light.png +0 -0
- package/docs/screenshots/06-diagnostics-light.png +0 -0
- package/docs/screenshots/07-mobile-overview.png +0 -0
- package/docs/screenshots/08-install-missing.png +0 -0
- package/docs/screenshots/09-keeper.png +0 -0
- package/package.json +56 -4
- package/src/cli.js +238 -0
- package/src/config.js +167 -0
- package/src/devices.js +163 -0
- package/src/errors.js +24 -0
- package/src/index.js +19 -0
- package/src/installer.js +907 -0
- package/src/manager.js +647 -0
- package/src/parse.js +207 -0
- package/src/paths.js +97 -0
- package/src/preferences.js +190 -0
- package/src/qr.js +37 -0
- package/src/relay.js +76 -0
- package/src/runtime/node.js +281 -0
- package/src/serve-targets.js +127 -0
- package/src/server.js +674 -0
- package/src/settings.js +262 -0
- package/src/supervisor.js +833 -0
- package/vendor/qrcode.mjs +2289 -0
- package/web/api.js +114 -0
- package/web/assets/portrait.jpg +0 -0
- package/web/boot.js +29 -0
- package/web/components.js +558 -0
- package/web/dict.js +663 -0
- package/web/drawer-access.js +318 -0
- package/web/drawer-address.js +271 -0
- package/web/drawer-automation.js +223 -0
- package/web/drawer-relay.js +128 -0
- package/web/drawer-tunnel.js +309 -0
- package/web/drawer.js +302 -0
- package/web/format.js +252 -0
- package/web/icon.png +0 -0
- package/web/index.html +29 -0
- package/web/main.js +471 -0
- package/web/room-diagnostics.js +426 -0
- package/web/room-overview.js +438 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2873 -0
- package/web/ticket.js +212 -0
package/web/drawer.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One settings drawer: a header row that folds its body, the per-drawer state
|
|
3
|
+
* that survives a language switch, a save bar, and the inline feedback line.
|
|
4
|
+
*
|
|
5
|
+
* The field layout and the payload shape stay in the drawer modules; this file
|
|
6
|
+
* only owns the mechanics every drawer shares.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { h, button, icon, setText, setHidden, setDisabled, setClass } from './components.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {{ t: Function, settingsUi: { drawers: Record<string, object> } }} ctx - shell context.
|
|
13
|
+
* @param {{ id: string, testid: string, title: string }} options - drawer identity.
|
|
14
|
+
* @returns {object} drawer shell.
|
|
15
|
+
*/
|
|
16
|
+
export function createDrawer(ctx, { id, testid, title }) {
|
|
17
|
+
const t = ctx.t;
|
|
18
|
+
if (!ctx.settingsUi.drawers[id]) {
|
|
19
|
+
ctx.settingsUi.drawers[id] = {
|
|
20
|
+
open: false,
|
|
21
|
+
draft: {},
|
|
22
|
+
local: {},
|
|
23
|
+
dirty: new Set(),
|
|
24
|
+
feedback: null,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const ui = ctx.settingsUi.drawers[id];
|
|
28
|
+
if (!(ui.dirty instanceof Set)) ui.dirty = new Set(ui.dirty || []);
|
|
29
|
+
if (!ui.draft || typeof ui.draft !== 'object') ui.draft = {};
|
|
30
|
+
if (!ui.local || typeof ui.local !== 'object') ui.local = {};
|
|
31
|
+
|
|
32
|
+
const titleId = `drawer-title-${id}`;
|
|
33
|
+
const bodyId = `drawer-body-${id}`;
|
|
34
|
+
|
|
35
|
+
const overrideDot = h('span', {
|
|
36
|
+
class: 'override-dot',
|
|
37
|
+
role: 'img',
|
|
38
|
+
'aria-label': t('overrideDot'),
|
|
39
|
+
title: t('overrideDot'),
|
|
40
|
+
hidden: true,
|
|
41
|
+
});
|
|
42
|
+
const summary = h('span', { class: 'drawer-summary' });
|
|
43
|
+
const chevron = h('span', { class: 'drawer-chevron', 'aria-hidden': 'true' }, icon('chevron'));
|
|
44
|
+
const header = h('button', {
|
|
45
|
+
type: 'button',
|
|
46
|
+
class: 'drawer-head',
|
|
47
|
+
id: titleId,
|
|
48
|
+
'aria-expanded': ui.open ? 'true' : 'false',
|
|
49
|
+
'aria-controls': bodyId,
|
|
50
|
+
'data-testid': `${testid}-toggle`,
|
|
51
|
+
},
|
|
52
|
+
h('span', { class: 'drawer-heading' },
|
|
53
|
+
h('span', { class: 'drawer-title' }, title),
|
|
54
|
+
overrideDot),
|
|
55
|
+
summary,
|
|
56
|
+
h('span', { class: 'drawer-hair', 'aria-hidden': 'true' }),
|
|
57
|
+
chevron);
|
|
58
|
+
|
|
59
|
+
const feedback = h('div', {
|
|
60
|
+
class: 'drawer-feedback',
|
|
61
|
+
role: 'status',
|
|
62
|
+
'aria-live': 'polite',
|
|
63
|
+
hidden: true,
|
|
64
|
+
'data-testid': `${testid}-feedback`,
|
|
65
|
+
});
|
|
66
|
+
const bodyInner = h('div', { class: 'drawer-body-inner' });
|
|
67
|
+
const saveCopy = h('span', { class: 'savebar-copy' });
|
|
68
|
+
const resetButton = button({ label: t('undo'), variant: 'ghost' });
|
|
69
|
+
const saveButton = button({ label: t('save'), variant: 'primary', testid: `${testid}-save` });
|
|
70
|
+
const savebar = h('div', { class: 'savebar' },
|
|
71
|
+
saveCopy,
|
|
72
|
+
h('div', { class: 'savebar-actions' }, resetButton, saveButton));
|
|
73
|
+
const body = h('div', {
|
|
74
|
+
class: 'drawer-body',
|
|
75
|
+
id: bodyId,
|
|
76
|
+
role: 'region',
|
|
77
|
+
'aria-labelledby': titleId,
|
|
78
|
+
hidden: !ui.open,
|
|
79
|
+
}, feedback, bodyInner, savebar);
|
|
80
|
+
|
|
81
|
+
const heading = h('h3', { class: 'drawer-heading-wrap' }, header);
|
|
82
|
+
const el = h('section', { class: `drawer${ui.open ? ' is-open' : ''}`, 'data-testid': testid }, heading, body);
|
|
83
|
+
|
|
84
|
+
let onSave = null;
|
|
85
|
+
let onReset = null;
|
|
86
|
+
|
|
87
|
+
function focusFirst() {
|
|
88
|
+
const target = bodyInner.querySelector(
|
|
89
|
+
'input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [role="switch"]:not([disabled]), a[href]',
|
|
90
|
+
);
|
|
91
|
+
if (target && typeof target.focus === 'function') target.focus();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function applyOpen(next, focus = false) {
|
|
95
|
+
ui.open = next === true;
|
|
96
|
+
header.setAttribute('aria-expanded', ui.open ? 'true' : 'false');
|
|
97
|
+
body.hidden = !ui.open;
|
|
98
|
+
setClass(el, 'is-open', ui.open);
|
|
99
|
+
if (ui.open && focus) {
|
|
100
|
+
// Wait for the body to become visible before moving focus into it.
|
|
101
|
+
requestAnimationFrame(focusFirst);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
header.addEventListener('click', (event) => {
|
|
106
|
+
applyOpen(!ui.open, event.detail === 0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
saveButton.addEventListener('click', () => {
|
|
110
|
+
if (typeof onSave === 'function') onSave();
|
|
111
|
+
});
|
|
112
|
+
resetButton.addEventListener('click', () => {
|
|
113
|
+
if (typeof onReset === 'function') onReset();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {string} text - next summary.
|
|
118
|
+
* @param {boolean} overridden - whether any field is a local override.
|
|
119
|
+
*/
|
|
120
|
+
function paintHeader(text, overridden) {
|
|
121
|
+
setText(summary, text);
|
|
122
|
+
setHidden(overrideDot, !overridden);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {{ tone: 'ok' | 'error', text: string } | null} next - feedback state.
|
|
127
|
+
*/
|
|
128
|
+
function paintFeedback(next) {
|
|
129
|
+
if (!next) {
|
|
130
|
+
setHidden(feedback, true);
|
|
131
|
+
setText(feedback, '');
|
|
132
|
+
feedback.className = 'drawer-feedback';
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
setText(feedback, next.text);
|
|
136
|
+
feedback.className = `drawer-feedback drawer-feedback-${next.tone}`;
|
|
137
|
+
setHidden(feedback, false);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
paintFeedback(ui.feedback);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
el,
|
|
144
|
+
body: bodyInner,
|
|
145
|
+
header,
|
|
146
|
+
saveButton,
|
|
147
|
+
resetButton,
|
|
148
|
+
summary,
|
|
149
|
+
ui,
|
|
150
|
+
titleId,
|
|
151
|
+
isOpen: () => ui.open === true,
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Re-render the header summary, the impact copy, and button states.
|
|
155
|
+
* @param {{ summary: string, overridden: boolean, dirty: boolean, restart: boolean, address: boolean, busy: boolean, busyText?: string }} chrome - derived chrome state.
|
|
156
|
+
*/
|
|
157
|
+
renderChrome(chrome) {
|
|
158
|
+
paintHeader(chrome.summary, chrome.overridden === true);
|
|
159
|
+
const parts = [];
|
|
160
|
+
if (chrome.dirty) {
|
|
161
|
+
parts.push(t('unsaved'));
|
|
162
|
+
if (chrome.restart) parts.push(t('saveRestart'));
|
|
163
|
+
if (chrome.address) parts.push(t('saveAddress'));
|
|
164
|
+
}
|
|
165
|
+
setText(saveCopy, parts.join(t('summaryJoin')));
|
|
166
|
+
setClass(saveCopy, 'is-dirty', chrome.dirty === true);
|
|
167
|
+
const blocked = chrome.dirty !== true || chrome.busy === true;
|
|
168
|
+
setHidden(resetButton, chrome.dirty !== true);
|
|
169
|
+
setDisabled(resetButton, blocked);
|
|
170
|
+
setDisabled(saveButton, blocked);
|
|
171
|
+
setText(saveButton, chrome.busy ? chrome.busyText || t('saving') : t('save'));
|
|
172
|
+
saveButton.setAttribute('aria-busy', chrome.busy ? 'true' : 'false');
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {'ok' | 'error'} tone - feedback tone.
|
|
177
|
+
* @param {string} text - already translated message.
|
|
178
|
+
*/
|
|
179
|
+
setFeedback(tone, text) {
|
|
180
|
+
ui.feedback = { tone, text };
|
|
181
|
+
paintFeedback(ui.feedback);
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
clearFeedback() {
|
|
185
|
+
if (ui.feedback === null) return;
|
|
186
|
+
ui.feedback = null;
|
|
187
|
+
paintFeedback(null);
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
setOpen(next, focus = false) {
|
|
191
|
+
applyOpen(next, focus);
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
focusFirst() {
|
|
195
|
+
focusFirst();
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Move focus to a specific control inside the drawer, or to the first one.
|
|
200
|
+
* @param {string} [selector] - CSS selector relative to the drawer.
|
|
201
|
+
*/
|
|
202
|
+
focus(selector) {
|
|
203
|
+
const target = selector ? el.querySelector(selector) : null;
|
|
204
|
+
if (target && typeof target.focus === 'function') target.focus();
|
|
205
|
+
else focusFirst();
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
/** @param {() => void} handler - save button action. */
|
|
209
|
+
onSave(handler) {
|
|
210
|
+
onSave = handler;
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
/** @param {() => void} handler - undo button action. */
|
|
214
|
+
onReset(handler) {
|
|
215
|
+
onReset = handler;
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A draft over a fixed set of settings keys, stored in the drawer UI state so
|
|
222
|
+
* it survives a language switch. Dirty keys are never overwritten by polling.
|
|
223
|
+
* @param {object} ui - drawer UI state created by createDrawer.
|
|
224
|
+
* @param {string[]} keys - settings keys this drawer edits.
|
|
225
|
+
* @returns {object} draft helpers.
|
|
226
|
+
*/
|
|
227
|
+
export function draftApi(ui, keys) {
|
|
228
|
+
const api = {
|
|
229
|
+
/**
|
|
230
|
+
* Refresh every clean key from the snapshot.
|
|
231
|
+
* @param {object | null} state - latest snapshot.
|
|
232
|
+
*/
|
|
233
|
+
sync(state) {
|
|
234
|
+
const settings = state && typeof state.settings === 'object' && state.settings !== null ? state.settings : {};
|
|
235
|
+
for (const key of keys) {
|
|
236
|
+
if (!ui.dirty.has(key)) ui.draft[key] = settings[key];
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param {string} key - settings key.
|
|
242
|
+
* @returns {unknown} the draft value.
|
|
243
|
+
*/
|
|
244
|
+
value(key) {
|
|
245
|
+
return ui.draft[key];
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Write a draft value and mark the key dirty.
|
|
250
|
+
* @param {string} key - settings key.
|
|
251
|
+
* @param {unknown} value - next value.
|
|
252
|
+
*/
|
|
253
|
+
set(key, value) {
|
|
254
|
+
ui.draft[key] = value;
|
|
255
|
+
ui.dirty.add(key);
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @param {string} key - settings key.
|
|
260
|
+
* @returns {boolean} whether the key has unsaved changes.
|
|
261
|
+
*/
|
|
262
|
+
isDirty(key) {
|
|
263
|
+
return ui.dirty.has(key);
|
|
264
|
+
},
|
|
265
|
+
|
|
266
|
+
/** @returns {string[]} the dirty keys. */
|
|
267
|
+
dirtyKeys() {
|
|
268
|
+
return keys.filter((key) => ui.dirty.has(key));
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Clear the dirty flag for some keys.
|
|
273
|
+
* @param {string[]} [subset] - keys to clear; defaults to all.
|
|
274
|
+
*/
|
|
275
|
+
clear(subset) {
|
|
276
|
+
for (const key of subset || keys) ui.dirty.delete(key);
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Build the settings patch from the dirty keys.
|
|
281
|
+
* @param {Record<string, unknown>} [overrides] - values that win over the draft.
|
|
282
|
+
* @returns {Record<string, unknown>} patch.
|
|
283
|
+
*/
|
|
284
|
+
patch(overrides = {}) {
|
|
285
|
+
const patch = {};
|
|
286
|
+
for (const key of api.dirtyKeys()) patch[key] = overrides[key] !== undefined ? overrides[key] : ui.draft[key];
|
|
287
|
+
return patch;
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Restore every key from a source object and drop the dirty flags.
|
|
292
|
+
* @param {Record<string, unknown>} values - effective values.
|
|
293
|
+
*/
|
|
294
|
+
reset(values) {
|
|
295
|
+
for (const key of keys) {
|
|
296
|
+
ui.draft[key] = values[key];
|
|
297
|
+
ui.dirty.delete(key);
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
return api;
|
|
302
|
+
}
|
package/web/format.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small pure readers and formatters for the state the server sends.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here touches the DOM: rooms and drawers call these so the shape of
|
|
5
|
+
* `GET /api/state` is understood in one place and every screen agrees on how
|
|
6
|
+
* a missing field is rendered.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The masked address length. Kept long so a hidden address reads as hidden. */
|
|
10
|
+
export const ADDRESS_MASK = '******************';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object | null | undefined} state - snapshot.
|
|
14
|
+
* @returns {object} tailcat facts; never null.
|
|
15
|
+
*/
|
|
16
|
+
export function tailcatOf(state) {
|
|
17
|
+
return state && typeof state.tailcat === 'object' && state.tailcat !== null ? state.tailcat : {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {object | null | undefined} state - snapshot.
|
|
22
|
+
* @returns {object} installer facts; never null.
|
|
23
|
+
*/
|
|
24
|
+
export function installerOf(state) {
|
|
25
|
+
return state && typeof state.installer === 'object' && state.installer !== null ? state.installer : {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {object | null | undefined} state - snapshot.
|
|
30
|
+
* @returns {object} effective settings; never null.
|
|
31
|
+
*/
|
|
32
|
+
export function settingsOf(state) {
|
|
33
|
+
return state && typeof state.settings === 'object' && state.settings !== null ? state.settings : {};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {object | null | undefined} state - snapshot.
|
|
38
|
+
* @returns {object} relay facts; never null.
|
|
39
|
+
*/
|
|
40
|
+
export function relayOf(state) {
|
|
41
|
+
return state && typeof state.relay === 'object' && state.relay !== null ? state.relay : {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {object | null | undefined} state - snapshot.
|
|
46
|
+
* @returns {object | null} pairing facts, or null before the tunnel has an address.
|
|
47
|
+
*/
|
|
48
|
+
export function pairingOf(state) {
|
|
49
|
+
return state && typeof state.pairing === 'object' && state.pairing !== null ? state.pairing : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {object | null | undefined} state - snapshot.
|
|
54
|
+
* @returns {string} one of online/starting/restarting/stopping/stopped/error/unknown.
|
|
55
|
+
*/
|
|
56
|
+
export function phaseOf(state) {
|
|
57
|
+
const phase = tailcatOf(state).phase;
|
|
58
|
+
if (phase === 'online' || phase === 'starting' || phase === 'restarting' || phase === 'stopping' || phase === 'stopped' || phase === 'error') {
|
|
59
|
+
return phase;
|
|
60
|
+
}
|
|
61
|
+
return 'unknown';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {unknown} phase - phase code.
|
|
66
|
+
* @returns {string} dictionary key for the phase word.
|
|
67
|
+
*/
|
|
68
|
+
export function phaseKey(phase) {
|
|
69
|
+
switch (phase) {
|
|
70
|
+
case 'online':
|
|
71
|
+
return 'phaseOnline';
|
|
72
|
+
case 'starting':
|
|
73
|
+
return 'phaseStarting';
|
|
74
|
+
case 'restarting':
|
|
75
|
+
return 'phaseRestarting';
|
|
76
|
+
case 'stopping':
|
|
77
|
+
return 'phaseStopping';
|
|
78
|
+
case 'stopped':
|
|
79
|
+
return 'phaseStopped';
|
|
80
|
+
case 'error':
|
|
81
|
+
return 'phaseError';
|
|
82
|
+
default:
|
|
83
|
+
return 'phaseUnknown';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {unknown} phase - phase code.
|
|
89
|
+
* @returns {'online' | 'pending' | 'error' | 'idle'} visual bucket for the status dot.
|
|
90
|
+
*/
|
|
91
|
+
export function phaseTone(phase) {
|
|
92
|
+
if (phase === 'online') return 'online';
|
|
93
|
+
if (phase === 'error') return 'error';
|
|
94
|
+
if (phase === 'starting' || phase === 'restarting' || phase === 'stopping') return 'pending';
|
|
95
|
+
return 'idle';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {object | null | undefined} state - snapshot.
|
|
100
|
+
* @returns {string[]} canonical `virtual:target` mappings.
|
|
101
|
+
*/
|
|
102
|
+
export function targetsOf(state) {
|
|
103
|
+
const settings = settingsOf(state);
|
|
104
|
+
return Array.isArray(settings.extraTargets) ? settings.extraTargets.filter((item) => typeof item === 'string' && item !== '') : [];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {object | null | undefined} state - snapshot.
|
|
109
|
+
* @returns {boolean} whether the tunnel has anything to serve.
|
|
110
|
+
*/
|
|
111
|
+
export function hasTargets(state) {
|
|
112
|
+
if (targetsOf(state).length > 0) return true;
|
|
113
|
+
const tailcat = tailcatOf(state);
|
|
114
|
+
return Array.isArray(tailcat.targets) && tailcat.targets.length > 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Split one canonical mapping. The target may itself contain a colon
|
|
119
|
+
* (`localhost:8080`), so only the first colon is a separator.
|
|
120
|
+
* @param {string} mapping - canonical `virtual:target`.
|
|
121
|
+
* @returns {{ virtual: string, target: string }} editable row.
|
|
122
|
+
*/
|
|
123
|
+
export function parseMapping(mapping) {
|
|
124
|
+
const text = typeof mapping === 'string' ? mapping : '';
|
|
125
|
+
const split = text.indexOf(':');
|
|
126
|
+
if (split <= 0) return { virtual: '', target: text };
|
|
127
|
+
return { virtual: text.slice(0, split), target: text.slice(split + 1) };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} virtual - virtual port text.
|
|
132
|
+
* @param {string} target - target text.
|
|
133
|
+
* @returns {string} canonical mapping.
|
|
134
|
+
*/
|
|
135
|
+
export function buildMapping(virtual, target) {
|
|
136
|
+
return `${String(virtual).trim()}:${String(target).trim()}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {unknown} region - `{ id, name }` from tailcat.
|
|
141
|
+
* @returns {string} `name id`, or an empty string when no region is known.
|
|
142
|
+
*/
|
|
143
|
+
export function formatRegion(region) {
|
|
144
|
+
if (!region || typeof region !== 'object') return '';
|
|
145
|
+
const name = typeof region.name === 'string' ? region.name.trim() : '';
|
|
146
|
+
const id = region.id === null || region.id === undefined ? '' : String(region.id);
|
|
147
|
+
if (name !== '' && id !== '') return `${name} ${id}`;
|
|
148
|
+
return name || id;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {number | null | undefined} ms - elapsed time.
|
|
153
|
+
* @param {Function} t - translator.
|
|
154
|
+
* @returns {string} compact localized duration.
|
|
155
|
+
*/
|
|
156
|
+
export function formatDuration(ms, t) {
|
|
157
|
+
if (typeof ms !== 'number' || !Number.isFinite(ms) || ms < 0) return '—';
|
|
158
|
+
if (ms < 60_000) return t('durationSeconds', { n: Math.max(1, Math.round(ms / 1_000)) });
|
|
159
|
+
if (ms < 3_600_000) return t('durationMinutes', { n: Math.floor(ms / 60_000) });
|
|
160
|
+
if (ms < 86_400_000) {
|
|
161
|
+
const hours = Math.floor(ms / 3_600_000);
|
|
162
|
+
const minutes = Math.floor((ms % 3_600_000) / 60_000);
|
|
163
|
+
return minutes > 0 ? t('durationHoursMinutes', { h: hours, m: minutes }) : t('durationHours', { h: hours });
|
|
164
|
+
}
|
|
165
|
+
return t('durationDays', { d: Math.floor(ms / 86_400_000) });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {unknown} iso - ISO timestamp.
|
|
170
|
+
* @param {'zh' | 'en'} lang - language code.
|
|
171
|
+
* @returns {string} local clock time, or an em dash.
|
|
172
|
+
*/
|
|
173
|
+
export function formatClock(iso, lang) {
|
|
174
|
+
if (typeof iso !== 'string' || iso === '') return '—';
|
|
175
|
+
const date = new Date(iso);
|
|
176
|
+
if (Number.isNaN(date.getTime())) return '—';
|
|
177
|
+
try {
|
|
178
|
+
return new Intl.DateTimeFormat(lang === 'zh' ? 'zh-CN' : 'en-GB', {
|
|
179
|
+
hour: '2-digit',
|
|
180
|
+
minute: '2-digit',
|
|
181
|
+
second: '2-digit',
|
|
182
|
+
hour12: false,
|
|
183
|
+
}).format(date);
|
|
184
|
+
} catch {
|
|
185
|
+
return '—';
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {unknown} bytes - byte count.
|
|
191
|
+
* @returns {string} compact size for install progress.
|
|
192
|
+
*/
|
|
193
|
+
export function formatBytes(bytes) {
|
|
194
|
+
if (typeof bytes !== 'number' || !Number.isFinite(bytes) || bytes <= 0) return '0 B';
|
|
195
|
+
if (bytes < 1_024) return `${Math.round(bytes)} B`;
|
|
196
|
+
if (bytes < 1_048_576) return `${(bytes / 1_024).toFixed(1)} KB`;
|
|
197
|
+
return `${(bytes / 1_048_576).toFixed(1)} MB`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Turn the server QR matrix into one crisp SVG path plus its quiet zone.
|
|
202
|
+
* @param {unknown} matrix - `{ size, rows }` from the pairing snapshot.
|
|
203
|
+
* @param {number} [quietZone] - white border in modules.
|
|
204
|
+
* @returns {{ viewBox: string, path: string } | null} geometry, or null when malformed.
|
|
205
|
+
*/
|
|
206
|
+
export function qrGeometry(matrix, quietZone = 2) {
|
|
207
|
+
if (!matrix || typeof matrix !== 'object') return null;
|
|
208
|
+
const rows = Array.isArray(matrix.rows) ? matrix.rows : null;
|
|
209
|
+
const size = Number(matrix.size);
|
|
210
|
+
if (!rows || !Number.isInteger(size) || size <= 0) return null;
|
|
211
|
+
const total = size + quietZone * 2;
|
|
212
|
+
let path = '';
|
|
213
|
+
for (let row = 0; row < rows.length; row += 1) {
|
|
214
|
+
const bits = rows[row];
|
|
215
|
+
if (typeof bits !== 'string') return null;
|
|
216
|
+
let column = 0;
|
|
217
|
+
while (column < bits.length) {
|
|
218
|
+
if (bits[column] !== '1') {
|
|
219
|
+
column += 1;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const start = column;
|
|
223
|
+
while (column < bits.length && bits[column] === '1') column += 1;
|
|
224
|
+
path += `M${start + quietZone} ${row + quietZone}h${column - start}v1h-${column - start}z`;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return { viewBox: `0 0 ${total} ${total}`, path };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* @param {object | null | undefined} state - snapshot.
|
|
232
|
+
* @returns {string | null} the pairing address, or null.
|
|
233
|
+
*/
|
|
234
|
+
export function addressOf(state) {
|
|
235
|
+
const pairing = pairingOf(state);
|
|
236
|
+
if (pairing && typeof pairing.address === 'string' && pairing.address !== '') return pairing.address;
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Translate an action failure for inline feedback.
|
|
242
|
+
* @param {Error & { code?: string }} error - failure.
|
|
243
|
+
* @param {Function} t - translator.
|
|
244
|
+
* @param {string} prefixKey - message template key.
|
|
245
|
+
* @returns {string} operator-facing message.
|
|
246
|
+
*/
|
|
247
|
+
export function failureText(error, t, prefixKey) {
|
|
248
|
+
if (!error) return t('unknown');
|
|
249
|
+
if (error.code === 'busy') return t('actionBusy');
|
|
250
|
+
const message = error.code === 'network' ? t('networkError') : error.message || t('unknown');
|
|
251
|
+
return t(prefixKey, { message });
|
|
252
|
+
}
|
package/web/icon.png
ADDED
|
Binary file
|
package/web/index.html
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh" data-theme="light" data-theme-mode="system">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
6
|
+
<title>tailcat webui</title>
|
|
7
|
+
<meta name="description" content="A quiet console for tailcat: map local ports through an encrypted tunnel and pair devices with a ticket.">
|
|
8
|
+
<meta name="color-scheme" content="light dark">
|
|
9
|
+
<link rel="icon" href="/icon.png" type="image/png">
|
|
10
|
+
<script src="/assets/boot.js"></script>
|
|
11
|
+
<link rel="stylesheet" href="/assets/styles.css">
|
|
12
|
+
</head>
|
|
13
|
+
<body>
|
|
14
|
+
<div class="app">
|
|
15
|
+
<header class="topbar" role="banner">
|
|
16
|
+
<a class="brand" href="#overview">
|
|
17
|
+
<img class="brand-mark" src="/icon.png" alt="" width="28" height="28" decoding="async">
|
|
18
|
+
<h1 class="brand-name">tailcat webui</h1>
|
|
19
|
+
</a>
|
|
20
|
+
<div class="topbar-actions" id="topbar-actions"></div>
|
|
21
|
+
</header>
|
|
22
|
+
<div class="app-main">
|
|
23
|
+
<nav class="room-rail" id="room-rail" role="tablist" aria-label="房间"></nav>
|
|
24
|
+
<main class="content" id="content"></main>
|
|
25
|
+
</div>
|
|
26
|
+
</div>
|
|
27
|
+
<script type="module" src="/assets/main.js"></script>
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|