import config from "@payload-config"; import { getPayload } from "payload"; import { headers as nextHeaders } from "next/headers"; import Link from "next/link"; import { isPayloadUser } from "@/utils/access-control/isPayloadUser"; import { hasLogisticsQualification } from "@/utils/access-control/hasLogisticsQualification"; import { LedgerRow, NoticeState, PageShell, ProvenanceChip, STATUS_TONE_CLASSES, type ReadoutCell, type StatusTone, } from "@/components/frontend/operations"; import { SupplyBoxActions } from "@/components/frontend/logistics/SupplyBoxActions"; import type { ArmaCommand, SupplyBox } from "@/payload-types"; import type { SurfaceUser } from "@/lib/operations/surface"; type PayloadType = Awaited>; export const metadata = { title: "Supply Box Detail: Polaris Task Force", }; function relationId(value: unknown): number | null { if (typeof value === "number") return value; if (value !== null && typeof value === "object" && "id" in (value as { id: unknown })) { const id = (value as { id: unknown }).id; return typeof id === "number" ? id : null; } return null; } function locationFromJson(value: unknown): { x: number; y: number; z: number } | null { if (typeof value !== "object" || value === null) return null; const { x, y, z } = value as { x?: unknown; y?: unknown; z?: unknown }; if (typeof x !== "number" || typeof y !== "number" || typeof z !== "number") return null; return { x, y, z }; } function locationLabel(value: unknown): string { const loc = locationFromJson(value); return loc === null ? "n/a" : `${loc.x}, ${loc.y}, ${loc.z}`; } function commandTone(status: string): StatusTone { if (status === "succeeded") return "success"; if (status === "failed") return "danger"; if (status === "delivered") return "info"; return "neutral"; } export interface SupplyBoxDetailData { box: SupplyBox | null; capability: boolean; commands: ArmaCommand[]; } /** * Full detail for a single supply box plus its recent queued commands, fetched * through collection access control (default access + the explicit session * user, never overrideAccess). Returns null for an unknown id so the page can * render a not-found notice. */ async function getSupplyBoxDetail( payload: PayloadType, user: SurfaceUser, id: string, ): Promise { const qualified = await hasLogisticsQualification(payload, user); if (!qualified) return { box: null, capability: false, commands: [] }; let box: SupplyBox | null = null; try { box = await payload.findByID({ collection: "supply-boxes", id: Number(id), depth: 2, user: user ?? undefined, }); } catch { box = null; } if (box === null) return { box: null, capability: true, commands: [] }; const serverId = relationId(box.server); const commands = serverId === null ? [] : ( await payload.find({ collection: "arma-commands", where: { server: { equals: serverId } }, sort: "-createdAt", limit: 50, depth: 0, user: user ?? undefined, }) ).docs.filter((cmd) => Array.isArray(cmd.payload) && cmd.payload[0] === box.boxId); return { box, capability: true, commands }; } function Section({ title, children }: { title: string; children: React.ReactNode }) { return (

{title}

{children}
); } function kvRow(label: string, value: React.ReactNode) { return (
{label} {value}
); } export default async function SupplyBoxDetailPage({ params }: { params: Promise<{ id: string }> }) { const { id } = await params; const payload = await getPayload({ config }); const { user: authUser } = await payload.auth({ headers: await nextHeaders(), canSetHeaders: false, }); const user = isPayloadUser(authUser) ? authUser : null; const surfaceUser: SurfaceUser = user ? { id: user.id } : null; const { box, capability, commands } = await getSupplyBoxDetail(payload, surfaceUser, id); if (capability === false) { return (
); } if (box === null) { return (
Back to supply boxes } />
); } const contents = Array.isArray(box.contents) ? box.contents : []; const itemCount = contents.reduce((sum, line) => { if (typeof line !== "object" || line === null) return sum; const count = (line as { count?: unknown }).count; return sum + (typeof count === "number" ? count : 0); }, 0); const sourceEventId = relationId(box.sourceEvent); const readoutCells: ReadoutCell[] = [ { label: "Class", value: box.boxClass ?? "n/a" }, { label: "Status", value: box.status }, { label: "Items", value: String(itemCount) }, { label: "Last Sync", value: box.lastSyncedAt }, ]; return (
Back to supply boxes {sourceEventId !== null ? ( ) : null}
{contents.length === 0 ? ( ) : (
    {contents.map((line, index) => (
  • {(line as { class?: unknown }).class as string} {(line as { count?: unknown }).count as number}
  • ))}
)}
{kvRow("Position", locationLabel(box.location))} {kvRow("Session", box.sessionId)}
({ class: (line as { class?: unknown }).class as string, count: (line as { count?: unknown }).count as number, }))} />
{commands.length === 0 ? ( ) : (
    {commands.map((cmd) => (
  • {cmd.type} {cmd.status} {cmd.error ? ( {cmd.error} ) : null}
  • ))}
)}
); }