Owner 2026-11-23 (plan 6F.1b): 26 new tips in English and Greek: Operation (own shifts, hidden takings, card payments, closing tables, the serve step, courses, quick notes), Sign-in (waiter and device modes, usernames, one session, idle sign-out), venue language and time zone, the fiscal device, locking, the server's address, the product editor (unit, available, quick add, VAT group, cost), a device's sign-in, a person's role, the cash counted at a shift's end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
169 lines
10 KiB
React
169 lines
10 KiB
React
// Settings → General (plan step 6.14a; was the v1 AppInfoTab): the system (version and whether an
|
|
// update is out, license, running or locked, how long it has been up), the local network the
|
|
// phones use (the server's address, changed by hand or found by itself; the pairing QR), what the
|
|
// venue has set up, exporting / importing the catalogue and the tables, and the support account's
|
|
// lock / unlock. The time zone is in "Venue & language" (it was here twice).
|
|
import { useState } from 'react'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import toast from 'react-hot-toast'
|
|
import { Lock, LockOpen, QrCode as QrIcon, RefreshCw } from 'lucide-react'
|
|
import { useAccess } from '@xenia/access'
|
|
import { errorMessage } from '@xenia/api'
|
|
import { useFormat, useTranslation } from '@xenia/i18n'
|
|
import { Button, Columns, Dialog, Input, Spinner } from '@xenia/ui'
|
|
import { QrCode } from '@xenia/ui/qr'
|
|
import client from '../../../api/client'
|
|
import { useLiveQuery } from '../../../live/useLiveQuery'
|
|
import { Pill } from '../../overview/parts'
|
|
import DataTransfer from './DataTransfer'
|
|
import { SettingRow, SettingsCard } from './SettingsCard'
|
|
import { lanAddress, uptime, waiterPairUrl } from './settingsLogic'
|
|
|
|
export default function SystemSettings() {
|
|
const { t } = useTranslation('manager')
|
|
const fmt = useFormat()
|
|
const qc = useQueryClient()
|
|
const { user } = useAccess()
|
|
const [qr, setQr] = useState(null)
|
|
const { data: status, isPending } = useLiveQuery({ queryKey: ['system-status'], queryFn: () => client.get('/api/system/status').then(r => r.data), refetchInterval: 30_000 })
|
|
const { data: stats } = useLiveQuery({ queryKey: ['system-stats'], queryFn: () => client.get('/api/system/stats').then(r => r.data), staleTime: 60_000 })
|
|
const refresh = useMutation({
|
|
mutationFn: () => client.post('/api/system/sync-license').catch(() => null), // the cloud may be away: still refresh what we know
|
|
onSettled: () => { qc.invalidateQueries({ queryKey: ['system-status'] }); qc.invalidateQueries({ queryKey: ['license-status'] }) },
|
|
})
|
|
const lock = useMutation({
|
|
mutationFn: on => client.post(on ? '/api/system/lock' : '/api/system/unlock'),
|
|
onSuccess: (_r, on) => { toast.success(t(on ? 'settings.system.locked' : 'settings.system.unlocked')); qc.invalidateQueries({ queryKey: ['system-status'] }) },
|
|
onError: err => toast.error(errorMessage(err, t)),
|
|
})
|
|
if (isPending) return <div className="py-8 flex justify-center"><Spinner /></div>
|
|
const up = uptime(status?.uptime_seconds)
|
|
const state = status?.locked ? ['danger', 'locked'] : status?.lock_pending ? ['warning', 'lockPending'] : ['success', 'running']
|
|
const latest = status?.latest_version
|
|
const counts = ['categories', 'products', 'tables', 'table_groups', 'managers', 'waiters']
|
|
|
|
return (
|
|
<Columns testId="settings-system">
|
|
<SettingsCard title={t('settings.system.title')}>
|
|
<SettingRow label={t('settings.system.version')}>
|
|
<span className="flex flex-wrap items-center gap-2 text-body font-semibold text-ink" data-testid="system-version">
|
|
{status?.version ?? '—'}
|
|
{latest && latest !== status?.version && <Pill tone="info">{t('settings.system.update', { version: latest })}</Pill>}
|
|
{latest && latest === status?.version && <Pill tone="success">{t('settings.system.upToDate')}</Pill>}
|
|
</span>
|
|
</SettingRow>
|
|
<SettingRow label={t('settings.system.license')} hint={status?.expires_at ? t('settings.system.expires', { date: fmt.formatDate(status.expires_at) }) : undefined}>
|
|
<Pill tone={status?.licensed ? 'success' : 'danger'}>{t(status?.licensed ? 'settings.system.licensed' : 'settings.system.unlicensed')}</Pill>
|
|
</SettingRow>
|
|
<SettingRow label={t('settings.system.state')}>
|
|
<Pill tone={state[0]}>{t(`settings.system.states.${state[1]}`)}</Pill>
|
|
</SettingRow>
|
|
<SettingRow label={t('settings.system.uptime')}>
|
|
<span className="flex items-center gap-2 text-body text-ink">
|
|
{t('settings.system.uptimeValue', { days: up.days, h: up.h, m: up.m })}
|
|
<Button size="sm" variant="ghost" icon={<RefreshCw size={14} />} loading={refresh.isPending} onClick={() => refresh.mutate()}>{t('settings.system.refresh')}</Button>
|
|
</span>
|
|
</SettingRow>
|
|
{status?.waiter_domain && (
|
|
<SettingRow label={t('settings.system.oldDomain')} hint={t('settings.system.oldDomainHint')}>
|
|
<span className="flex items-center gap-2 text-label font-mono break-all">{status.waiter_domain}
|
|
<Button size="sm" variant="secondary" icon={<QrIcon size={14} />} onClick={() => setQr({ url: status.waiter_domain })}>{t('settings.system.qr')}</Button>
|
|
</span>
|
|
</SettingRow>
|
|
)}
|
|
</SettingsCard>
|
|
|
|
<LanAccess status={status} onQr={setQr} />
|
|
|
|
<SettingsCard title={t('settings.system.setUp')} testId="system-stats">
|
|
<div className="grid grid-cols-2 sm:grid-cols-3 gap-3 p-5">
|
|
{counts.map(k => (
|
|
<div key={k} className="rounded-card bg-surface-2 p-3 grid">
|
|
<span className="text-overline font-semibold uppercase tracking-wide text-ink-3">{t(`settings.system.counts.${k}`)}</span>
|
|
<span className="text-title font-semibold tabular-nums text-ink">{stats?.[k] ?? '—'}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</SettingsCard>
|
|
|
|
<DataTransfer />
|
|
|
|
{user?.role === 'superadmin' && (
|
|
<SettingsCard title={t('settings.system.support')} description={t('settings.system.supportHint')}>
|
|
<div className="flex gap-2 p-5">
|
|
<Button variant="secondary" icon={<LockOpen size={15} />} loading={lock.isPending && lock.variables === false} onClick={() => lock.mutate(false)}>{t('settings.system.unlock')}</Button>
|
|
<Button variant="danger" icon={<Lock size={15} />} loading={lock.isPending && lock.variables === true} onClick={() => lock.mutate(true)}>{t('settings.system.lock')}</Button>
|
|
</div>
|
|
</SettingsCard>
|
|
)}
|
|
|
|
{qr && (
|
|
<Dialog open size="sm" onClose={() => setQr(null)} title={t('settings.system.qrTitle')} description={qr.caption}
|
|
footer={<Button variant="secondary" onClick={() => setQr(null)}>{t('settings.back')}</Button>}>
|
|
<div className="grid justify-items-center gap-3" data-testid="system-qr">
|
|
<QrCode value={qr.url} size={220} />
|
|
<p className="text-caption font-mono text-ink-2 break-all text-center">{qr.url}</p>
|
|
</div>
|
|
</Dialog>
|
|
)}
|
|
</Columns>
|
|
)
|
|
}
|
|
|
|
/** The server's address on the venue's Wi-Fi: what the phones and the QR use (services/lan_ip.py). */
|
|
function LanAccess({ status, onQr }) {
|
|
const { t } = useTranslation('manager')
|
|
const qc = useQueryClient()
|
|
const info = status?.lan_ip_info
|
|
const { ip, source } = lanAddress(status, window.location.hostname)
|
|
const [editing, setEditing] = useState(null) // the typed address while editing
|
|
const save = useMutation({
|
|
mutationFn: value => client.put('/api/system/lan-ip-override', { ip: value }).then(r => r.data),
|
|
onSuccess: (_r, value) => { toast.success(t(value ? 'settings.lan.saved' : 'settings.lan.automatic')); setEditing(null); qc.invalidateQueries({ queryKey: ['system-status'] }) },
|
|
onError: err => toast.error(errorMessage(err, t)),
|
|
})
|
|
return (
|
|
<SettingsCard title={t('settings.lan.title')} description={t('settings.lan.intro')} testId="settings-lan">
|
|
{info?.mismatch && (
|
|
<div className="m-5 mb-0 rounded-card bg-warning-soft text-warning-ink p-3 text-label grid gap-2" role="alert">
|
|
<span>{t('settings.lan.mismatch', { ip, detected: info.detected })}</span>
|
|
<Button size="sm" variant="secondary" className="justify-self-start" loading={save.isPending} onClick={() => save.mutate(null)}>
|
|
{t('settings.lan.useDetected', { ip: info.detected })}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
<SettingRow label={t('settings.lan.address')} tip={t('settings.lan.addressTip')} hint={!ip ? t('settings.lan.unknown') : info && info.detection !== 'ok' && editing == null ? t(`settings.lan.detection.${info.detection}`, { defaultValue: '' }) : undefined}>
|
|
{editing != null ? (
|
|
<span className="grid gap-2">
|
|
<Input value={editing} autoFocus className="font-mono" placeholder={info?.detected || '192.168.1.50'} onChange={e => setEditing(e.target.value)}
|
|
onKeyDown={e => { if (e.key === 'Enter' && editing.trim()) save.mutate(editing.trim()); if (e.key === 'Escape') setEditing(null) }} data-testid="lan-ip" />
|
|
<span className="flex flex-wrap gap-1">
|
|
<Button size="sm" loading={save.isPending} disabled={!editing.trim()} onClick={() => save.mutate(editing.trim())}>{t('settings.save')}</Button>
|
|
{info?.override && <Button size="sm" variant="secondary" onClick={() => save.mutate(null)}>{t('settings.lan.auto')}</Button>}
|
|
<Button size="sm" variant="ghost" onClick={() => setEditing(null)}>{t('settings.back')}</Button>
|
|
</span>
|
|
</span>
|
|
) : (
|
|
<span className="flex flex-wrap items-center gap-2">
|
|
<span className="font-mono text-body text-ink">{ip ?? '—'}</span>
|
|
{source && <Pill>{t(`settings.lan.source.${source}`)}</Pill>}
|
|
<Button size="sm" variant="secondary" onClick={() => setEditing(info?.override || ip || '')} data-testid="lan-change">{t('settings.lan.change')}</Button>
|
|
</span>
|
|
)}
|
|
</SettingRow>
|
|
{ip && (<>
|
|
<SettingRow label={t('settings.lan.waiters')} hint={t('settings.lan.waitersHint')}>
|
|
<span className="flex flex-wrap items-center gap-2">
|
|
<span className="font-mono text-label text-ink break-all">http://{ip}</span>
|
|
<Button size="sm" variant="secondary" icon={<QrIcon size={14} />} data-testid="lan-qr"
|
|
onClick={() => onQr({ url: waiterPairUrl(ip, status?.site_id, status?.tls?.spki_sha256), caption: t('settings.lan.qrCaption') })}>{t('settings.system.qr')}</Button>
|
|
</span>
|
|
</SettingRow>
|
|
<SettingRow label={t('settings.lan.manager')}>
|
|
<span className="font-mono text-label text-ink break-all">http://{ip}/manager</span>
|
|
</SettingRow>
|
|
</>)}
|
|
</SettingsCard>
|
|
)
|
|
}
|