import { redirect } from "next/navigation"; import { currentUser } from "@/lib/session"; import { prisma } from "@/lib/db"; import { locTrail, type LocationRec } from "@/lib/compute"; import { shelfLabelCode } from "@/lib/scanroute"; import AutoPrint from "@/components/AutoPrint"; import { barcodeSvg } from "@/lib/barcode"; export const dynamic = "force-dynamic"; type SP = Record; /* Shelf labels, 6 to an A4 sheet. * * A shelf label is Code 128 of `TCL-`. Scanned on the counter app's Scan tab it opens * that shelf's count. `?copies=` prints the same label several times (a shelf with two ends); * `?native=1` is the Android app's print path, which hands the page to the print service itself. */ export default async function ShelfLabelSheet({ searchParams }: { searchParams: Promise }) { const user = await currentUser(); if (!user) redirect("/auth"); const q = await searchParams; const locationId = String(q.location || "").trim().slice(0, 64); const copies = Math.min(24, Math.max(1, parseInt(q.copies || "1", 10) || 1)); const native = q.native === "1"; if (!locationId) redirect("/m/count"); const rows = await prisma.location.findMany({ where: { facilityId: user.facilityId }, select: { id: true, name: true, kind: true, parentId: true, sort: true, archived: true }, }); const loc = rows.find((l) => l.id === locationId && !l.archived); if (!loc) redirect("/m/count"); const byId: Record = Object.fromEntries(rows.map((l) => [l.id, l])); const trail = loc.parentId ? locTrail(byId, loc.parentId, 0) : ""; const code = shelfLabelCode(loc.id); const svg = barcodeSvg(code, { module: 2, height: 58 }); return (
{copies} label{copies === 1 ? "" : "s"} {loc.name} {code} {!native && }
{Array.from({ length: copies }, (_, i) => (
{loc.name}
{trail &&
{trail}
}
{code}
))}
); }