@ds-hu/dsh-tailcat-remote 0.0.0-stage → 0.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.
package/client.js ADDED
@@ -0,0 +1,1005 @@
1
+ window.__ModuleLoader__.load({
2
+ id: '@ds-hu/dsh-tailcat-remote',
3
+ factory(require) {
4
+ const React = require('react');
5
+ const h = React.createElement;
6
+ const { useCallback, useEffect, useMemo, useState } = React;
7
+
8
+ const NS = 'tailcat-remote';
9
+ const API = '/api/tailcat-remote';
10
+
11
+ const zh = {
12
+ nav: '远程访问',
13
+ title: '远程访问',
14
+ subtitle: '通过 tailcat 从任意地点访问本 Harness,无需开放公网端口,也无需调整入站防火墙。',
15
+ refresh: '刷新',
16
+ loading: '正在读取远程访问状态…',
17
+ statusLabel: '隧道',
18
+ statusOnline: '运行中',
19
+ statusStarting: '启动中',
20
+ statusRestarting: '等待重启',
21
+ statusStopping: '停止中',
22
+ statusStopped: '已停止',
23
+ statusError: '异常',
24
+ statusUnknown: '未知',
25
+ start: '启动',
26
+ stop: '停止',
27
+ restart: '重启',
28
+ rotating: '正在更换…',
29
+ rotate: '更换地址',
30
+ rotateArm: '确认更换地址?',
31
+ rotateConfirm: '确认,立即更换',
32
+ cancel: '取消',
33
+ rotateHint: '旧的 tailcat 密钥会被覆盖,隧道重新启动,旧地址立即失效。',
34
+ rotateCaveat: '更换 tailcat 地址不会让已持有 DSH 浏览器会话凭据的浏览器退出登录;这些凭据到期或从浏览器会话记录中移除后才失效。',
35
+ rotateDisconnect: '如果你正通过 tailcat 打开本页,更换、停止或保存设备列表都会断开当前页面;请在本机页面操作,或用新地址重新连接。',
36
+ qrUnavailable: '二维码生成失败,请直接复制上方地址。',
37
+ copy: '复制',
38
+ copied: '已复制',
39
+ copyFailed: '复制失败,请手动选择文本。',
40
+ showQr: '显示二维码',
41
+ hideQr: '隐藏二维码',
42
+ pairingTitle: '配对这台 Harness',
43
+ pairingHint: '在承载隧道的设备上运行下面的命令,然后用该设备的浏览器打开本地地址。',
44
+ addressLabel: 'tailcat 地址',
45
+ commandLabel: '客户端设备命令',
46
+ commandHint: '客户端绑定 {localPort} 端口,Harness 位于虚拟端口 {port}。',
47
+ openLocal: '打开本地地址',
48
+ localUrlHint: '隧道建立后,这个地址会走一次正常的 DSH 登录流程,之后沿用浏览器会话 Cookie。',
49
+ stepsTitle: '使用步骤',
50
+ stepInstall: '在客户端设备安装 tailcat。',
51
+ stepRun: '运行下面的命令,或扫描二维码把地址带到那台设备。',
52
+ stepOpen: '在客户端浏览器打开 http://127.0.0.1:{localPort}/。',
53
+ installHint: '可通过 Homebrew、发行版包或 GitHub Release 安装 tailcat;Linux 下载后赋予可执行权限并放进 PATH。',
54
+ securityTitle: '安全',
55
+ allowOffTitle: '地址就是唯一的钥匙',
56
+ allowOffBody: '任何拿到 tailcat 地址的人都能访问本 Harness。在下方添加至少一个客户端公钥后,访问才会限制在已配对设备。',
57
+ allowOnTitle: '已限制 {count} 台设备',
58
+ allowOnBody: '不在列表中的客户端会被 tailcat 拒绝。地址仍然需要,但不再足够。',
59
+ devicesLabel: '允许的客户端公钥',
60
+ devicesHint: '每行一个 nodekey。在客户端运行 tailcat genkey --client --key=client-default 创建密钥并打印公钥。',
61
+ devicesPlaceholder: 'nodekey:…',
62
+ devicesSave: '保存并应用',
63
+ devicesReset: '撤销修改',
64
+ devicesSaved: '设备列表已保存,tailcat 已重启生效。',
65
+ devicesEmpty: '还没有设备密钥,当前仅凭地址访问。',
66
+ deviceCount: '{count} 台已授权',
67
+ devicesGenerate: '在客户端设备生成密钥:',
68
+ diagnosticsTitle: '状态与诊断',
69
+ diagPhase: '状态',
70
+ diagAddressMode: '密钥模式',
71
+ diagKeyName: '密钥名',
72
+ diagVersion: 'tailcat 版本',
73
+ diagBinary: '可执行文件',
74
+ diagRegion: '中继区域',
75
+ diagRestarts: '自动重启',
76
+ diagDoor: '门',
77
+ diagTarget: '转发目标',
78
+ diagLastError: '最近错误',
79
+ diagWarnings: '警告',
80
+ recentTitle: '最近连接',
81
+ recentEmpty: '暂无连接记录。',
82
+ recentPeer: '设备',
83
+ recentPort: '端口',
84
+ recentVia: '路径',
85
+ recentDuration: '时长',
86
+ recentTime: '时间',
87
+ errorTitle: '远程访问需要处理',
88
+ retry: '重试',
89
+ unavailableTitle: '远程访问不可用',
90
+ unavailableBody: '插件无法读取控制接口。请刷新页面或查看 Host 日志。',
91
+ sessionExpired: '浏览器会话已过期,请重新打开 dsh web 输出的地址。',
92
+ setupTitle: '安装 tailcat',
93
+ setupBody: '插件找不到 tailcat 可执行文件。安装后重试。',
94
+ setupRetry: '重新检测',
95
+ firstRun: '首次启动:tailcat 正在生成保存密钥并向 DERP 中继探测路由,可能需要一分钟。',
96
+ modeStable: '保存密钥(稳定)',
97
+ modeEphemeral: '临时密钥(重启后变化)',
98
+ modeSocket: 'Unix socket',
99
+ modeTcp: '127.0.0.1 TCP',
100
+ busyStart: '正在启动 tailcat…',
101
+ busyStop: '正在停止 tailcat…',
102
+ busyRestart: '正在重启 tailcat…',
103
+ busyRotate: '正在更换地址…',
104
+ busySave: '正在保存设备密钥…',
105
+ noticeStarted: 'tailcat 正在启动。',
106
+ noticeStopped: 'tailcat 已停止。',
107
+ noticeRestarted: 'tailcat 已重启。',
108
+ noticeRotated: '地址已更换,新地址显示在下方。',
109
+ qrAlt: '包含 tailcat 地址的二维码',
110
+ footerNote: 'tailcat 地址即凭据,请只分享给你控制的设备。',
111
+ never: '从未',
112
+ unknown: '未知',
113
+ };
114
+
115
+ const en = {
116
+ nav: 'Remote Access',
117
+ title: 'Remote Access',
118
+ subtitle: 'Reach this Harness from anywhere through tailcat — no public port and no inbound firewall change.',
119
+ refresh: 'Refresh',
120
+ loading: 'Reading remote-access state…',
121
+ statusLabel: 'Tunnel',
122
+ statusOnline: 'Online',
123
+ statusStarting: 'Starting',
124
+ statusRestarting: 'Restarting',
125
+ statusStopping: 'Stopping',
126
+ statusStopped: 'Stopped',
127
+ statusError: 'Error',
128
+ statusUnknown: 'Unknown',
129
+ start: 'Start',
130
+ stop: 'Stop',
131
+ restart: 'Restart',
132
+ rotating: 'Rotating…',
133
+ rotate: 'Rotate address',
134
+ rotateArm: 'Rotate address?',
135
+ rotateConfirm: 'Yes, rotate now',
136
+ cancel: 'Cancel',
137
+ rotateHint: 'The saved tailcat key is replaced, the tunnel restarts, and the old address stops working immediately.',
138
+ rotateCaveat: 'Rotating the tailcat address does not sign out browsers that already hold a DSH session cookie. Those credentials end when they expire or are removed from the browser-session record.',
139
+ rotateDisconnect: 'If this page is open through tailcat, rotating, stopping, or saving the device list drops the current connection. Use the page on the Host, then reconnect with the new address.',
140
+ qrUnavailable: 'The QR code could not be generated; copy the address above instead.',
141
+ copy: 'Copy',
142
+ copied: 'Copied',
143
+ copyFailed: 'Could not copy; select the text manually.',
144
+ showQr: 'Show QR code',
145
+ hideQr: 'Hide QR code',
146
+ pairingTitle: 'Pair this Harness',
147
+ pairingHint: 'Run the command on the device that will carry the tunnel, then open the local URL in that device’s browser.',
148
+ addressLabel: 'tailcat address',
149
+ commandLabel: 'Command for the client device',
150
+ commandHint: 'The client binds port {localPort}; the Harness lives on virtual port {port}.',
151
+ openLocal: 'Open local URL',
152
+ localUrlHint: 'Once the tunnel is up, this URL offers the normal DSH login flow once, then keeps the session cookie.',
153
+ stepsTitle: 'Three steps',
154
+ stepInstall: 'Install tailcat on the client device.',
155
+ stepRun: 'Run the command, or scan the QR code to move the address to that device.',
156
+ stepOpen: 'Open http://127.0.0.1:{localPort}/ in the client browser.',
157
+ installHint: 'Install tailcat with Homebrew, a release binary, or another supported package. On Linux, download it from the GitHub release, make it executable, and put it on PATH.',
158
+ securityTitle: 'Security',
159
+ allowOffTitle: 'The address is the only key',
160
+ allowOffBody: 'Anyone who obtains the tailcat address can reach this Harness. Add at least one client public key below to restrict access to paired devices.',
161
+ allowOnTitle: 'Restricted to {count} device(s)',
162
+ allowOnBody: 'tailcat refuses every client whose key is not in this list. The address remains necessary, but it is not sufficient.',
163
+ devicesLabel: 'Allowed client public keys',
164
+ devicesHint: 'One nodekey per line. On the client, run tailcat genkey --client --key=client-default to create a key and print its public key.',
165
+ devicesPlaceholder: 'nodekey:…',
166
+ devicesSave: 'Save and apply',
167
+ devicesReset: 'Reset changes',
168
+ devicesSaved: 'Device list saved; tailcat restarted to apply it.',
169
+ devicesEmpty: 'No device keys yet — address-only mode.',
170
+ deviceCount: '{count} allowed',
171
+ devicesGenerate: 'Generate a client key on the client device:',
172
+ diagnosticsTitle: 'Status & diagnostics',
173
+ diagPhase: 'Phase',
174
+ diagAddressMode: 'Key mode',
175
+ diagKeyName: 'Key name',
176
+ diagVersion: 'tailcat version',
177
+ diagBinary: 'Executable',
178
+ diagRegion: 'Relay region',
179
+ diagRestarts: 'Auto-restarts',
180
+ diagDoor: 'Door',
181
+ diagTarget: 'Forward target',
182
+ diagLastError: 'Last error',
183
+ diagWarnings: 'Warnings',
184
+ recentTitle: 'Recent connections',
185
+ recentEmpty: 'No connections recorded yet.',
186
+ recentPeer: 'Device',
187
+ recentPort: 'Port',
188
+ recentVia: 'Path',
189
+ recentDuration: 'Duration',
190
+ recentTime: 'Time',
191
+ errorTitle: 'Remote access needs attention',
192
+ retry: 'Retry',
193
+ unavailableTitle: 'Remote access is unavailable',
194
+ unavailableBody: 'The plugin could not read its control API. Reload the page or check the Host log.',
195
+ sessionExpired: 'The browser session expired; reopen the URL printed by dsh web.',
196
+ setupTitle: 'Install tailcat',
197
+ setupBody: 'The plugin could not find the tailcat executable. Install it, then retry.',
198
+ setupRetry: 'Check again',
199
+ firstRun: 'First run: tailcat is creating a saved key and asking a DERP relay for a route. This can take a minute.',
200
+ modeStable: 'Saved (stable)',
201
+ modeEphemeral: 'Ephemeral (changes on restart)',
202
+ modeSocket: 'Unix socket',
203
+ modeTcp: '127.0.0.1 TCP',
204
+ busyStart: 'Starting tailcat…',
205
+ busyStop: 'Stopping tailcat…',
206
+ busyRestart: 'Restarting tailcat…',
207
+ busyRotate: 'Rotating the address…',
208
+ busySave: 'Saving device keys…',
209
+ noticeStarted: 'tailcat is starting.',
210
+ noticeStopped: 'tailcat is stopped.',
211
+ noticeRestarted: 'tailcat restarted.',
212
+ noticeRotated: 'The address was rotated; the new one is below.',
213
+ qrAlt: 'QR code containing the tailcat address',
214
+ footerNote: 'The tailcat address is a credential. Share it only with devices you control.',
215
+ never: 'never',
216
+ unknown: 'unknown',
217
+ };
218
+
219
+ const PHASE_KEY = {
220
+ online: 'statusOnline',
221
+ starting: 'statusStarting',
222
+ restarting: 'statusRestarting',
223
+ stopping: 'statusStopping',
224
+ stopped: 'statusStopped',
225
+ error: 'statusError',
226
+ };
227
+
228
+ /**
229
+ * Dark/light-theme-aware page shell. All colors come from Harness theme
230
+ * tokens except the QR code itself, which must stay black on white.
231
+ */
232
+ const STYLE = `
233
+ .tcr-page { height: 100%; overflow-y: auto; background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); font-family: inherit; }
234
+ .tcr-shell { max-width: 980px; margin: 0 auto; padding: 28px 24px 56px; display: flex; flex-direction: column; gap: 16px; }
235
+ .tcr-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; }
236
+ .tcr-heading { display: flex; align-items: flex-start; gap: 14px; min-width: 0; }
237
+ .tcr-mark { flex: 0 0 auto; width: 42px; height: 42px; border-radius: 12px; background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l3); display: grid; place-items: center; color: var(--dsw-alias-brand-primary); }
238
+ .tcr-title { margin: 0; font-size: 22px; line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; }
239
+ .tcr-subtitle { margin: 5px 0 0; font-size: 13px; line-height: 1.55; color: var(--dsw-alias-label-secondary); max-width: 620px; }
240
+ .tcr-header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
241
+ .tcr-card { background: var(--dsw-alias-bg-layer-1); border: 1px solid var(--dsw-alias-border-l3); border-radius: 14px; padding: 18px 20px; }
242
+ .tcr-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
243
+ .tcr-card-title { margin: 0; font-size: 15px; line-height: 1.4; font-weight: 600; }
244
+ .tcr-card-sub { margin: 4px 0 0; font-size: 12.5px; line-height: 1.55; color: var(--dsw-alias-label-secondary); }
245
+ .tcr-btn { appearance: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 32px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--dsw-alias-border-l2); background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-primary); font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap; transition: background .15s ease, border-color .15s ease, opacity .15s ease; }
246
+ .tcr-btn:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }
247
+ .tcr-btn:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }
248
+ .tcr-btn:disabled { opacity: .45; cursor: not-allowed; }
249
+ .tcr-btn-primary { background: var(--dsw-alias-button-primary-fill); border-color: transparent; color: var(--dsw-alias-label-primary-foreground); }
250
+ .tcr-btn-primary:hover:not(:disabled) { background: var(--dsw-alias-button-primary-hover); }
251
+ .tcr-btn-danger { color: var(--dsw-alias-state-error-primary); border-color: var(--dsw-alias-border-l3); background: transparent; }
252
+ .tcr-btn-danger:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover-danger); }
253
+ .tcr-btn-ghost { background: transparent; border-color: transparent; color: var(--dsw-alias-label-secondary); }
254
+ .tcr-btn-ghost:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
255
+ .tcr-pill { display: inline-flex; align-items: center; gap: 7px; height: 28px; padding: 0 10px; border-radius: 999px; background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l3); font-size: 12.5px; color: var(--dsw-alias-label-secondary); }
256
+ .tcr-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dsw-alias-label-tertiary); }
257
+ .tcr-phase-online .tcr-dot { background: var(--dsw-alias-state-success-primary); }
258
+ .tcr-phase-starting .tcr-dot, .tcr-phase-restarting .tcr-dot { background: var(--dsw-alias-state-warn-primary); }
259
+ .tcr-phase-error .tcr-dot { background: var(--dsw-alias-state-error-primary); }
260
+ .tcr-phase-stopping .tcr-dot { background: var(--dsw-alias-state-warn-primary); }
261
+ .tcr-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 20px; }
262
+ .tcr-row { display: flex; align-items: baseline; justify-content: space-between; gap: 14px; padding: 8px 0; border-bottom: 1px dashed var(--dsw-alias-border-l3); font-size: 13px; min-width: 0; }
263
+ .tcr-row:last-child { border-bottom: 0; }
264
+ .tcr-row-label { color: var(--dsw-alias-label-secondary); flex: 0 0 auto; }
265
+ .tcr-row-value { text-align: right; color: var(--dsw-alias-label-primary); min-width: 0; overflow-wrap: anywhere; }
266
+ .tcr-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 12.5px; }
267
+ .tcr-code { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l3); border-radius: 10px; padding: 10px 12px; margin-top: 8px; min-width: 0; }
268
+ .tcr-code-text { min-width: 0; overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 12.5px; line-height: 1.55; user-select: all; }
269
+ .tcr-address { font-size: 13px; line-height: 1.65; }
270
+ .tcr-label { display: block; font-size: 12px; font-weight: 500; color: var(--dsw-alias-label-secondary); margin: 0 0 6px; }
271
+ .tcr-textarea { width: 100%; box-sizing: border-box; min-height: 92px; resize: vertical; border-radius: 10px; border: 1px solid var(--dsw-alias-border-l2); background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); padding: 10px 12px; font: inherit; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; line-height: 1.6; }
272
+ .tcr-textarea:focus { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 1px; }
273
+ .tcr-alert { border-radius: 10px; padding: 12px 14px; border: 1px solid var(--dsw-alias-border-l3); background: var(--dsw-alias-bg-layer-2); font-size: 13px; line-height: 1.6; }
274
+ .tcr-alert-danger { border-color: color-mix(in srgb, var(--dsw-alias-state-error-primary) 35%, var(--dsw-alias-border-l3)); background: color-mix(in srgb, var(--dsw-alias-state-error-primary) 9%, var(--dsw-alias-bg-layer-1)); }
275
+ .tcr-alert-warn { border-color: color-mix(in srgb, var(--dsw-alias-state-warn-primary) 35%, var(--dsw-alias-border-l3)); background: color-mix(in srgb, var(--dsw-alias-state-warn-primary) 10%, var(--dsw-alias-bg-layer-1)); }
276
+ .tcr-alert-title { font-weight: 600; margin: 0 0 4px; }
277
+ .tcr-alert-body { margin: 0; color: var(--dsw-alias-label-secondary); overflow-wrap: anywhere; }
278
+ .tcr-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
279
+ .tcr-hint { font-size: 12px; line-height: 1.6; color: var(--dsw-alias-label-tertiary); margin-top: 8px; }
280
+ .tcr-qr-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 14px; }
281
+ .tcr-qr { width: min(240px, 62vw); padding: 12px; background: #fff; border-radius: 12px; border: 1px solid var(--dsw-alias-border-l3); box-sizing: content-box; }
282
+ .tcr-qr svg { display: block; width: 100%; height: auto; }
283
+ .tcr-steps { margin: 12px 0 0; padding-left: 20px; color: var(--dsw-alias-label-secondary); font-size: 13px; line-height: 1.7; }
284
+ .tcr-steps code { color: var(--dsw-alias-label-primary); }
285
+ .tcr-badge { display: inline-flex; align-items: center; height: 22px; padding: 0 8px; border-radius: 6px; background: var(--dsw-alias-bg-layer-3); color: var(--dsw-alias-label-secondary); font-size: 11.5px; white-space: nowrap; }
286
+ .tcr-recent { display: flex; flex-direction: column; gap: 0; margin-top: 4px; }
287
+ .tcr-recent-row { display: grid; grid-template-columns: 1.5fr .5fr .9fr .6fr .7fr; gap: 10px; align-items: baseline; padding: 8px 0; border-bottom: 1px dashed var(--dsw-alias-border-l3); font-size: 12.5px; }
288
+ .tcr-recent-row:last-child { border-bottom: 0; }
289
+ .tcr-recent-head { color: var(--dsw-alias-label-tertiary); font-size: 11.5px; }
290
+ .tcr-notice { border-radius: 10px; padding: 10px 14px; font-size: 13px; border: 1px solid var(--dsw-alias-border-l3); background: var(--dsw-alias-bg-layer-2); }
291
+ .tcr-notice-ok { border-color: color-mix(in srgb, var(--dsw-alias-state-success-primary) 35%, var(--dsw-alias-border-l3)); background: color-mix(in srgb, var(--dsw-alias-state-success-primary) 9%, var(--dsw-alias-bg-layer-1)); }
292
+ .tcr-notice-error { border-color: color-mix(in srgb, var(--dsw-alias-state-error-primary) 35%, var(--dsw-alias-border-l3)); background: color-mix(in srgb, var(--dsw-alias-state-error-primary) 9%, var(--dsw-alias-bg-layer-1)); color: var(--dsw-alias-label-primary); }
293
+ .tcr-footer { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; color: var(--dsw-alias-label-tertiary); font-size: 11.5px; line-height: 1.6; padding-top: 6px; }
294
+ .tcr-spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--dsw-alias-border-l2); border-top-color: var(--dsw-alias-brand-primary); animation: tcr-spin .8s linear infinite; }
295
+ @keyframes tcr-spin { to { transform: rotate(360deg); } }
296
+ .tcr-empty { color: var(--dsw-alias-label-tertiary); font-size: 13px; }
297
+ .tcr-warning-list { margin: 8px 0 0; padding-left: 18px; color: var(--dsw-alias-state-warn-label); font-size: 12px; line-height: 1.6; }
298
+ @media (max-width: 720px) {
299
+ .tcr-shell { padding: 16px 14px 40px; }
300
+ .tcr-header { flex-direction: column; }
301
+ .tcr-header-actions { justify-content: flex-start; }
302
+ .tcr-grid { grid-template-columns: 1fr; }
303
+ .tcr-recent-row { grid-template-columns: 1.4fr .6fr .9fr; }
304
+ .tcr-recent-row > :nth-child(4), .tcr-recent-row > :nth-child(5) { display: none; }
305
+ }
306
+ `;
307
+
308
+ /**
309
+ * Sidebar glyph. Inherits the row color so the active state is the shell's.
310
+ * @param {{ size?: number }} props - slot-injected icon props.
311
+ */
312
+ function PanelIcon({ size = 20 }) {
313
+ return h(
314
+ 'svg',
315
+ {
316
+ width: size,
317
+ height: size,
318
+ viewBox: '0 0 24 24',
319
+ fill: 'none',
320
+ 'aria-hidden': 'true',
321
+ style: { display: 'block' },
322
+ },
323
+ h('path', {
324
+ d: 'M7.2 8.6 6 4.8l4.2 2.5',
325
+ stroke: 'currentColor',
326
+ strokeWidth: 1.7,
327
+ strokeLinecap: 'round',
328
+ strokeLinejoin: 'round',
329
+ }),
330
+ h('path', {
331
+ d: 'M16.8 8.6 18 4.8l-4.2 2.5',
332
+ stroke: 'currentColor',
333
+ strokeWidth: 1.7,
334
+ strokeLinecap: 'round',
335
+ strokeLinejoin: 'round',
336
+ }),
337
+ h('path', {
338
+ d: 'M5.6 11.4c0-3.1 2.8-5.6 6.4-5.6s6.4 2.5 6.4 5.6v1.7c0 3.1-2.8 5.6-6.4 5.6s-6.4-2.5-6.4-5.6z',
339
+ stroke: 'currentColor',
340
+ strokeWidth: 1.6,
341
+ }),
342
+ h('circle', { cx: 12, cy: 12.3, r: 1.9, fill: 'currentColor' }),
343
+ h('path', { d: 'M12 14.2v2.2', stroke: 'currentColor', strokeWidth: 1.9, strokeLinecap: 'round' }),
344
+ );
345
+ }
346
+
347
+ /**
348
+ * @param {object} props - tone, title, children.
349
+ */
350
+ function Alert({ tone = 'info', title, children }) {
351
+ return h(
352
+ 'div',
353
+ { className: `tcr-alert ${tone === 'danger' ? 'tcr-alert-danger' : tone === 'warn' ? 'tcr-alert-warn' : ''}` },
354
+ title ? h('p', { className: 'tcr-alert-title' }, title) : null,
355
+ h('div', { className: 'tcr-alert-body' }, children),
356
+ );
357
+ }
358
+
359
+ /**
360
+ * @param {object} props - card framing.
361
+ */
362
+ function Card({ title, subtitle, actions, children }) {
363
+ return h(
364
+ 'section',
365
+ { className: 'tcr-card' },
366
+ title
367
+ ? h(
368
+ 'div',
369
+ { className: 'tcr-card-head' },
370
+ h('div', null, h('h2', { className: 'tcr-card-title' }, title), subtitle ? h('p', { className: 'tcr-card-sub' }, subtitle) : null),
371
+ actions ? h('div', { className: 'tcr-actions', style: { marginTop: 0 } }, actions) : null,
372
+ )
373
+ : null,
374
+ children,
375
+ );
376
+ }
377
+
378
+ /**
379
+ * @param {object} props - button props.
380
+ */
381
+ function Button({ variant = 'secondary', disabled = false, onClick, children, title }) {
382
+ return h(
383
+ 'button',
384
+ { type: 'button', className: `tcr-btn tcr-btn-${variant}`, disabled, onClick, title },
385
+ children,
386
+ );
387
+ }
388
+
389
+ /**
390
+ * @param {object} props - copy payload and label.
391
+ */
392
+ function CopyButton({ text, label, t, onResult }) {
393
+ return h(
394
+ Button,
395
+ {
396
+ variant: 'ghost',
397
+ onClick: async () => {
398
+ const ok = await copyText(text);
399
+ onResult?.(ok ? t('copied') : t('copyFailed'), ok);
400
+ },
401
+ title: label ?? t('copy'),
402
+ },
403
+ label ?? t('copy'),
404
+ );
405
+ }
406
+
407
+ /**
408
+ * @param {object} props - address matrix.
409
+ */
410
+ function QrCode({ matrix, t }) {
411
+ const geometry = useMemo(() => (matrix ? qrGeometry(matrix) : null), [matrix]);
412
+ if (!geometry) return null;
413
+ return h(
414
+ 'div',
415
+ { className: 'tcr-qr' },
416
+ h(
417
+ 'svg',
418
+ {
419
+ viewBox: geometry.viewBox,
420
+ role: 'img',
421
+ 'aria-label': t('qrAlt'),
422
+ shapeRendering: 'crispEdges',
423
+ xmlns: 'http://www.w3.org/2000/svg',
424
+ },
425
+ h('rect', { width: '100%', height: '100%', fill: '#fff' }),
426
+ h('path', { d: geometry.path, fill: '#000' }),
427
+ ),
428
+ );
429
+ }
430
+
431
+ /**
432
+ * @param {{ phase?: string, t: Function }} props - phase from the Host.
433
+ */
434
+ function StatusPill({ phase, t }) {
435
+ const key = PHASE_KEY[phase] ?? 'statusUnknown';
436
+ return h(
437
+ 'span',
438
+ { className: `tcr-pill tcr-phase-${phase ?? 'unknown'}`, role: 'status', 'aria-live': 'polite' },
439
+ h('span', { className: 'tcr-dot' }),
440
+ `${t('statusLabel')} · ${t(key)}`,
441
+ );
442
+ }
443
+
444
+ /**
445
+ * @param {object} props - label and value.
446
+ */
447
+ function Row({ label, children }) {
448
+ return h(
449
+ 'div',
450
+ { className: 'tcr-row' },
451
+ h('span', { className: 'tcr-row-label' }, label),
452
+ h('span', { className: 'tcr-row-value' }, children),
453
+ );
454
+ }
455
+
456
+ /** @returns {object} polling state and action helpers. */
457
+ function useRemoteState() {
458
+ const [snapshot, setSnapshot] = useState(null);
459
+ const [error, setError] = useState(null);
460
+ const [busy, setBusy] = useState(null);
461
+ const [notice, setNotice] = useState(null);
462
+
463
+ const reload = useCallback(async () => {
464
+ try {
465
+ const response = await fetch(API, {
466
+ method: 'GET',
467
+ headers: { accept: 'application/json' },
468
+ cache: 'no-store',
469
+ credentials: 'same-origin',
470
+ });
471
+ const data = await response.json().catch(() => null);
472
+ if (!response.ok || !data?.ok) {
473
+ throw new Error(data?.error?.message ?? `HTTP ${response.status}`);
474
+ }
475
+ setSnapshot(data);
476
+ setError(null);
477
+ } catch (failure) {
478
+ setError(failure instanceof Error ? failure.message : String(failure));
479
+ }
480
+ }, []);
481
+
482
+ useEffect(() => {
483
+ void reload();
484
+ const timer = setInterval(() => {
485
+ if (!document.hidden) void reload();
486
+ }, 2_500);
487
+ const onVisibility = () => {
488
+ if (!document.hidden) void reload();
489
+ };
490
+ document.addEventListener('visibilitychange', onVisibility);
491
+ return () => {
492
+ clearInterval(timer);
493
+ document.removeEventListener('visibilitychange', onVisibility);
494
+ };
495
+ }, [reload]);
496
+
497
+ const act = useCallback(
498
+ async (action, extra, noticeKey) => {
499
+ setBusy(action);
500
+ setNotice(null);
501
+ try {
502
+ const response = await fetch(API, {
503
+ method: 'POST',
504
+ headers: { accept: 'application/json', 'content-type': 'application/json' },
505
+ credentials: 'same-origin',
506
+ body: JSON.stringify({ action, ...extra }),
507
+ });
508
+ const data = await response.json().catch(() => null);
509
+ if (!response.ok || !data?.ok) {
510
+ throw new Error(data?.error?.message ?? `HTTP ${response.status}`);
511
+ }
512
+ setSnapshot(data);
513
+ if (noticeKey) setNotice({ kind: 'ok', key: noticeKey });
514
+ return true;
515
+ } catch (failure) {
516
+ setNotice({ kind: 'error', text: failure instanceof Error ? failure.message : String(failure) });
517
+ return false;
518
+ } finally {
519
+ setBusy(null);
520
+ void reload();
521
+ }
522
+ },
523
+ [reload],
524
+ );
525
+
526
+ return { snapshot, error, busy, notice, setNotice, act, reload };
527
+ }
528
+
529
+ /** The page registered at the `main` key `tailcat-remote`. */
530
+ function RemotePage({ t }) {
531
+ const { snapshot, error, busy, notice, setNotice, act, reload } = useRemoteState();
532
+ const [showQr, setShowQr] = useState(false);
533
+ const [rotateArmed, setRotateArmed] = useState(false);
534
+ const [draft, setDraft] = useState('');
535
+ const [devicesDirty, setDevicesDirty] = useState(false);
536
+ const [localNotice, setLocalNotice] = useState(null);
537
+
538
+ const displayedNotice = localNotice ?? notice;
539
+ useEffect(() => {
540
+ if (!displayedNotice) return undefined;
541
+ const timer = setTimeout(() => {
542
+ setLocalNotice(null);
543
+ setNotice(null);
544
+ }, 6_000);
545
+ return () => clearTimeout(timer);
546
+ }, [displayedNotice, setNotice]);
547
+
548
+ useEffect(() => {
549
+ if (rotateArmed) {
550
+ const timer = setTimeout(() => setRotateArmed(false), 8_000);
551
+ return () => clearTimeout(timer);
552
+ }
553
+ return undefined;
554
+ }, [rotateArmed]);
555
+
556
+ useEffect(() => {
557
+ if (!snapshot || devicesDirty) return;
558
+ setDraft((snapshot.devices.allow ?? []).join('\n'));
559
+ }, [snapshot, devicesDirty]);
560
+
561
+ const tailcat = snapshot?.tailcat;
562
+ const phase = tailcat?.phase ?? 'stopped';
563
+ const pairing = snapshot?.pairing ?? null;
564
+ const busyKey =
565
+ busy === 'start'
566
+ ? 'busyStart'
567
+ : busy === 'stop'
568
+ ? 'busyStop'
569
+ : busy === 'restart'
570
+ ? 'busyRestart'
571
+ : busy === 'rotate'
572
+ ? 'busyRotate'
573
+ : busy === 'devices'
574
+ ? 'busySave'
575
+ : null;
576
+
577
+ const reportCopy = (text, ok) => setLocalNotice({ kind: ok ? 'ok' : 'error', text });
578
+
579
+ if (!snapshot) {
580
+ return h(
581
+ 'div',
582
+ { className: 'tcr-page' },
583
+ h('style', null, STYLE),
584
+ h(
585
+ 'div',
586
+ { className: 'tcr-shell' },
587
+ h('div', { className: 'tcr-header' }, h('div', { className: 'tcr-heading' },
588
+ h('div', { className: 'tcr-mark' }, h(PanelIcon, { size: 22 })),
589
+ h('div', null, h('h1', { className: 'tcr-title' }, t('title')), h('p', { className: 'tcr-subtitle' }, t('subtitle'))),
590
+ )),
591
+ error
592
+ ? h(
593
+ Alert,
594
+ { tone: 'danger', title: t('unavailableTitle') },
595
+ h('p', null, t('unavailableBody')),
596
+ h('p', { className: 'tcr-mono' }, error),
597
+ h('div', { className: 'tcr-actions' }, h(Button, { onClick: () => void reload() }, t('retry'))),
598
+ )
599
+ : h('div', { className: 'tcr-empty' }, h('span', { className: 'tcr-spinner', style: { display: 'inline-block', verticalAlign: 'middle', marginRight: 8 } }), t('loading')),
600
+ ),
601
+ );
602
+ }
603
+
604
+ const binaryMissing = tailcat.binary?.resolved === null && phase === 'error';
605
+ const install = snapshot.plugin?.name ?? '@ds-hu/dsh-tailcat-remote';
606
+ const command = pairing?.command ?? '';
607
+ const localUrl = pairing ? `http://127.0.0.1:${pairing.localPort}/` : '';
608
+ const saveBusy = busy === 'devices';
609
+
610
+ const saveDevices = async () => {
611
+ const allow = draft
612
+ .split(/\r?\n/)
613
+ .map((line) => line.trim())
614
+ .filter((line) => line.length > 0 && !line.startsWith('#'));
615
+ const ok = await act('devices', { allow }, 'devicesSaved');
616
+ if (ok) setDevicesDirty(false);
617
+ };
618
+
619
+ const rotate = async () => {
620
+ setRotateArmed(false);
621
+ await act('rotate', {}, 'noticeRotated');
622
+ };
623
+
624
+ return h(
625
+ 'div',
626
+ { className: 'tcr-page' },
627
+ h('style', null, STYLE),
628
+ h(
629
+ 'div',
630
+ { className: 'tcr-shell' },
631
+ h(
632
+ 'div',
633
+ { className: 'tcr-header' },
634
+ h(
635
+ 'div',
636
+ { className: 'tcr-heading' },
637
+ h('div', { className: 'tcr-mark' }, h(PanelIcon, { size: 22 })),
638
+ h(
639
+ 'div',
640
+ null,
641
+ h('h1', { className: 'tcr-title' }, t('title')),
642
+ h('p', { className: 'tcr-subtitle' }, t('subtitle')),
643
+ ),
644
+ ),
645
+ h(
646
+ 'div',
647
+ { className: 'tcr-header-actions' },
648
+ h(StatusPill, { phase, t }),
649
+ phase === 'stopped' || phase === 'error'
650
+ ? h(
651
+ Button,
652
+ {
653
+ variant: 'primary',
654
+ disabled: Boolean(busy),
655
+ onClick: () => void act('start', {}, 'noticeStarted'),
656
+ },
657
+ t('start'),
658
+ )
659
+ : h(
660
+ Button,
661
+ {
662
+ disabled: Boolean(busy),
663
+ onClick: () => void act('stop', {}, 'noticeStopped'),
664
+ },
665
+ t('stop'),
666
+ ),
667
+ h(
668
+ Button,
669
+ { disabled: Boolean(busy), onClick: () => void act('restart', {}, 'noticeRestarted') },
670
+ t('restart'),
671
+ ),
672
+ h(Button, { variant: 'ghost', disabled: Boolean(busy), onClick: () => void reload() }, t('refresh')),
673
+ ),
674
+ ),
675
+
676
+ displayedNotice
677
+ ? h(
678
+ 'div',
679
+ { className: `tcr-notice ${displayedNotice.kind === 'ok' ? 'tcr-notice-ok' : 'tcr-notice-error'}`, role: 'status' },
680
+ displayedNotice.key ? t(displayedNotice.key) : displayedNotice.text,
681
+ )
682
+ : null,
683
+ busyKey ? h('div', { className: 'tcr-hint' }, h('span', { className: 'tcr-spinner', style: { display: 'inline-block', verticalAlign: 'middle', marginRight: 8 } }), t(busyKey)) : null,
684
+
685
+ error ? h(Alert, { tone: 'danger', title: t('unavailableTitle') }, h('p', null, t('unavailableBody')), h('p', { className: 'tcr-mono' }, error)) : null,
686
+ snapshot.door?.phase === 'error'
687
+ ? h(Alert, { tone: 'danger', title: t('diagDoor') }, snapshot.door.lastError ?? t('unknown'))
688
+ : null,
689
+ tailcat.lastError && phase === 'error' && !binaryMissing
690
+ ? h(Alert, { tone: 'danger', title: t('diagLastError') }, h('p', { className: 'tcr-mono' }, tailcat.lastError))
691
+ : null,
692
+
693
+ binaryMissing
694
+ ? h(
695
+ Card,
696
+ { title: t('setupTitle'), subtitle: t('setupBody') },
697
+ h('p', { className: 'tcr-hint' }, t('installHint')),
698
+ h('div', { className: 'tcr-actions' }, h(Button, { variant: 'primary', disabled: Boolean(busy), onClick: () => void act('restart', {}, 'noticeRestarted') }, t('setupRetry'))),
699
+ )
700
+ : null,
701
+
702
+ !pairing && !binaryMissing
703
+ ? h(
704
+ Card,
705
+ { title: t('statusLabel') },
706
+ h(
707
+ 'div',
708
+ { className: 'tcr-grid' },
709
+ h(Row, { label: t('diagPhase') }, t(PHASE_KEY[phase] ?? 'statusUnknown')),
710
+ h(Row, { label: t('diagBinary') }, h('span', { className: 'tcr-mono' }, tailcat.binary?.resolved ?? snapshot.config.binaryPath)),
711
+ ),
712
+ phase === 'starting' || phase === 'restarting'
713
+ ? h(Alert, { tone: 'warn' }, h('div', { className: 'tcr-hint', style: { marginTop: 0 } }, h('span', { className: 'tcr-spinner', style: { display: 'inline-block', verticalAlign: 'middle', marginRight: 8 } }), t('firstRun')))
714
+ : h('p', { className: 'tcr-hint' }, h(Button, { variant: 'primary', disabled: Boolean(busy), onClick: () => void act('start', {}, 'noticeStarted') }, t('start'))),
715
+ )
716
+ : null,
717
+
718
+ pairing
719
+ ? h(
720
+ Card,
721
+ {
722
+ title: t('pairingTitle'),
723
+ subtitle: t('pairingHint'),
724
+ actions: h('span', { className: 'tcr-badge' }, tailcat.keyMode === 'ephemeral' ? t('modeEphemeral') : t('modeStable')),
725
+ },
726
+ h('label', { className: 'tcr-label' }, t('addressLabel')),
727
+ h(
728
+ 'div',
729
+ { className: 'tcr-code' },
730
+ h('span', { className: 'tcr-code-text tcr-address' }, pairing.address),
731
+ h('div', { style: { display: 'flex', gap: 6 } },
732
+ h(CopyButton, { text: pairing.address, t, onResult: reportCopy }),
733
+ h(Button, { variant: 'ghost', onClick: () => setShowQr((value) => !value), 'aria-pressed': showQr }, showQr ? t('hideQr') : t('showQr')),
734
+ ),
735
+ ),
736
+ showQr
737
+ ? h('div', { className: 'tcr-qr-wrap' },
738
+ pairing.qr
739
+ ? h(QrCode, { matrix: pairing.qr, t })
740
+ : h('p', { className: 'tcr-hint', style: { marginTop: 0 } }, t('qrUnavailable')),
741
+ h('div', { className: 'tcr-hint', style: { marginTop: 0 } }, t('stepRun')),
742
+ )
743
+ : null,
744
+ h('label', { className: 'tcr-label', style: { marginTop: 16 } }, t('commandLabel')),
745
+ h(
746
+ 'div',
747
+ { className: 'tcr-code' },
748
+ h('span', { className: 'tcr-code-text' }, command),
749
+ h(CopyButton, { text: command, t, onResult: reportCopy }),
750
+ ),
751
+ h('p', { className: 'tcr-hint' }, t('commandHint', { localPort: pairing.localPort, port: pairing.port })),
752
+ h('ol', { className: 'tcr-steps' },
753
+ h('li', null, t('stepInstall')),
754
+ h('li', null, t('stepRun')),
755
+ h('li', null, t('stepOpen', { localPort: pairing.localPort })),
756
+ ),
757
+ h('label', { className: 'tcr-label', style: { marginTop: 16 } }, t('openLocal')),
758
+ h(
759
+ 'div',
760
+ { className: 'tcr-code' },
761
+ h('span', { className: 'tcr-code-text' }, localUrl),
762
+ h(CopyButton, { text: localUrl, t, onResult: reportCopy }),
763
+ h(
764
+ 'a',
765
+ { className: 'tcr-btn tcr-btn-ghost', href: localUrl, target: '_blank', rel: 'noopener noreferrer', style: { textDecoration: 'none' } },
766
+ t('openLocal'),
767
+ ),
768
+ ),
769
+ h('p', { className: 'tcr-hint' }, t('localUrlHint')),
770
+ )
771
+ : null,
772
+
773
+ h(
774
+ Card,
775
+ { title: t('securityTitle') },
776
+ snapshot.devices.enforced
777
+ ? h(Alert, { tone: 'info', title: t('allowOnTitle', { count: snapshot.devices.count }) }, t('allowOnBody'))
778
+ : h(Alert, { tone: 'danger', title: t('allowOffTitle') }, t('allowOffBody')),
779
+ h('label', { className: 'tcr-label', style: { marginTop: 16 } }, t('devicesLabel')),
780
+ h('textarea', {
781
+ className: 'tcr-textarea',
782
+ value: draft,
783
+ placeholder: t('devicesPlaceholder'),
784
+ spellCheck: false,
785
+ onChange: (event) => {
786
+ setDraft(event.target.value);
787
+ setDevicesDirty(true);
788
+ },
789
+ }),
790
+ h('p', { className: 'tcr-hint' }, t('devicesHint')),
791
+ h('p', { className: 'tcr-hint' }, t('devicesGenerate'), h('code', { className: 'tcr-mono' }, ' tailcat genkey --client --key=client-default')),
792
+ h(
793
+ 'div',
794
+ { className: 'tcr-actions' },
795
+ h(Button, { variant: 'primary', disabled: saveBusy || !devicesDirty, onClick: () => void saveDevices() }, saveBusy ? t('busySave') : t('devicesSave')),
796
+ h(Button, { variant: 'ghost', disabled: saveBusy || !devicesDirty, onClick: () => { setDevicesDirty(false); setDraft((snapshot.devices.allow ?? []).join('\n')); } }, t('devicesReset')),
797
+ ),
798
+ snapshot.devices.warnings?.length
799
+ ? h('ul', { className: 'tcr-warning-list' }, snapshot.devices.warnings.map((warning, index) => h('li', { key: index }, warning)))
800
+ : null,
801
+ h('div', { style: { marginTop: 20, paddingTop: 16, borderTop: '1px dashed var(--dsw-alias-border-l3)' } },
802
+ h('p', { className: 'tcr-card-title', style: { fontSize: 14 } }, t('rotate')),
803
+ h('p', { className: 'tcr-hint' }, t('rotateHint')),
804
+ h(
805
+ 'div',
806
+ { className: 'tcr-actions' },
807
+ rotateArmed
808
+ ? h(Button, { variant: 'danger', disabled: Boolean(busy), onClick: () => void rotate() }, t('rotateConfirm'))
809
+ : h(Button, { variant: 'danger', disabled: Boolean(busy), onClick: () => setRotateArmed(true) }, t('rotate')),
810
+ rotateArmed
811
+ ? h(Button, { variant: 'ghost', disabled: Boolean(busy), onClick: () => setRotateArmed(false) }, t('cancel'))
812
+ : null,
813
+ ),
814
+ h('p', { className: 'tcr-hint' }, t('rotateCaveat')),
815
+ h('p', { className: 'tcr-hint' }, t('rotateDisconnect')),
816
+ ),
817
+ ),
818
+
819
+ h(
820
+ Card,
821
+ { title: t('diagnosticsTitle') },
822
+ h(
823
+ 'div',
824
+ { className: 'tcr-grid' },
825
+ h(Row, { label: t('diagPhase') }, t(PHASE_KEY[phase] ?? 'statusUnknown')),
826
+ h(Row, { label: t('diagAddressMode') }, tailcat.addressMode === 'ephemeral' ? t('modeEphemeral') : t('modeStable')),
827
+ h(Row, { label: t('diagKeyName') }, h('span', { className: 'tcr-mono' }, tailcat.keyName)),
828
+ h(Row, { label: t('diagVersion') }, tailcat.binary?.version ?? t('unknown')),
829
+ h(Row, { label: t('diagBinary') }, h('span', { className: 'tcr-mono' }, tailcat.binary?.resolved ?? snapshot.config.binaryPath)),
830
+ h(Row, { label: t('diagRegion') }, tailcat.region ? `${tailcat.region.name} (${tailcat.region.id})` : t('unknown')),
831
+ h(Row, { label: t('diagRestarts') }, String(tailcat.restarts ?? 0)),
832
+ h(Row, { label: t('diagDoor') }, snapshot.door.mode === 'socket' ? `${t('modeSocket')} · ${snapshot.door.socketPath}` : `${t('modeTcp')} · 127.0.0.1:${snapshot.door.port}`),
833
+ h(Row, { label: t('diagTarget') }, snapshot.door.target ? `${snapshot.door.target.host}:${snapshot.door.target.port}` : t('unknown')),
834
+ h(Row, { label: t('diagLastError') }, tailcat.lastError ?? t('never')),
835
+ ),
836
+ tailcat.warnings?.length
837
+ ? h('ul', { className: 'tcr-warning-list' }, tailcat.warnings.map((warning, index) => h('li', { key: index }, warning)))
838
+ : null,
839
+ ),
840
+
841
+ h(
842
+ Card,
843
+ { title: t('recentTitle') },
844
+ tailcat.recent?.length
845
+ ? h(
846
+ 'div',
847
+ { className: 'tcr-recent' },
848
+ h(
849
+ 'div',
850
+ { className: 'tcr-recent-row tcr-recent-head' },
851
+ h('span', null, t('recentPeer')),
852
+ h('span', null, t('recentPort')),
853
+ h('span', null, t('recentVia')),
854
+ h('span', null, t('recentDuration')),
855
+ h('span', null, t('recentTime')),
856
+ ),
857
+ ...tailcat.recent.slice(0, 8).map((entry, index) =>
858
+ h(
859
+ 'div',
860
+ { className: 'tcr-recent-row', key: `${entry.at}-${index}` },
861
+ h('span', { className: 'tcr-mono', title: entry.peer ?? entry.reason ?? '' }, entry.peer ?? t('unknown')),
862
+ h('span', null, entry.port ?? '—'),
863
+ h('span', null, entry.via ?? entry.reason ?? '—'),
864
+ h('span', null, formatDuration(entry.durationMs)),
865
+ h('span', { title: entry.at }, formatTime(entry.at)),
866
+ ),
867
+ ),
868
+ )
869
+ : h('div', { className: 'tcr-empty' }, t('recentEmpty')),
870
+ ),
871
+
872
+ h(
873
+ 'div',
874
+ { className: 'tcr-footer' },
875
+ h('span', null, `${install} · v${snapshot.plugin.version}`),
876
+ h('span', null, t('footerNote')),
877
+ ),
878
+ ),
879
+ );
880
+ }
881
+
882
+ /**
883
+ * @param {string} key - translation key.
884
+ * @param {object} [params] - named placeholders.
885
+ * @returns {string} translated copy.
886
+ */
887
+ function translate(t, key, params) {
888
+ return params ? t(key, params) : t(key);
889
+ }
890
+
891
+ /**
892
+ * Copy text through the async clipboard API, with a legacy fallback for
893
+ * browsers that only allow copy from a user-gesture-owned textarea.
894
+ * @param {string} text - payload.
895
+ * @returns {Promise<boolean>} whether the copy succeeded.
896
+ */
897
+ async function copyText(text) {
898
+ try {
899
+ await navigator.clipboard.writeText(text);
900
+ return true;
901
+ } catch {
902
+ try {
903
+ const area = document.createElement('textarea');
904
+ area.value = text;
905
+ area.style.position = 'fixed';
906
+ area.style.top = '-1000px';
907
+ area.style.opacity = '0';
908
+ document.body.appendChild(area);
909
+ area.select();
910
+ const ok = document.execCommand('copy');
911
+ area.remove();
912
+ return ok;
913
+ } catch {
914
+ return false;
915
+ }
916
+ }
917
+ }
918
+
919
+ /**
920
+ * Turn a server QR matrix into one crisp SVG path plus quiet zone.
921
+ * @param {{ size: number, rows: string[] }} matrix - matrix from the Host.
922
+ * @param {number} [quietZone] - white border in modules.
923
+ * @returns {{ viewBox: string, path: string }} geometry.
924
+ */
925
+ function qrGeometry(matrix, quietZone = 2) {
926
+ const total = matrix.size + quietZone * 2;
927
+ let path = '';
928
+ for (let row = 0; row < matrix.rows.length; row += 1) {
929
+ const bits = matrix.rows[row];
930
+ let column = 0;
931
+ while (column < bits.length) {
932
+ if (bits[column] !== '1') {
933
+ column += 1;
934
+ continue;
935
+ }
936
+ const start = column;
937
+ while (column < bits.length && bits[column] === '1') column += 1;
938
+ path += `M${start + quietZone} ${row + quietZone}h${column - start}v1h-${column - start}z`;
939
+ }
940
+ }
941
+ return { viewBox: `0 0 ${total} ${total}`, path };
942
+ }
943
+
944
+ /**
945
+ * @param {number | null | undefined} ms - duration.
946
+ * @returns {string} compact duration.
947
+ */
948
+ function formatDuration(ms) {
949
+ if (typeof ms !== 'number' || !Number.isFinite(ms)) return '—';
950
+ if (ms < 1_000) return `${Math.round(ms)} ms`;
951
+ if (ms < 60_000) return `${(ms / 1_000).toFixed(1)} s`;
952
+ return `${Math.floor(ms / 60_000)}m ${Math.round((ms % 60_000) / 1_000)}s`;
953
+ }
954
+
955
+ /**
956
+ * @param {string | null | undefined} iso - timestamp.
957
+ * @returns {string} local clock time.
958
+ */
959
+ function formatTime(iso) {
960
+ if (!iso) return '—';
961
+ try {
962
+ return new Intl.DateTimeFormat(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit' }).format(new Date(iso));
963
+ } catch {
964
+ return String(iso);
965
+ }
966
+ }
967
+
968
+ return {
969
+ inject: ['slots', 'locale'],
970
+ apply(ctx) {
971
+ const t = ctx.locale.bind(NS);
972
+ ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'tailcat-remote: dictionaries');
973
+ ctx.effect(
974
+ () =>
975
+ ctx.slots.inject('sidebar.panellist', () =>
976
+ ctx.slots.register(
977
+ {
978
+ name: 'sidebar.panellist',
979
+ id: 'tailcat-remote',
980
+ order: 30,
981
+ label: () => translate(t, 'nav'),
982
+ },
983
+ PanelIcon,
984
+ ),
985
+ ),
986
+ 'tailcat-remote: sidebar entry',
987
+ );
988
+ ctx.effect(
989
+ () =>
990
+ ctx.slots.inject('main', () =>
991
+ ctx.slots.register(
992
+ {
993
+ name: 'main',
994
+ key: 'tailcat-remote',
995
+ locale: NS,
996
+ },
997
+ RemotePage,
998
+ ),
999
+ ),
1000
+ 'tailcat-remote: page',
1001
+ );
1002
+ },
1003
+ };
1004
+ },
1005
+ });