From cc00ec39c7dbe5eefbd4d7fc9370cc168c2c540a Mon Sep 17 00:00:00 2001 From: bonamin Date: Fri, 13 Nov 2026 23:22:43 +0200 Subject: [PATCH] =?UTF-8?q?feat(manager):=20Devices=20page=20=E2=80=94=20p?= =?UTF-8?q?air=20by=20code,=20app,=20name,=20sign-in=20mode,=20link,=20cha?= =?UTF-8?q?nge=20app,=20remove?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Platform-v2 step 2.6d: - /devices (core.devices.manage, sidebar): devices from /api/devices with status (online / last seen / waiting for the device / app turned off / removed), app, sign-in mode (own or venue default), linked device; usage per device app against the license limit (devices) - pair dialog: code as typed (normalized), app (only apps that are on, "all in use" when the limit is reached), name, sign-in mode for apps with a choice, optional linked device; license.limit_reached explained with the app's limit - edit (name, sign-in mode, link), change app (signs out whoever is on it), remove with confirm (the device goes back to pairing), show removed - manager_dashboard gets node --test (tests/platform.test.mjs: module states, dependents, device status/usage/room/links), added to run-all - e2e manager.mjs: 54 checks (+ wrong code, normalized code, pair, the device picks up its token, online, rename, change app, remove → device.revoked) Co-Authored-By: Claude Opus 5.5 --- docs/README.md | 2 +- manager_dashboard/package.json | 3 +- manager_dashboard/src/App.jsx | 2 + .../src/pages/platform/DevicesPage.jsx | 327 ++++++++++++++++++ .../src/pages/platform/devicesLogic.js | 55 +++ manager_dashboard/src/shell/nav.js | 3 +- manager_dashboard/tests/platform.test.mjs | 64 ++++ packages/i18n/locales/el/manager.json | 62 +++- packages/i18n/locales/en/manager.json | 62 +++- tests/e2e/README.md | 1 + tests/e2e/manager.mjs | 61 ++++ tests/e2e/run-all.mjs | 1 + 12 files changed, 638 insertions(+), 5 deletions(-) create mode 100644 manager_dashboard/src/pages/platform/DevicesPage.jsx create mode 100644 manager_dashboard/src/pages/platform/devicesLogic.js create mode 100644 manager_dashboard/tests/platform.test.mjs diff --git a/docs/README.md b/docs/README.md index 623ca01..f3c5e34 100644 --- a/docs/README.md +++ b/docs/README.md @@ -148,7 +148,7 @@ The native plugins (`@capacitor/*`, `@capgo/*`) are pinned to **exact** versions ## Testing Automated tests cover the native-app/LAN work and licensing. See **[tests/e2e/README.md](../tests/e2e/README.md)**. -- **Unit (seconds):** `python -m pytest tests -q` in `local_backend/`, `npm test` in `waiter_pwa/`, and `npm test -w @xenia/` at the workspace root. +- **Unit (seconds):** `python -m pytest tests -q` in `local_backend/`, `npm test` in `waiter_pwa/`, and `npm test -w <@xenia/ui|@xenia/i18n|@xenia/api|@xenia/access|manager-dashboard>` at the workspace root. - **End-to-end:** `node run-all.mjs` in `tests/e2e/`. That covers install.sh, licensing with a real cloud, browser modes, and the Android emulator suites. `--no-android` skips the emulator. **CS-11. Tests follow the code.** When you change something a test covers (licensing, LAN IP, TLS, pairing, rediscovery, bundles, install.sh, the proxy config), run its suite and update the test in the same commit. Run `node run-all.mjs` before every release. diff --git a/manager_dashboard/package.json b/manager_dashboard/package.json index 919376d..4eccdae 100644 --- a/manager_dashboard/package.json +++ b/manager_dashboard/package.json @@ -6,7 +6,8 @@ "scripts": { "dev": "vite", "build": "vite build", - "preview": "vite preview" + "preview": "vite preview", + "test": "node --test \"tests/*.test.mjs\"" }, "dependencies": { "@tanstack/react-query": "^5.62.0", diff --git a/manager_dashboard/src/App.jsx b/manager_dashboard/src/App.jsx index f09d2b7..3994d67 100644 --- a/manager_dashboard/src/App.jsx +++ b/manager_dashboard/src/App.jsx @@ -53,6 +53,7 @@ import MiscPage from './pages/reports/MiscPage' // Other import { AppsPage, IntegrationsPage } from './pages/platform/ModulesPage' import LicensePage from './pages/platform/LicensePage' +import DevicesPage from './pages/platform/DevicesPage' import PhonePage from './pages/PhonePage' import SettingsPage from './pages/Settings/SettingsPage' import NotesPage from './pages/NotesPage' @@ -181,6 +182,7 @@ export default function App() { } /> } /> } /> + } /> } /> } /> } /> diff --git a/manager_dashboard/src/pages/platform/DevicesPage.jsx b/manager_dashboard/src/pages/platform/DevicesPage.jsx new file mode 100644 index 0000000..28e4352 --- /dev/null +++ b/manager_dashboard/src/pages/platform/DevicesPage.jsx @@ -0,0 +1,327 @@ +// Devices page (plan step 2.6d, APPS_PLATFORM D-8, §6.3): screens and terminals paired +// with the venue, each running one app. Backend: /api/devices (step 1.8, core.devices.manage). +// - Pair: the device shows a 6-character code (POST /api/devices/pair-request on its side); +// the manager enters it here with the app, a name, how staff sign in on it and an optional +// linked device → POST /api/devices. The license limit per app is shown and enforced +// (license.limit_reached). +// - Edit (name, sign-in mode, link) → PATCH; change app → POST /reassign (signs out whoever +// is on it); remove → POST /revoke (the device goes back to its pairing screen). +// Per-app settings (e.g. a KDS's prep zone) arrive with the apps themselves in phase 3. +import { useCallback, useEffect, useMemo, useState } from 'react' +import toast from 'react-hot-toast' +import { MonitorSmartphone, Plus } from 'lucide-react' +import { errorMessage, parseError } from '@xenia/api' +import { useTranslation, useFormat } from '@xenia/i18n' +import { Button, Dialog, EmptyState, Field, Input, Select, Spinner } from '@xenia/ui' +import client from '../../api/client' +import { deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode } from './devicesLogic' + +const STATUS_TONE = { + online: 'bg-success-soft text-success-ink', + offline: 'bg-surface-2 text-ink-2', + waiting: 'bg-info-soft text-info-ink', + app_off: 'bg-warning-soft text-warning-ink', + removed: 'bg-surface-2 text-ink-3', +} + +export default function DevicesPage() { + const { t } = useTranslation('manager') + const f = useFormat() + const [devices, setDevices] = useState(null) + const [modules, setModules] = useState(null) + const [showRemoved, setShowRemoved] = useState(false) + const [error, setError] = useState(null) + const [dialog, setDialog] = useState(null) // { kind: 'pair' | 'edit' | 'app' | 'remove', device? } + + const load = useCallback(async () => { + try { + const [d, m] = await Promise.all([ + client.get('/api/devices', { params: { include_revoked: showRemoved } }), + client.get('/api/platform/modules'), + ]) + setDevices(d.data) + setModules(m.data.modules) + setError(null) + } catch (err) { + setError(errorMessage(err, t)) + } + }, [showRemoved, t]) + + useEffect(() => { load() }, [load]) + + const apps = useMemo(() => (modules || []).filter(m => m.device_app), [modules]) + const appOn = key => !!apps.find(a => a.key === key)?.effective + const usage = useMemo(() => (devices && modules ? deviceUsage(devices, modules) : {}), [devices, modules]) + const appName = key => t(`modules:${key}.name`, { defaultValue: key }) + const limitLabel = key => t(`modules:${key}.limits.devices`, { defaultValue: appName(key) }) + const byId = id => devices?.find(d => d.id === id) + + /** A refusal for one more device of `app`: say it with the app's own words. */ + const explain = useCallback(err => { + const x = parseError(err) + if (x.code === 'license.limit_reached') return t('devices.limitReached', { label: limitLabel(x.params.module), max: x.params.max }) + return errorMessage(err, t) + }, [t]) // eslint-disable-line react-hooks/exhaustive-deps + + async function act(fn, okText) { + try { + await fn() + setDialog(null) + if (okText) toast.success(okText) + await load() + return null + } catch (err) { + return explain(err) + } + } + + if (error) return
{t('common:actions.retry')}} />
+ if (!devices || !modules) return
+ + const live = devices.filter(d => !d.revoked_at) + const signInLabel = d => { + const modes = apps.find(a => a.key === d.app_key)?.device_app?.login_modes || [] + if (d.login_mode || modes.length < 2) return t(`devices.signIn.${d.effective_login_mode}`) // its own choice, or the only one the app has + return t('devices.signIn.default', { mode: t(`devices.signIn.${d.effective_login_mode}`) }) + } + const statusText = d => { + const s = deviceStatus(d, { appOn: appOn(d.app_key) }) + if (s === 'offline') return d.last_seen_at ? t('devices.status.offline', { when: f.formatRelative(d.last_seen_at) }) : t('devices.status.offlineNever') + if (s === 'removed') return t('devices.status.removed', { when: f.formatRelative(d.revoked_at) }) + return t(`devices.status.${s}`) + } + + return ( +
+
+

{t('devices.intro')}

+ +
+ +
+ {apps.filter(a => a.effective).map(a => { + const u = usage[a.key] || { used: 0, max: null } + return ( + = u.max ? 'bg-warning-soft text-warning-ink' : 'bg-surface-2 text-ink-2'}`}> + {u.max == null + ? t('devices.usageUnlimited', { label: limitLabel(a.key), used: u.used }) + : t('devices.usage', { label: limitLabel(a.key), used: u.used, max: u.max })} + + ) + })} +
+ + {devices.length === 0 + ? } title={t('devices.empty')} description={t('devices.emptyBody')} /> + : ( +
+ + + + {['device', 'app', 'status', 'signIn', 'linked', 'actions'].map(c => )} + + + + {devices.map(d => { + const s = deviceStatus(d, { appOn: appOn(d.app_key) }) + return ( + + + + + + + + + ) + })} + +
{t(`devices.columns.${c}`)}
+
{d.name}
+
{f.formatDate(d.paired_at, 'dateMedium')}
+
{appName(d.app_key)}{statusText(d)}{d.revoked_at ? '—' : signInLabel(d)}{byId(d.linked_device_id)?.name || '—'} + {!d.revoked_at && ( +
+ + + +
+ )} +
+
+ )} + + + + {dialog?.kind === 'pair' && ( + setDialog(null)} + onPair={body => act(() => client.post('/api/devices', body), t('devices.paired', { name: body.name }))} /> + )} + {dialog?.kind === 'edit' && ( + setDialog(null)} + onSave={body => act(() => client.patch(`/api/devices/${dialog.device.id}`, body), t('devices.saved'))} /> + )} + {dialog?.kind === 'app' && ( + setDialog(null)} + onSave={appKey => act(() => client.post(`/api/devices/${dialog.device.id}/reassign`, { app_key: appKey }), t('devices.saved'))} /> + )} + {dialog?.kind === 'remove' && ( + setDialog(null)} + onRemove={() => act(() => client.post(`/api/devices/${dialog.device.id}/revoke`))} /> + )} +
+ ) +} + +function loginModeOptions(app, t) { + const modes = app?.device_app?.login_modes || [] + if (modes.length < 2) return null + return [{ value: '', label: t('devices.signIn.defaultPlain') }, ...modes.map(m => ({ value: m, label: t(`devices.signIn.${m}`) }))] +} + +function useSubmit(fn) { + const [busy, setBusy] = useState(false) + const [error, setError] = useState('') + const run = async (...args) => { + setBusy(true) + setError('') + const err = await fn(...args) + setBusy(false) + if (err) setError(err) + } + return { busy, error, run } +} + +function PairDialog({ apps, usage, devices, appName, limitLabel, onClose, onPair }) { + const { t } = useTranslation('manager') + const usable = apps.filter(a => a.effective) + const [code, setCode] = useState('') + const [appKey, setAppKey] = useState(usable.find(a => hasRoom(usage, a.key))?.key || usable[0]?.key || '') + const [name, setName] = useState('') + const [mode, setMode] = useState('') + const [link, setLink] = useState('') + const { busy, error, run } = useSubmit(onPair) + const app = apps.find(a => a.key === appKey) + const full = appKey && !hasRoom(usage, appKey) + const modes = loginModeOptions(app, t) + const ready = code.length === 6 && appKey && name.trim() && !full + + const submit = e => { + e.preventDefault() + if (!ready || busy) return + run({ code, app_key: appKey, name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null }) + } + + return ( + }> + {usable.length === 0 + ?

{t('devices.form.noApps')}

+ : ( +
+ + setCode(normalizeCode(e.target.value))} autoComplete="off" spellCheck={false} + className="font-mono tracking-[0.3em] text-center uppercase" autoFocus /> + + + setName(e.target.value)} placeholder={t('devices.form.namePlaceholder')} maxLength={60} /> + + {modes && ( + + setLink(e.target.value)} + options={[{ value: '', label: t('devices.form.noLink') }, ...linkTargets(devices).map(d => ({ value: String(d.id), label: d.name }))]} /> + + )} + {error &&

{error}

} +
+ ) +} + +function EditDialog({ device, apps, devices, onClose, onSave }) { + const { t } = useTranslation('manager') + const [name, setName] = useState(device.name) + const [mode, setMode] = useState(device.login_mode || '') + const [link, setLink] = useState(device.linked_device_id ? String(device.linked_device_id) : '') + const { busy, error, run } = useSubmit(onSave) + const modes = loginModeOptions(apps.find(a => a.key === device.app_key), t) + const targets = linkTargets(devices, device.id) + + const submit = e => { + e.preventDefault() + if (!name.trim() || busy) return + run({ name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null }) + } + + return ( + }> +
+ + setName(e.target.value)} maxLength={60} /> + + {modes && ( + + setLink(e.target.value)} + options={[{ value: '', label: t('devices.form.noLink') }, ...targets.map(d => ({ value: String(d.id), label: d.name }))]} /> + + )} + {error &&

{error}

} +
+ ) +} + +function ChangeAppDialog({ device, apps, usage, appName, onClose, onSave }) { + const { t } = useTranslation('manager') + const usable = apps.filter(a => a.effective) + const [appKey, setAppKey] = useState(device.app_key) + const { busy, error, run } = useSubmit(onSave) + const full = appKey !== device.app_key && !hasRoom(usage, appKey) + + return ( + }> +
+ +