"use client"; /* The print sheet over a stock line: copies, why, and the bar that sends it. A native shell hands the label page to Android printing; a browser opens the printable label page. */ import { useEffect, useState } from "react"; import { printLabels, printState, type PrintState } from "@/lib/nativeprint"; import { MBar, MChipRow, MKick, MRow, MSheet, MStepper, useToast } from "@/components/m"; const REASONS = ["Torn", "Faded", "New shelf"] as const; type Reason = (typeof REASONS)[number]; const plural = (n: number, w: string) => `${n} ${w}${n === 1 ? "" : "s"}`; export default function PrintSheet({ open, onClose, code, title }: { open: boolean; onClose: () => void; code: string; title: string }) { const toast = useToast(); const [copies, setCopies] = useState(1); const [reason, setReason] = useState("Torn"); const [state, setState] = useState(null); const [sending, setSending] = useState(false); useEffect(() => { if (!open) return; setCopies(1); setReason("Torn"); let live = true; printState().then((p) => { if (live) setState(p); }); return () => { live = false; }; }, [open]); const go = async () => { if (sending) return; setSending(true); const r = await printLabels({ code, copies, reason }); setSending(false); if (!r.ok) { toast(r.error); return; } onClose(); toast(state?.state === "browser" ? `${plural(copies, "label")} opened to print` : `${plural(copies, "label")} sent to the shelf printer`); }; return ( }> Shelf printer{state ? ` · ${state.label}` : ""}

{title}

} /> ({ value: r, label: r }))} />
); }