import { useEffect, useState } from "react"; import { closeShift, fetchShift, openShift, type ShiftReport } from "./api.js"; // Manned-mode shift control. Start/End are explicit (not time-based — see // wiki/concepts/shift.md). End Shift signs + prints a Z-report and shows the // totals. Available to cashier/operator/admin (readonly has no shift). const money = (m: number, cur: string | null) => `${(m / 100).toFixed(2)} ${cur ?? ""}`.trim(); export function ShiftControl() { const [startedAt, setStartedAt] = useState(null); const [busy, setBusy] = useState(false); const [report, setReport] = useState(null); const [err, setErr] = useState(null); useEffect(() => { fetchShift() .then((s) => setStartedAt(s.open?.startedAt ?? null)) .catch(() => { /* readonly / not permitted — hide control */ }); }, []); async function start() { setBusy(true); setErr(null); setReport(null); try { const { startedAt } = await openShift(); setStartedAt(startedAt); } catch (e) { setErr((e as Error).message); } finally { setBusy(false); } } async function end() { setBusy(true); setErr(null); try { const z = await closeShift(); setReport(z); setStartedAt(null); } catch (e) { setErr((e as Error).message); } finally { setBusy(false); } } return (
Shift:{" "} {startedAt ? ( <> open since {new Date(startedAt).toLocaleString()}{" "} ) : ( <> not started{" "} )} {err &&

{err}

} {report && (
Z-REPORT — {report.operator}
Payments: {report.paymentCount}
Cash: {money(report.cashTotalMinor, report.currency)}
Card: {money(report.cardTotalMinor, report.currency)}
{report.printed ? "Printed to booth receipt." : "Recorded (no printer to print to)."}
)}
); }