import { useState } from 'react'; import { createPrintStation, discardDeadBadgePrint, enqueueBadgePrint, renamePrintStation, requeueDeadBadgePrint, revokePrintStation, } from '../lib/api'; import type { PrintDeadLetter, PrintRecentJob, PrintStation, PrintStationState } from '../lib/types'; import { useToast } from '../context/ToastContext'; import { Empty, Modal, Spinner } from '../components/ui'; import { activeStations, printerHint, timeAgo, usePrintStatus } from '../lib/printStatus'; const STATE_LABEL: Record = { ready: 'ready', printing: 'printing', error: 'error', offline: 'offline', revoked: 'revoked', }; const STATE_PILL: Record = { ready: 'green', printing: 'session', error: 'red', offline: 'attendee', revoked: 'attendee', }; const kindLabel = (kind: string) => (kind === 'checkin' ? 'check-in' : kind === 'reprint' ? 'reprint' : kind); function fmtUptime(secs: number | undefined): string { if (secs == null || !Number.isFinite(secs)) return '—'; const h = Math.floor(secs / 3600); const m = Math.floor((secs % 3600) / 60); return h > 0 ? `${h}h ${m}m` : `${m}m`; } function fmtTime(iso: string | null): string { if (!iso) return '—'; const d = new Date(iso); return Number.isNaN(d.getTime()) ? iso : d.toLocaleString(); } export function Printing() { const { push } = useToast(); const { overview, error, refresh } = usePrintStatus(); const [adding, setAdding] = useState(false); const [stationName, setStationName] = useState(''); const [creating, setCreating] = useState(false); const [issuedToken, setIssuedToken] = useState<{ name: string; token: string } | null>(null); const [copied, setCopied] = useState(false); const [busyId, setBusyId] = useState(null); const [renamingId, setRenamingId] = useState(null); const [renameValue, setRenameValue] = useState(''); const [renaming, setRenaming] = useState(false); if (error && !overview) { return ; } if (!overview) { return (
); } const stations = overview.stations; const live = activeStations(overview); const { queue } = overview; async function addStation() { const name = stationName.trim(); if (!name || creating) return; setCreating(true); try { const { token } = await createPrintStation(name); setIssuedToken({ name, token }); setStationName(''); setAdding(false); await refresh(); } catch (e) { push('error', e instanceof Error ? e.message : 'Could not create the station'); } finally { setCreating(false); } } async function revoke(station: PrintStation) { if (!confirm(`Revoke "${station.name}"? Its token stops working immediately and the Raspberry Pi can no longer print.`)) return; setBusyId(station.id); try { await revokePrintStation(station.id); await refresh(); push('success', `Station "${station.name}" revoked`); } catch (e) { push('error', e instanceof Error ? e.message : 'Revoke failed'); } finally { setBusyId(null); } } function startRename(station: PrintStation) { setRenamingId(station.id); setRenameValue(station.name); } function cancelRename() { setRenamingId(null); setRenameValue(''); } async function saveRename(station: PrintStation) { const name = renameValue.trim(); if (!name || renaming) return; setRenaming(true); try { await renamePrintStation(station.id, name); await refresh(); push('success', `Station renamed to "${name}"`); cancelRename(); } catch (e) { push('error', e instanceof Error ? e.message : 'Rename failed'); } finally { setRenaming(false); } } async function requeue(dl: PrintDeadLetter) { setBusyId(dl.msg_id); try { await requeueDeadBadgePrint(dl.msg_id); await refresh(); push('success', 'Job requeued — the station will pick it up'); } catch (e) { push('error', e instanceof Error ? e.message : 'Requeue failed'); } finally { setBusyId(null); } } async function discard(dl: PrintDeadLetter) { if (!confirm(`Discard this dead job${dl.full_name ? ` for ${dl.full_name}` : ''}? It will not be printed.`)) return; setBusyId(dl.msg_id); try { await discardDeadBadgePrint(dl.msg_id); await refresh(); push('success', 'Dead job discarded'); } catch (e) { push('error', e instanceof Error ? e.message : 'Discard failed'); } finally { setBusyId(null); } } async function reprint(job: PrintRecentJob) { if (!job.registration_id) { push('error', 'This job has no registration to reprint'); return; } setBusyId(job.msg_id); try { await enqueueBadgePrint(job.registration_id); await refresh(); push('success', `Reprint queued for ${job.full_name ?? job.email ?? 'attendee'}`); } catch (e) { push('error', e instanceof Error ? e.message : 'Reprint failed'); } finally { setBusyId(null); } } async function copyToken() { if (!issuedToken) return; try { await navigator.clipboard.writeText(issuedToken.token); setCopied(true); } catch { push('error', 'Copy failed — select the token and copy it manually'); } } return (

Printing

Badge queue and printer stations. {error ? Status update failed: {error} : null}
{adding ? ( <> setStationName(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && addStation()} /> ) : ( )}
{queue.pending}
pending
{queue.in_flight}
in progress
{queue.completed}
done
{queue.dead_letter}
dead letter

Stations

{live.length === 0 && stations.length === 0 && (
)} {live.length === 0 && stations.length > 0 && (
)} {live.map((s) => { const st = s.last_status; const hint = printerHint(s); const printer = st?.printer; const roll = printer?.label_id ? `${printer.label_id}${printer.media_type ? ` · ${printer.media_type}` : ''}${ printer.media_width_mm ? ` · ${printer.media_width_mm}×${printer.media_length_mm ?? '∞'} mm` : '' }` : '—'; return (
{renamingId === s.id ? ( setRenameValue(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') void saveRename(s); if (e.key === 'Escape') cancelRename(); }} /> ) : ( <> {s.name} {STATE_LABEL[s.state]} )}
last seen {timeAgo(s.last_seen_at)} ago {renamingId !== s.id && ( )}
{hint &&
{hint}
}
Worker
{st?.worker ?? '—'}
Current job
{st?.job_msg_id ? `#${st.job_msg_id}` : '—'}
Printer
{printer?.model ?? '—'}
Roll
{roll}
Errors
{printer?.errors?.length ? printer.errors.join(' · ') : '—'}
Network
{st?.network ? `${st.network.state ?? '—'}${st.network.ssid ? ` · ${st.network.ssid}` : ''}${st.network.ip ? ` · ${st.network.ip}` : ''}` : '—'}
Version
{st?.version ?? '—'}
Uptime
{fmtUptime(st?.uptime_s)}
Last error
{st?.last_error ?? '—'}
); })} {stations.filter((s) => s.state === 'revoked').map((s) => (
{s.name} revoked
revoked {fmtTime(s.revoked_at)}
))}

Dead letters

{overview.dead_letters.map((dl) => ( ))}
Attendee Event Error Attempts Failed at Actions
{dl.full_name ?? '—'}
{dl.email ?? ''}
{dl.event_name ?? '—'} {dl.error ?? '—'} {dl.attempts ?? '—'} {fmtTime(dl.failed_at)}
{overview.dead_letters.length === 0 && (
)}

Recently printed

{overview.recent.map((job) => ( ))}
Attendee Kind Time Actions
{job.full_name ?? '—'}
{job.email ?? ''}
{kindLabel(job.kind)} {fmtTime(job.archived_at)}
{overview.recent.length === 0 && (
)}
{issuedToken && ( { setIssuedToken(null); setCopied(false); }} footer={ } >

Store it in the Raspberry Pi setup now — it cannot be shown again.

e.target.select()} aria-label="Station token" />
)}
); }