feat: add the settings page with the audit trail

Shop identity, the cantonal code that drives the holiday calendar, the
two wake intervals and the image format, plus the device list and a
translation self-test. The audit trail sits at the bottom, rendered
field by field in French rather than as raw JSON.

Regenerating a device token shows it once and stores only its digest, so
the panel has to be re-paired through the captive portal afterwards.
That is the point rather than a drawback: this is the control you reach
for when a token may have leaked, and a version that let you read the
old one back would not be one.

The server URL to type into the captive portal is reconstructed from the
incoming request, so the value shown is the one the device would
actually have to reach — not one assembled from configuration that may
not match what the proxy is serving.

Wake intervals are bounded by the same constants the device API enforces,
so a value the settings page accepts cannot be one the panel is refused.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-20 21:26:05 +02:00
co-authored by Claude Opus 5
parent 830e595393
commit 1668240724
4 changed files with 556 additions and 0 deletions
+282
View File
@@ -0,0 +1,282 @@
'use client';
import { useState, useTransition } from 'react';
import {
forgetDevice,
regenerateDeviceToken,
saveSettings,
testTranslation,
type SettingsInput,
} from './actions';
export type DeviceRow = {
id: string;
friendlyId: string;
macAddress: string;
lastSeenAt: string | null;
fwVersion: string | null;
};
export default function SettingsForm({
canEdit,
initial,
devices,
deviceBaseUrl,
}: {
canEdit: boolean;
initial: SettingsInput;
devices: DeviceRow[];
deviceBaseUrl: string;
}) {
const [form, setForm] = useState(initial);
const [pending, startTransition] = useTransition();
const [message, setMessage] = useState<{ kind: 'ok' | 'error'; text: string } | null>(null);
const [issuedToken, setIssuedToken] = useState<{ id: string; token: string } | null>(null);
function set<K extends keyof SettingsInput>(key: K, value: SettingsInput[K]) {
setMessage(null);
setForm((current) => ({ ...current, [key]: value }));
}
function run(work: () => Promise<{ ok: true } | { ok: false; error: string }>, success: string) {
setMessage(null);
startTransition(async () => {
const result = await work();
setMessage(result.ok ? { kind: 'ok', text: success } : { kind: 'error', text: result.error });
});
}
return (
<div className="mt-8 space-y-10">
{message ? (
<p
role="status"
className={`rounded-[var(--radius-md)] px-4 py-3 text-sm ${
message.kind === 'ok'
? 'bg-[var(--success-tint)] text-[var(--success)]'
: 'bg-[var(--danger-tint)] text-[var(--danger)]'
}`}
>
{message.text}
</p>
) : null}
<section className="rounded-[var(--radius-md)] border border-[var(--line)] bg-[var(--surface)] p-5">
<h2 className="text-lg">Boutique</h2>
<div className="mt-4 grid gap-4 sm:grid-cols-2">
<Field label="Nom affiché">
<input
type="text"
value={form.shopName}
disabled={!canEdit}
onChange={(event) => set('shopName', event.target.value)}
className={inputClass}
/>
</Field>
<Field label="Fuseau horaire">
<input
type="text"
value={form.timezone}
disabled={!canEdit}
onChange={(event) => set('timezone', event.target.value)}
className={inputClass}
/>
</Field>
<Field label="Pays (ISO)">
<input
type="text"
value={form.countryIsoCode}
disabled={!canEdit}
onChange={(event) => set('countryIsoCode', event.target.value)}
className={inputClass}
/>
</Field>
<Field
label="Canton / subdivision"
hint="Détermine les jours fériés synchronisés."
>
<input
type="text"
value={form.subdivisionCode}
disabled={!canEdit}
onChange={(event) => set('subdivisionCode', event.target.value)}
className={inputClass}
/>
</Field>
</div>
</section>
<section className="rounded-[var(--radius-md)] border border-[var(--line)] bg-[var(--surface)] p-5">
<h2 className="text-lg">Écran</h2>
<p className="mt-1 text-sm text-[var(--ink-muted)]">
L’écran dort entre deux réveils. Un intervalle court garde l’affichage juste, un intervalle
long ménage la batterie.
</p>
<div className="mt-4 grid gap-4 sm:grid-cols-2">
<Field label="Réveil quand la boutique est ouverte (secondes)">
<input
type="number"
value={form.refreshRateOpenSec}
disabled={!canEdit}
onChange={(event) => set('refreshRateOpenSec', Number(event.target.value))}
className={inputClass}
/>
</Field>
<Field label="Réveil la nuit et jours fermés (secondes)">
<input
type="number"
value={form.refreshRateClosedSec}
disabled={!canEdit}
onChange={(event) => set('refreshRateClosedSec', Number(event.target.value))}
className={inputClass}
/>
</Field>
<Field label="Format d’image" hint="Le firmware Seeed référence des .bmp.">
<select
value={form.imageFormat}
disabled={!canEdit}
onChange={(event) => set('imageFormat', event.target.value as 'bmp' | 'png')}
className={inputClass}
>
<option value="bmp">BMP 1 bit</option>
<option value="png">PNG 1 bit</option>
</select>
</Field>
</div>
</section>
{canEdit ? (
<button
type="button"
disabled={pending}
onClick={() => run(() => saveSettings(form), 'Paramètres enregistrés.')}
className="rounded-[var(--radius-md)] bg-[var(--accent)] px-4 py-2.5 font-medium text-[var(--on-accent)] transition-colors hover:bg-[var(--accent-hover)] disabled:opacity-40"
>
{pending ? 'Enregistrement…' : 'Enregistrer les paramètres'}
</button>
) : null}
<section className="rounded-[var(--radius-md)] border border-[var(--line)] bg-[var(--surface)] p-5">
<h2 className="text-lg">Appareils</h2>
<p className="mt-1 text-sm text-[var(--ink-muted)]">
À saisir dans le portail captif de l’écran sous <em>Advanced &gt; Custom Server</em>,{' '}
<strong>sans barre oblique finale</strong>&nbsp;: <code>{deviceBaseUrl}</code>
</p>
{devices.length === 0 ? (
<p className="mt-4 text-sm text-[var(--ink-muted)]">
Aucun appareil appairé. Le premier écran qui appelle sera enregistré automatiquement.
</p>
) : (
<ul className="mt-4 divide-y divide-[var(--line)]">
{devices.map((device) => (
<li key={device.id} className="flex flex-wrap items-center gap-x-3 gap-y-1 py-3">
<span className="font-mono">{device.friendlyId}</span>
<span className="text-sm text-[var(--ink-muted)]">{device.macAddress}</span>
<span className="text-sm text-[var(--ink-muted)]">
{device.lastSeenAt ? `vu ${device.lastSeenAt}` : 'jamais vu'}
{device.fwVersion ? ` · firmware ${device.fwVersion}` : ''}
</span>
{canEdit ? (
<span className="ml-auto flex gap-3 text-sm">
<button
type="button"
disabled={pending}
onClick={() => {
setMessage(null);
setIssuedToken(null);
startTransition(async () => {
const result = await regenerateDeviceToken(device.id);
if (result.ok) {
setIssuedToken({ id: device.id, token: result.value.token });
} else {
setMessage({ kind: 'error', text: result.error });
}
});
}}
className="underline-offset-2 hover:underline disabled:opacity-40"
>
Régénérer le jeton
</button>
<button
type="button"
disabled={pending}
onClick={() => run(() => forgetDevice(device.id), 'Appareil oublié.')}
className="text-[var(--ink-muted)] underline-offset-2 hover:text-[var(--danger)] hover:underline disabled:opacity-40"
>
Oublier
</button>
</span>
) : null}
{issuedToken?.id === device.id ? (
<div className="mt-2 w-full rounded-[var(--radius-sm)] border border-[var(--accent)] bg-[var(--accent-tint)] p-3 text-sm">
<p className="font-medium text-[var(--accent-ink)]">
Nouveau jeton — affiché une seule fois&nbsp;:
</p>
<code className="mt-1 block break-all">{issuedToken.token}</code>
<p className="mt-1 text-[var(--accent-ink)]">
L’ancien ne fonctionne plus. Réappairez l’écran par le portail captif.
</p>
</div>
) : null}
</li>
))}
</ul>
)}
</section>
{canEdit ? (
<section className="rounded-[var(--radius-md)] border border-[var(--line)] bg-[var(--surface)] p-5">
<h2 className="text-lg">Service de traduction</h2>
<p className="mt-1 text-sm text-[var(--ink-muted)]">
Traduit une phrase type pour vérifier la configuration, sans créer de message.
</p>
<button
type="button"
disabled={pending}
onClick={() => {
setMessage(null);
startTransition(async () => {
const result = await testTranslation();
setMessage(
result.ok
? { kind: 'ok', text: `Traduction obtenue : « ${result.value.text} »` }
: { kind: 'error', text: result.error },
);
});
}}
className="mt-3 rounded-[var(--radius-md)] border border-[var(--line-strong)] px-4 py-2 transition-colors hover:bg-[var(--surface-muted)] disabled:opacity-40"
>
Tester la traduction
</button>
</section>
) : null}
</div>
);
}
const inputClass =
'mt-1 w-full rounded-[var(--radius-sm)] border border-[var(--line-strong)] bg-[var(--surface)] px-2 py-2 text-base disabled:opacity-60';
function Field({
label,
hint,
children,
}: {
label: string;
hint?: string;
children: React.ReactNode;
}) {
return (
<label className="block text-sm">
<span className="block text-[var(--ink-muted)]">{label}</span>
{children}
{hint ? <span className="mt-1 block text-xs text-[var(--ink-muted)]">{hint}</span> : null}
</label>
);
}
+169
View File
@@ -0,0 +1,169 @@
'use server';
import { revalidatePath } from 'next/cache';
import { diffOf, recordAudit } from '@/lib/audit';
import { requireAdmin } from '@/lib/auth/actions';
import { prisma } from '@/lib/db';
import { generateDeviceToken, hashToken } from '@/lib/device/auth';
import { MAX_REFRESH_SEC, MIN_REFRESH_SEC } from '@/lib/device/refresh';
import { translateToEnglish } from '@/lib/translation/service';
export type ActionResult<T = undefined> = { ok: true; value: T } | { ok: false; error: string };
export type SettingsInput = {
shopName: string;
timezone: string;
countryIsoCode: string;
subdivisionCode: string;
refreshRateOpenSec: number;
refreshRateClosedSec: number;
imageFormat: 'bmp' | 'png';
};
export async function saveSettings(input: SettingsInput): Promise<ActionResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const shopName = input.shopName.trim();
if (!shopName) {
return { ok: false, error: 'Le nom de la boutique ne peut pas être vide.' };
}
if (!isValidTimezone(input.timezone)) {
return { ok: false, error: `Fuseau horaire inconnu : « ${input.timezone} ».` };
}
for (const [label, value] of [
['ouverture', input.refreshRateOpenSec],
['fermeture', input.refreshRateClosedSec],
] as const) {
if (!Number.isFinite(value) || value < MIN_REFRESH_SEC || value > MAX_REFRESH_SEC) {
return {
ok: false,
error: `L’intervalle en ${label} doit être compris entre ${MIN_REFRESH_SEC} et ${MAX_REFRESH_SEC} secondes.`,
};
}
}
const before = await prisma.settings.findUnique({ where: { id: 'singleton' } });
const data = {
shopName,
timezone: input.timezone.trim(),
countryIsoCode: input.countryIsoCode.trim().toUpperCase(),
subdivisionCode: input.subdivisionCode.trim().toUpperCase(),
refreshRateOpenSec: input.refreshRateOpenSec,
refreshRateClosedSec: input.refreshRateClosedSec,
imageFormat: input.imageFormat,
};
await prisma.settings.upsert({
where: { id: 'singleton' },
update: data,
create: { id: 'singleton', ...data },
});
await recordAudit({
userEmail: gate.value.email,
action: 'settings.update',
entity: 'Settings',
diff: diffOf(before ? asAudit(before) : null, asAudit(data)),
});
revalidatePath('/admin');
revalidatePath('/admin/parametres');
return { ok: true, value: undefined };
}
/**
* Issues a fresh token for a panel.
*
* The new token is returned once and never again — only its digest is stored.
* The device has to be re-paired through the captive portal afterwards, which
* is the point: this is what you reach for when a token may have leaked.
*/
export async function regenerateDeviceToken(id: string): Promise<ActionResult<{ token: string }>> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const device = await prisma.device.findUnique({ where: { id } });
if (!device) {
return { ok: false, error: 'Cet appareil n’existe plus.' };
}
const token = generateDeviceToken();
await prisma.device.update({ where: { id }, data: { apiKeyHash: hashToken(token) } });
await recordAudit({
userEmail: gate.value.email,
action: 'device.regenerate_token',
entity: 'Device',
entityId: id,
diff: { jeton: { before: 'précédent', after: 'régénéré' } },
});
revalidatePath('/admin/parametres');
return { ok: true, value: { token } };
}
export async function forgetDevice(id: string): Promise<ActionResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const device = await prisma.device.findUnique({ where: { id } });
if (!device) {
return { ok: false, error: 'Cet appareil n’existe plus.' };
}
await prisma.device.delete({ where: { id } });
await recordAudit({
userEmail: gate.value.email,
action: 'device.forget',
entity: 'Device',
entityId: id,
diff: diffOf({ adresse: device.macAddress, identifiant: device.friendlyId }, null),
});
revalidatePath('/admin/parametres');
return { ok: true, value: undefined };
}
/** Round-trips a short phrase so the service can be checked without a message. */
export async function testTranslation(): Promise<ActionResult<{ text: string }>> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const outcome = await translateToEnglish('Fermeture exceptionnelle jeudi après-midi');
return outcome.ok
? { ok: true, value: { text: outcome.text } }
: { ok: false, error: outcome.error };
}
function isValidTimezone(value: string): boolean {
try {
new Intl.DateTimeFormat('en', { timeZone: value.trim() });
return true;
} catch {
return false;
}
}
/** Accepts a stored row too, whose imageFormat is a plain string. */
function asAudit(settings: Omit<SettingsInput, 'imageFormat'> & { imageFormat: string }): Record<string, unknown> {
return {
nom: settings.shopName,
fuseau: settings.timezone,
pays: settings.countryIsoCode,
canton: settings.subdivisionCode,
'réveil ouvert (s)': settings.refreshRateOpenSec,
'réveil fermé (s)': settings.refreshRateClosedSec,
'format image': settings.imageFormat,
};
}
+104
View File
@@ -0,0 +1,104 @@
import { headers } from 'next/headers';
import { auth } from '@/lib/auth';
import { publicBaseUrl } from '@/lib/config';
import { prisma } from '@/lib/db';
import { loadSettings } from '@/lib/schedule/context';
import SettingsForm, { type DeviceRow } from './SettingsForm';
export const metadata = { title: 'Paramètres — ITA ITO' };
/** The audit trail is a log, not a database browser: the last fifty suffice. */
const AUDIT_LIMIT = 50;
export default async function SettingsPage() {
const [session, settings, headerList] = await Promise.all([auth(), loadSettings(), headers()]);
const [devices, audit] = await Promise.all([
prisma.device.findMany({ orderBy: { createdAt: 'asc' } }),
prisma.auditLog.findMany({ orderBy: { createdAt: 'desc' }, take: AUDIT_LIMIT }),
]);
// Reconstructed from the incoming request so the value shown is the one the
// device would actually have to reach.
const baseUrl = publicBaseUrl(
new Request('https://placeholder.invalid', { headers: headerList }),
);
const deviceRows: DeviceRow[] = devices.map((device) => ({
id: device.id,
friendlyId: device.friendlyId,
macAddress: device.macAddress,
lastSeenAt: device.lastSeenAt ? device.lastSeenAt.toLocaleString('fr-CH') : null,
fwVersion: device.fwVersion,
}));
return (
<main className="mx-auto max-w-3xl px-4 py-10">
<h1 className="text-2xl">Paramètres</h1>
<SettingsForm
canEdit={session?.user?.role === 'admin'}
initial={{
shopName: settings.shopName,
timezone: settings.timezone,
countryIsoCode: settings.countryIsoCode,
subdivisionCode: settings.subdivisionCode,
refreshRateOpenSec: settings.refreshRateOpenSec,
refreshRateClosedSec: settings.refreshRateClosedSec,
imageFormat: settings.imageFormat,
}}
devices={deviceRows}
deviceBaseUrl={baseUrl}
/>
<section className="mt-12">
<h2 className="text-lg">Journal d’audit</h2>
<p className="mt-1 text-sm text-[var(--ink-muted)]">
Les {AUDIT_LIMIT} dernières actions. Qui a changé quoi, et quand.
</p>
{audit.length === 0 ? (
<p className="mt-3 text-sm text-[var(--ink-muted)]">Aucune action enregistrée.</p>
) : (
<ul className="mt-3 divide-y divide-[var(--line)] overflow-hidden rounded-[var(--radius-md)] border border-[var(--line)] bg-[var(--surface)] text-sm">
{audit.map((entry) => (
<li key={entry.id} className="px-4 py-2.5">
<div className="flex flex-wrap items-baseline gap-x-3 gap-y-1">
<span className="text-[var(--ink-muted)] tabular-nums">
{entry.createdAt.toLocaleString('fr-CH')}
</span>
<span className="font-mono text-xs">{entry.action}</span>
<span className="text-[var(--ink-muted)]">{entry.userEmail}</span>
</div>
{entry.diff ? (
<ul className="mt-1 space-y-0.5 text-[var(--ink-muted)]">
{Object.entries(entry.diff as Record<string, { before: unknown; after: unknown }>).map(
([field, change]) => (
<li key={field}>
<span className="font-medium">{field}</span> :{' '}
{render(change.before)} → {render(change.after)}
</li>
),
)}
</ul>
) : null}
</li>
))}
</ul>
)}
</section>
</main>
);
}
function render(value: unknown): string {
if (value === null || value === undefined) {
return '—';
}
if (typeof value === 'boolean') {
return value ? 'oui' : 'non';
}
return String(value);
}