"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { queueCrateCommand, type ActionResult, type CrateCommand, } from "@/app/(frontend)/logistics/supply-boxes/actions"; export interface SupplyBoxContentLine { class: string; count: number; } interface SupplyBoxActionsProps { boxId: string; boxClass: string | null; location: { x: number; y: number; z: number } | null; contents: SupplyBoxContentLine[]; } type PendingAction = CrateCommand | null; export function SupplyBoxActions({ boxId, boxClass, location, contents }: SupplyBoxActionsProps) { const [lines, setLines] = useState(contents); const [confirm, setConfirm] = useState(null); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); const router = useRouter(); function updateLine(index: number, field: "class" | "count", value: string) { setLines((prev) => prev.map((line, i) => i === index ? field === "class" ? { ...line, class: value } : { ...line, count: Number.parseInt(value, 10) || 0 } : line, ), ); } function addLine() { setLines((prev) => [...prev, { class: "", count: 1 }]); } function removeLine(index: number) { setLines((prev) => prev.filter((_, i) => i !== index)); } function openConfirm(action: Exclude) { setError(null); setConfirm(action); } async function dispatch() { if (confirm === null) return; setError(null); let result: ActionResult<{ commandId: string; status: string }>; if (confirm === "apply") { const clean = lines.filter((line) => line.class.trim() !== "" && line.count > 0); result = await queueCrateCommand( boxId, "apply", [clean.map((line) => [line.class.trim(), line.count])], ); } else if (confirm === "despawn") { result = await queueCrateCommand(boxId, "despawn", []); } else { if (boxClass === null || location === null) return; result = await queueCrateCommand(boxId, "spawn", [boxClass, location.x, location.y, location.z]); } if (!result.success) { setError(result.error ?? "Failed to queue command."); return; } setConfirm(null); router.refresh(); } const applyLineCount = lines.filter((line) => line.class.trim() !== "" && line.count > 0).length; return (
{lines.map((line, index) => (
updateLine(index, "class", e.target.value)} placeholder="itemClass" className="min-w-0 flex-1 border border-white/15 bg-black/40 px-2 py-1 font-mono text-xs text-white/80 placeholder:text-white/30" /> updateLine(index, "count", e.target.value)} type="number" min={1} className="w-20 border border-white/15 bg-black/40 px-2 py-1 font-mono text-xs text-white/80" />
))}
{error ? (

{error}

) : null} { if (!open) setConfirm(null); }} > Confirm crate command {confirm === "apply" ? `Queue crate.apply with ${applyLineCount} item line${applyLineCount === 1 ? "" : "s"} to the game server?` : confirm === "despawn" ? "Remove this crate from the game server? The stored row stays as a deleted snapshot." : "Spawn this crate at its last known location?"}
); }