@deep-podder/dsh-remote-mobile 0.0.0-stage → 0.0.5

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,687 @@
1
+ // Client half of dsh-remote-mobile (browser module of the DSH Web UI).
2
+ // Renders the pairing panel inside Settings: QR, fallback PIN, address,
3
+ // expiry countdown and paired-device management. All data comes from the Host
4
+ // server over a key-gated loopback endpoint.
5
+ window.__ModuleLoader__.load({
6
+ id: 'dsh-remote-mobile',
7
+ factory(require) {
8
+ const React = require('react')
9
+ const h = React.createElement
10
+ const { useCallback, useEffect, useMemo, useRef, useState } = React
11
+
12
+ const styles = {
13
+ page: {
14
+ display: 'flex',
15
+ flexDirection: 'column',
16
+ gap: '18px',
17
+ maxWidth: '720px',
18
+ color: 'var(--dsw-alias-label-primary)',
19
+ },
20
+ header: {
21
+ display: 'flex',
22
+ flexDirection: 'column',
23
+ gap: '4px',
24
+ },
25
+ title: {
26
+ margin: 0,
27
+ fontSize: '18px',
28
+ fontWeight: 600,
29
+ letterSpacing: '-0.01em',
30
+ },
31
+ subtitle: {
32
+ margin: 0,
33
+ fontSize: '13px',
34
+ lineHeight: 1.5,
35
+ color: 'var(--dsw-alias-label-secondary)',
36
+ },
37
+ card: {
38
+ display: 'flex',
39
+ gap: '24px',
40
+ flexWrap: 'wrap',
41
+ padding: '20px',
42
+ borderRadius: '14px',
43
+ background: 'var(--dsw-alias-bg-layer-1)',
44
+ border: '1px solid var(--dsw-alias-border-l1)',
45
+ },
46
+ qrWrap: {
47
+ width: '196px',
48
+ height: '196px',
49
+ padding: '10px',
50
+ borderRadius: '12px',
51
+ background: '#FFFFFF',
52
+ boxSizing: 'border-box',
53
+ flex: '0 0 auto',
54
+ },
55
+ qrImg: { width: '100%', height: '100%', display: 'block' },
56
+ details: {
57
+ display: 'flex',
58
+ flex: '1 1 260px',
59
+ minWidth: '240px',
60
+ flexDirection: 'column',
61
+ gap: '14px',
62
+ },
63
+ label: {
64
+ fontSize: '11px',
65
+ fontWeight: 600,
66
+ letterSpacing: '0.08em',
67
+ textTransform: 'uppercase',
68
+ color: 'var(--dsw-alias-label-secondary)',
69
+ },
70
+ pin: {
71
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
72
+ fontSize: '34px',
73
+ fontWeight: 600,
74
+ letterSpacing: '0.28em',
75
+ lineHeight: 1.1,
76
+ margin: 0,
77
+ color: 'var(--dsw-alias-label-primary)',
78
+ },
79
+ address: {
80
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
81
+ fontSize: '12px',
82
+ lineHeight: 1.5,
83
+ wordBreak: 'break-all',
84
+ margin: 0,
85
+ color: 'var(--dsw-alias-label-secondary)',
86
+ },
87
+ row: { display: 'flex', gap: '8px', flexWrap: 'wrap', alignItems: 'center' },
88
+ button: {
89
+ appearance: 'none',
90
+ border: '1px solid var(--dsw-alias-border-l2)',
91
+ background: 'var(--dsw-alias-bg-layer-2)',
92
+ color: 'var(--dsw-alias-label-primary)',
93
+ borderRadius: '8px',
94
+ padding: '7px 12px',
95
+ fontSize: '12px',
96
+ fontWeight: 500,
97
+ cursor: 'pointer',
98
+ lineHeight: 1.4,
99
+ },
100
+ muted: {
101
+ appearance: 'none',
102
+ border: 'none',
103
+ background: 'transparent',
104
+ color: 'var(--dsw-alias-label-secondary)',
105
+ fontSize: '12px',
106
+ cursor: 'pointer',
107
+ padding: 0,
108
+ textDecoration: 'underline',
109
+ textUnderlineOffset: '2px',
110
+ },
111
+ meta: { fontSize: '12px', color: 'var(--dsw-alias-label-secondary)' },
112
+ banner: {
113
+ padding: '10px 12px',
114
+ borderRadius: '10px',
115
+ fontSize: '12px',
116
+ lineHeight: 1.5,
117
+ border: '1px solid var(--dsw-alias-border-l1)',
118
+ background: 'var(--dsw-alias-bg-layer-2)',
119
+ color: 'var(--dsw-alias-label-secondary)',
120
+ },
121
+ warn: {
122
+ padding: '10px 12px',
123
+ borderRadius: '10px',
124
+ fontSize: '12px',
125
+ lineHeight: 1.5,
126
+ border: '1px solid var(--dsw-alias-state-warn-primary)',
127
+ background: 'var(--dsw-alias-bg-layer-2)',
128
+ color: 'var(--dsw-alias-state-warn-primary)',
129
+ },
130
+ error: {
131
+ padding: '10px 12px',
132
+ borderRadius: '10px',
133
+ fontSize: '12px',
134
+ lineHeight: 1.5,
135
+ border: '1px solid var(--dsw-alias-state-error-primary)',
136
+ color: 'var(--dsw-alias-state-error-primary)',
137
+ background: 'var(--dsw-alias-bg-layer-2)',
138
+ },
139
+ devices: {
140
+ display: 'flex',
141
+ flexDirection: 'column',
142
+ gap: '8px',
143
+ },
144
+ device: {
145
+ display: 'flex',
146
+ alignItems: 'center',
147
+ gap: '10px',
148
+ padding: '10px 12px',
149
+ borderRadius: '10px',
150
+ background: 'var(--dsw-alias-bg-layer-2)',
151
+ border: '1px solid var(--dsw-alias-border-l1)',
152
+ },
153
+ dot: {
154
+ width: '7px',
155
+ height: '7px',
156
+ borderRadius: '50%',
157
+ background: 'var(--dsw-alias-state-success-primary)',
158
+ flex: '0 0 auto',
159
+ },
160
+ spacer: { flex: '1 1 auto' },
161
+ toggleRow: {
162
+ display: 'flex',
163
+ alignItems: 'center',
164
+ gap: '14px',
165
+ padding: '12px 14px',
166
+ borderRadius: '12px',
167
+ border: '1px solid var(--dsw-alias-border-l1)',
168
+ background: 'var(--dsw-alias-bg-layer-1)',
169
+ },
170
+ confirmRow: {
171
+ display: 'flex',
172
+ alignItems: 'center',
173
+ gap: '12px',
174
+ flexWrap: 'wrap',
175
+ padding: '12px 14px',
176
+ borderRadius: '12px',
177
+ border: '1px solid var(--dsw-alias-state-warn-primary)',
178
+ background: 'var(--dsw-alias-bg-layer-1)',
179
+ color: 'var(--dsw-alias-label-primary)',
180
+ lineHeight: 1.5,
181
+ },
182
+ }
183
+
184
+ function switchStyle(on) {
185
+ return {
186
+ position: 'relative',
187
+ width: '42px',
188
+ height: '24px',
189
+ flex: '0 0 auto',
190
+ padding: 0,
191
+ border: 'none',
192
+ borderRadius: '999px',
193
+ cursor: 'pointer',
194
+ background: on ? 'var(--dsw-alias-state-success-primary)' : 'var(--dsw-alias-border-l2)',
195
+ transition: 'background 160ms ease',
196
+ }
197
+ }
198
+
199
+ function switchKnobStyle(on) {
200
+ return {
201
+ position: 'absolute',
202
+ top: '3px',
203
+ left: on ? '21px' : '3px',
204
+ width: '18px',
205
+ height: '18px',
206
+ borderRadius: '50%',
207
+ background: '#FFFFFF',
208
+ transition: 'left 160ms ease',
209
+ boxShadow: '0 1px 2px rgba(0, 0, 0, 0.28)',
210
+ }
211
+ }
212
+
213
+ function formatRelative(ts) {
214
+ if (!Number.isFinite(ts)) return ''
215
+ const delta = Date.now() - ts
216
+ if (delta < 60_000) return 'just now'
217
+ if (delta < 3_600_000) return `${Math.floor(delta / 60_000)} min ago`
218
+ if (delta < 86_400_000) return `${Math.floor(delta / 3_600_000)} h ago`
219
+ return `${Math.floor(delta / 86_400_000)} d ago`
220
+ }
221
+
222
+ function formatCountdown(ms) {
223
+ if (!Number.isFinite(ms) || ms <= 0) return 'expired'
224
+ const seconds = Math.ceil(ms / 1000)
225
+ if (seconds < 60) return `${seconds}s`
226
+ return `${Math.floor(seconds / 60)}m ${String(seconds % 60).padStart(2, '0')}s`
227
+ }
228
+
229
+ function PairingPanel() {
230
+ const injected = typeof window !== 'undefined' ? window.__DSH_REMOTE_MOBILE__ : undefined
231
+ const bootstrap = injected !== undefined && typeof injected === 'object' && Number.isInteger(injected.port)
232
+ ? injected
233
+ : undefined
234
+ const localPage = typeof window !== 'undefined' && (
235
+ window.location.protocol === 'file:'
236
+ || window.location.hostname === '127.0.0.1'
237
+ || window.location.hostname === 'localhost'
238
+ || window.location.hostname === '::1'
239
+ || window.location.hostname === '[::1]'
240
+ )
241
+ const [status, setStatus] = useState(null)
242
+ const [error, setError] = useState(null)
243
+ const [busy, setBusy] = useState(false)
244
+ const [busyToggle, setBusyToggle] = useState(false)
245
+ const [confirmOff, setConfirmOff] = useState(false)
246
+ const [copied, setCopied] = useState(false)
247
+ const copiedTimer = useRef(null)
248
+ const [tick, setTick] = useState(0)
249
+ const [selectedAddress, setSelectedAddress] = useState(null)
250
+
251
+ // Try the injected keyed endpoint first, then the DSH web server's
252
+ // same-origin route (immune to page CSP and file:// origins), then the
253
+ // bare loopback endpoint. The first one that answers is reused.
254
+ const sources = useMemo(() => {
255
+ const list = []
256
+ if (bootstrap !== undefined) {
257
+ list.push({ base: `http://127.0.0.1:${bootstrap.port}`, key: bootstrap.key })
258
+ }
259
+ list.push({ base: '', key: null })
260
+ if (localPage) {
261
+ list.push({ base: `http://127.0.0.1:${bootstrap !== undefined ? bootstrap.port : 3456}`, key: null })
262
+ }
263
+ return list
264
+ }, [bootstrap, localPage])
265
+ const activeIndex = useRef(-1)
266
+
267
+ const opUrl = useCallback((source, op, query) => {
268
+ const base = source.base === ''
269
+ ? (op === 'status' ? '/remote-mobile/pairing' : `/remote-mobile/pairing/${op}`)
270
+ : (source.key
271
+ ? `${source.base}${op === 'status' ? '/api/desktop/status' : `/api/desktop/${op}`}?key=${encodeURIComponent(source.key)}`
272
+ : `${source.base}${op === 'status' ? '/api/desktop/status' : `/api/desktop/${op}`}`)
273
+ if (!query) return base
274
+ return `${base}${base.includes('?') ? '&' : '?'}${query}`
275
+ }, [])
276
+
277
+ const request = useCallback(async (op, options = {}) => {
278
+ const failures = []
279
+ const order = []
280
+ if (activeIndex.current >= 0) order.push(activeIndex.current)
281
+ for (let index = 0; index < sources.length; index += 1) {
282
+ if (!order.includes(index)) order.push(index)
283
+ }
284
+ const { query, ...init } = options
285
+ for (const index of order) {
286
+ const source = sources[index]
287
+ try {
288
+ const response = await fetch(opUrl(source, op, query), {
289
+ cache: 'no-store',
290
+ mode: 'cors',
291
+ credentials: 'omit',
292
+ ...init,
293
+ headers: init.body === undefined
294
+ ? undefined
295
+ : { 'content-type': 'application/json', ...(init.headers ?? {}) },
296
+ })
297
+ if (!response.ok) {
298
+ failures.push(`${source.base === '' ? 'same-origin' : source.base}: HTTP ${response.status}`)
299
+ continue
300
+ }
301
+ const data = await response.json()
302
+ activeIndex.current = index
303
+ return data
304
+ } catch (cause) {
305
+ failures.push(`${source.base === '' ? 'same-origin' : source.base}: ${cause instanceof Error ? cause.message : String(cause)}`)
306
+ }
307
+ }
308
+ throw new Error(`Could not reach the local pairing server (${failures.join('; ')})`)
309
+ }, [opUrl, sources])
310
+
311
+ const load = useCallback(async () => {
312
+ try {
313
+ const next = await request('status', selectedAddress === null ? {} : { query: `address=${encodeURIComponent(selectedAddress)}` })
314
+ setStatus(next)
315
+ setError(null)
316
+ if (next !== null && next.enabled === false) setConfirmOff(false)
317
+ } catch (cause) {
318
+ setError(cause instanceof Error ? cause.message : String(cause))
319
+ }
320
+ }, [request, selectedAddress])
321
+
322
+ useEffect(() => {
323
+ void load()
324
+ const poll = setInterval(() => void load(), 5000)
325
+ const clock = setInterval(() => setTick((value) => value + 1), 1000)
326
+ return () => {
327
+ clearInterval(poll)
328
+ clearInterval(clock)
329
+ if (copiedTimer.current !== null) clearTimeout(copiedTimer.current)
330
+ }
331
+ }, [load])
332
+
333
+ const rotate = useCallback(async () => {
334
+ setBusy(true)
335
+ try {
336
+ await request('rotate', { method: 'POST', body: '{}' })
337
+ await load()
338
+ } catch (cause) {
339
+ setError(cause instanceof Error ? cause.message : String(cause))
340
+ } finally {
341
+ setBusy(false)
342
+ }
343
+ }, [request, load])
344
+
345
+ const revoke = useCallback(async (deviceId) => {
346
+ setBusy(true)
347
+ try {
348
+ await request('revoke', { method: 'POST', body: JSON.stringify({ deviceId }) })
349
+ await load()
350
+ } catch (cause) {
351
+ setError(cause instanceof Error ? cause.message : String(cause))
352
+ } finally {
353
+ setBusy(false)
354
+ }
355
+ }, [request, load])
356
+
357
+ const revokeAll = useCallback(async () => {
358
+ setBusy(true)
359
+ try {
360
+ await request('revoke', { method: 'POST', body: JSON.stringify({ all: true }) })
361
+ await load()
362
+ } catch (cause) {
363
+ setError(cause instanceof Error ? cause.message : String(cause))
364
+ } finally {
365
+ setBusy(false)
366
+ }
367
+ }, [request, load])
368
+
369
+ const setDevicePermission = useCallback(async (deviceId, canApprove) => {
370
+ setBusy(true)
371
+ try {
372
+ await request('device', { method: 'POST', body: JSON.stringify({ deviceId, canApprove }) })
373
+ await load()
374
+ setError(null)
375
+ } catch (cause) {
376
+ setError(cause instanceof Error ? cause.message : String(cause))
377
+ } finally {
378
+ setBusy(false)
379
+ }
380
+ }, [request, load])
381
+
382
+ const copy = useCallback(async () => {
383
+ if (status === null || status.url === null) return
384
+ try {
385
+ await navigator.clipboard.writeText(status.url)
386
+ setCopied(true)
387
+ copiedTimer.current = setTimeout(() => {
388
+ copiedTimer.current = null
389
+ setCopied(false)
390
+ }, 1600)
391
+ } catch {
392
+ setError('Clipboard is not available in this browser.')
393
+ }
394
+ }, [status])
395
+
396
+ const toggleEnabled = useCallback(async (next) => {
397
+ setBusyToggle(true)
398
+ setConfirmOff(false)
399
+ try {
400
+ await request(next ? 'enable' : 'disable', { method: 'POST', body: '{}' })
401
+ await load()
402
+ setError(null)
403
+ } catch (cause) {
404
+ setError(cause instanceof Error ? cause.message : String(cause))
405
+ } finally {
406
+ setBusyToggle(false)
407
+ }
408
+ }, [request, load])
409
+
410
+ const pinRemaining = status === null || status.enabled === false ? 0 : status.pinExpiresAt - Date.now()
411
+
412
+ const deviceList = status !== null && status.devices.length > 0
413
+ ? h('div', { style: styles.devices }, status.devices.map((device) => h('div', { key: device.id, style: styles.device },
414
+ h('span', { style: styles.dot }),
415
+ h('div', null,
416
+ h('div', { style: { fontSize: '13px', fontWeight: 500 } }, device.name),
417
+ h('div', { style: styles.meta }, `Paired ${formatRelative(device.createdAt)} · last seen ${formatRelative(device.lastSeenAt)}`)),
418
+ h('span', { style: styles.spacer }),
419
+ h('button', {
420
+ type: 'button',
421
+ role: 'switch',
422
+ 'aria-checked': device.canApprove !== false,
423
+ 'aria-label': `Allow ${device.name} to answer approvals`,
424
+ title: device.canApprove !== false
425
+ ? 'This device can answer approvals'
426
+ : 'This device cannot answer approvals',
427
+ disabled: busy,
428
+ style: switchStyle(device.canApprove !== false),
429
+ onClick: () => setDevicePermission(device.id, !(device.canApprove !== false)),
430
+ }, h('span', { style: switchKnobStyle(device.canApprove !== false) })),
431
+ h('button', { type: 'button', style: styles.muted, disabled: busy, onClick: () => revoke(device.id) }, 'Revoke'))))
432
+ : h('div', { style: styles.banner }, 'No devices paired yet.')
433
+
434
+ const pairingCard = h('div', { style: styles.card },
435
+ h('div', { style: styles.qrWrap },
436
+ h('img', {
437
+ style: styles.qrImg,
438
+ alt: 'Pairing QR code',
439
+ src: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(status === null ? '' : status.qrSvg)}`,
440
+ })),
441
+ h('div', { style: styles.details },
442
+ h('div', null,
443
+ h('div', { style: styles.label }, 'PIN'),
444
+ h('p', { style: styles.pin, 'aria-label': `PIN ${status === null ? '' : status.pin}` }, status === null ? '' : status.pin)),
445
+ h('div', null,
446
+ h('div', { style: styles.label }, 'Address'),
447
+ h('p', { style: styles.address }, status === null ? '' : status.url),
448
+ status !== null && Array.isArray(status.addressDetails) && status.addressDetails.length > 1
449
+ ? h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '6px', marginTop: '6px' } },
450
+ status.addressDetails.map((entry) => h('button', {
451
+ key: entry.address,
452
+ type: 'button',
453
+ onClick: () => setSelectedAddress(entry.address),
454
+ 'aria-pressed': status.address === entry.address,
455
+ style: status.address === entry.address
456
+ ? { ...styles.button, borderColor: 'var(--dsw-alias-brand-primary)', color: 'var(--dsw-alias-brand-primary)' }
457
+ : { ...styles.button, fontSize: '11px', padding: '5px 8px' },
458
+ }, entry.address.startsWith('http') ? entry.name : `${entry.address}${entry.name ? ` · ${entry.name}` : ''}`)))
459
+ : null,
460
+ status !== null && Array.isArray(status.addresses) && status.addresses.some((address) => address.startsWith('100.') || address.startsWith('https://'))
461
+ ? h('p', { style: styles.meta }, 'Use the Tailscale address when the phone is away from this Wi-Fi. Open Tailscale on the phone first.')
462
+ : null),
463
+ h('div', { style: styles.row },
464
+ h('button', { type: 'button', style: styles.button, onClick: copy }, copied ? 'Copied' : 'Copy link'),
465
+ h('button', { type: 'button', style: styles.button, disabled: busy, onClick: rotate }, 'New PIN')),
466
+ h('div', { style: styles.meta }, `PIN expires in ${formatCountdown(pinRemaining)}`)))
467
+
468
+ const devicesSection = h('div', null,
469
+ h('div', { style: styles.label }, 'Paired devices'),
470
+ deviceList,
471
+ status !== null && status.devices.length > 0
472
+ ? h('div', { style: { marginTop: '12px' } },
473
+ h('button', { type: 'button', style: styles.muted, disabled: busy, onClick: revokeAll }, 'Revoke all devices'))
474
+ : null)
475
+
476
+ const body = status === null
477
+ ? h('div', { style: styles.banner }, 'Starting the local mobile server…')
478
+ : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '18px' } },
479
+ status.enabled === false
480
+ ? h('div', { style: styles.banner }, 'Remote access is off. The phone cannot pair or connect while the switch is off.')
481
+ : pairingCard,
482
+ status.enabled === false
483
+ ? null
484
+ : status.loopbackOnly
485
+ ? h('div', { style: styles.warn }, 'Only a loopback address was detected. A phone cannot reach this machine until it has a LAN or Tailscale address.')
486
+ : null,
487
+ devicesSection)
488
+
489
+ const connectedNames = status !== null && Array.isArray(status.connectedNames) ? status.connectedNames : []
490
+ const connectedCount = status !== null && Number.isFinite(status.connectedDevices) ? status.connectedDevices : 0
491
+ const lastConnectedLabel = status === null || !status.lastConnectedAt ? 'never' : formatRelative(status.lastConnectedAt)
492
+ const toggleSubtitle = status === null
493
+ ? 'Checking…'
494
+ : status.enabled === false
495
+ ? `Off · last connected ${lastConnectedLabel}`
496
+ : connectedCount > 0
497
+ ? `${connectedNames.length > 0 ? connectedNames.join(', ') : 'A device'} connected now`
498
+ : `Last connected ${lastConnectedLabel}`
499
+
500
+ return h('div', { style: styles.page },
501
+ h('div', { style: styles.header },
502
+ h('h2', { style: styles.title }, 'Remote mobile access'),
503
+ h('p', { style: styles.subtitle }, 'Scan the QR code with your phone, or open the address and enter the PIN. Devices must be on the same network — preferably Tailscale or a trusted Wi-Fi.')),
504
+ h('div', { style: styles.toggleRow },
505
+ h('div', { style: { flex: '1 1 auto' } },
506
+ h('div', { style: { fontSize: '13px', fontWeight: 600 } },
507
+ status !== null && status.enabled === false ? 'Remote access is off' : 'Remote access is on'),
508
+ h('div', { style: styles.meta }, toggleSubtitle)),
509
+ h('button', {
510
+ type: 'button',
511
+ role: 'switch',
512
+ 'aria-checked': status !== null && status.enabled !== false,
513
+ 'aria-label': 'Enable remote mobile access',
514
+ disabled: busyToggle || status === null,
515
+ onClick: () => {
516
+ if (status === null) return
517
+ if (status.enabled === false) {
518
+ void toggleEnabled(true)
519
+ return
520
+ }
521
+ if (connectedCount > 0) {
522
+ setConfirmOff(true)
523
+ return
524
+ }
525
+ void toggleEnabled(false)
526
+ },
527
+ style: switchStyle(status !== null && status.enabled !== false),
528
+ }, h('span', { style: switchKnobStyle(status !== null && status.enabled !== false) }))),
529
+ confirmOff && status !== null && status.enabled !== false
530
+ ? h('div', { style: styles.confirmRow },
531
+ h('div', { style: { flex: '1 1 240px', fontSize: '12px' } },
532
+ `${connectedNames.length > 0 ? connectedNames.join(', ') : 'A phone'} is connected. Turning access off disconnects it until you turn it back on.`),
533
+ h('button', {
534
+ type: 'button',
535
+ style: styles.button,
536
+ disabled: busyToggle,
537
+ onClick: () => void toggleEnabled(false),
538
+ }, 'Turn off'),
539
+ h('button', {
540
+ type: 'button',
541
+ style: styles.muted,
542
+ onClick: () => setConfirmOff(false),
543
+ }, 'Cancel'))
544
+ : null,
545
+ error !== null ? h('div', { style: styles.error }, error) : null,
546
+ body)
547
+ }
548
+
549
+ /**
550
+ * Desktop approval dock. Both the phone and the desktop show the same
551
+ * pending approvals; whichever answers first settles the request, and the
552
+ * dock disappears on the next poll. It only appears while a phone is
553
+ * connected (otherwise the shipped desktop approval UI owns the request).
554
+ */
555
+ function ApprovalDock() {
556
+ const [approvals, setApprovals] = useState([])
557
+ const [busyId, setBusyId] = useState(null)
558
+
559
+ const load = useCallback(async () => {
560
+ const sources = ['/remote-mobile/pairing/pending', 'http://127.0.0.1:3456/api/desktop/pending']
561
+ for (const url of sources) {
562
+ try {
563
+ const response = await fetch(url, {
564
+ cache: 'no-store',
565
+ credentials: 'omit',
566
+ mode: url.startsWith('http') ? 'cors' : 'same-origin',
567
+ })
568
+ if (!response.ok) continue
569
+ const data = await response.json()
570
+ setApprovals(Array.isArray(data && data.approvals) ? data.approvals : [])
571
+ return
572
+ } catch {
573
+ // try the next source
574
+ }
575
+ }
576
+ }, [])
577
+
578
+ useEffect(() => {
579
+ void load()
580
+ const timer = setInterval(() => void load(), 1500)
581
+ return () => clearInterval(timer)
582
+ }, [load])
583
+
584
+ const answer = useCallback(async (requestId, decision) => {
585
+ setBusyId(requestId)
586
+ try {
587
+ const sources = ['/remote-mobile/pairing/answer', 'http://127.0.0.1:3456/api/desktop/answer']
588
+ for (const url of sources) {
589
+ try {
590
+ const response = await fetch(url, {
591
+ method: 'POST',
592
+ headers: { 'content-type': 'application/json' },
593
+ body: JSON.stringify({ requestId, decision }),
594
+ cache: 'no-store',
595
+ credentials: 'omit',
596
+ mode: url.startsWith('http') ? 'cors' : 'same-origin',
597
+ })
598
+ if (!response.ok) continue
599
+ setApprovals((previous) => previous.filter((item) => item.requestId !== requestId))
600
+ return
601
+ } catch {
602
+ // try the next source
603
+ }
604
+ }
605
+ } catch {
606
+ // Both sources refused; the approval dock stays available for a retry.
607
+ } finally {
608
+ setBusyId(null)
609
+ }
610
+ }, [])
611
+
612
+ if (approvals.length === 0) return null
613
+ const item = approvals[0]
614
+ const reason = item.displayReason || item.reason || ''
615
+ const card = {
616
+ position: 'fixed',
617
+ right: '20px',
618
+ bottom: '20px',
619
+ zIndex: 60,
620
+ width: 'min(420px, calc(100vw - 40px))',
621
+ display: 'flex',
622
+ flexDirection: 'column',
623
+ gap: '10px',
624
+ padding: '14px 16px',
625
+ borderRadius: '14px',
626
+ background: 'var(--dsw-alias-bg-overlay)',
627
+ border: '1px solid var(--dsw-alias-border-l2)',
628
+ boxShadow: '0 12px 32px rgba(0, 0, 0, 0.28)',
629
+ color: 'var(--dsw-alias-label-primary)',
630
+ }
631
+ const eyebrow = {
632
+ display: 'flex',
633
+ alignItems: 'center',
634
+ gap: '8px',
635
+ fontSize: '11px',
636
+ fontWeight: 600,
637
+ letterSpacing: '0.08em',
638
+ textTransform: 'uppercase',
639
+ color: 'var(--dsw-alias-state-warn-primary)',
640
+ }
641
+ const tool = {
642
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
643
+ fontSize: '13px',
644
+ fontWeight: 600,
645
+ }
646
+ const body = { margin: 0, fontSize: '12px', lineHeight: 1.5, color: 'var(--dsw-alias-label-secondary)' }
647
+ const actions = { display: 'flex', justifyContent: 'flex-end', gap: '8px' }
648
+ const button = {
649
+ appearance: 'none',
650
+ borderRadius: '8px',
651
+ padding: '7px 12px',
652
+ fontSize: '12px',
653
+ fontWeight: 500,
654
+ cursor: 'pointer',
655
+ border: '1px solid var(--dsw-alias-border-l2)',
656
+ background: 'var(--dsw-alias-bg-layer-2)',
657
+ color: 'var(--dsw-alias-label-primary)',
658
+ }
659
+ const primary = { ...button, background: 'var(--dsw-alias-label-primary)', borderColor: 'transparent', color: 'var(--dsw-alias-bg-base)', fontWeight: 600 }
660
+ const busy = busyId === item.requestId
661
+ return h('div', { style: card, role: 'status', 'aria-live': 'polite' },
662
+ h('div', { style: eyebrow }, `Approval needed${approvals.length > 1 ? ` · ${approvals.length}` : ''}`),
663
+ h('div', { style: tool }, item.toolName || 'tool'),
664
+ reason.length > 0 ? h('p', { style: body }, reason) : null,
665
+ h('div', { style: actions },
666
+ h('button', { type: 'button', style: button, disabled: busy, onClick: () => void answer(item.requestId, 'reject') }, busy ? 'Sending…' : 'Reject'),
667
+ h('button', { type: 'button', style: primary, disabled: busy, onClick: () => void answer(item.requestId, 'allow') }, busy ? 'Sending…' : 'Approve')))
668
+ }
669
+
670
+ return {
671
+ inject: ['slots'],
672
+ apply(ctx) {
673
+ ctx.slots.inject('settings.section', () => ctx.slots.register({
674
+ name: 'settings.section',
675
+ id: 'remote-mobile',
676
+ order: 60,
677
+ label: 'Remote mobile access',
678
+ }, PairingPanel))
679
+ ctx.slots.inject('shell.overlay', () => ctx.slots.register({
680
+ name: 'shell.overlay',
681
+ id: 'remote-mobile-approvals',
682
+ order: 20,
683
+ }, ApprovalDock))
684
+ },
685
+ }
686
+ },
687
+ })
@@ -0,0 +1,7 @@
1
+ - insert:
2
+ - id: dsh-remote-mobile
3
+ name: '@deep-podder/dsh-remote-mobile'
4
+ config:
5
+ enabled: true
6
+ host: '0.0.0.0'
7
+ port: 3456