207 lines
No EOL
7.4 KiB
TypeScript
207 lines
No EOL
7.4 KiB
TypeScript
"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<SupplyBoxContentLine[]>(contents);
|
|
const [confirm, setConfirm] = useState<PendingAction>(null);
|
|
const [error, setError] = useState<string | null>(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<PendingAction, null>) {
|
|
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 (
|
|
<div data-slot="supply-box-actions" className="flex flex-col gap-3">
|
|
<div className="flex flex-col gap-1">
|
|
<label className="font-mono text-[9px] uppercase tracking-widest text-white/50">
|
|
Contents to apply
|
|
</label>
|
|
<div className="flex flex-col gap-1">
|
|
{lines.map((line, index) => (
|
|
<div key={index} className="flex items-center gap-2">
|
|
<input
|
|
value={line.class}
|
|
onChange={(e) => 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"
|
|
/>
|
|
<input
|
|
value={String(line.count)}
|
|
onChange={(e) => 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"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => removeLine(index)}
|
|
className="border border-white/15 px-2 py-1 font-mono text-[10px] uppercase tracking-widest text-white/60 hover:border-rose-500/40 hover:text-rose-300"
|
|
>
|
|
Remove
|
|
</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={addLine}
|
|
className="self-start border border-white/15 px-2 py-1 font-mono text-[10px] uppercase tracking-widest text-white/70 hover:border-white/30 hover:text-white"
|
|
>
|
|
Add line
|
|
</button>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<button
|
|
type="button"
|
|
data-slot="supply-box-apply"
|
|
onClick={() => openConfirm("apply")}
|
|
disabled={applyLineCount === 0}
|
|
className="border border-emerald-500/30 bg-emerald-500/5 px-2 py-1 font-mono text-[10px] uppercase tracking-widest text-emerald-300 transition-colors hover:border-emerald-500/50 hover:text-emerald-200 disabled:cursor-not-allowed disabled:opacity-50"
|
|
>
|
|
Queue apply
|
|
</button>
|
|
<button
|
|
type="button"
|
|
data-slot="supply-box-despawn"
|
|
onClick={() => openConfirm("despawn")}
|
|
className="border border-rose-500/30 bg-rose-500/5 px-2 py-1 font-mono text-[10px] uppercase tracking-widest text-rose-300 transition-colors hover:border-rose-500/50 hover:text-rose-200"
|
|
>
|
|
Despawn
|
|
</button>
|
|
<button
|
|
type="button"
|
|
data-slot="supply-box-respawn"
|
|
onClick={() => openConfirm("spawn")}
|
|
disabled={boxClass === null || location === null}
|
|
className="border border-blue-500/30 bg-blue-500/5 px-2 py-1 font-mono text-[10px] uppercase tracking-widest text-blue-300 transition-colors hover:border-blue-500/50 hover:text-blue-200 disabled:cursor-not-allowed disabled:opacity-50"
|
|
>
|
|
Respawn
|
|
</button>
|
|
</div>
|
|
|
|
{error ? (
|
|
<p data-slot="supply-box-actions-error" className="min-w-0 truncate text-xs text-rose-300">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
|
|
<Dialog
|
|
open={confirm !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) setConfirm(null);
|
|
}}
|
|
>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>Confirm crate command</DialogTitle>
|
|
<DialogDescription>
|
|
{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?"}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogFooter>
|
|
<button
|
|
type="button"
|
|
onClick={() => setConfirm(null)}
|
|
className="border border-white/15 px-3 py-1 font-mono text-[10px] uppercase tracking-widest text-white/70 hover:border-white/30 hover:text-white"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
data-slot="supply-box-confirm"
|
|
onClick={() => startTransition(dispatch)}
|
|
disabled={isPending}
|
|
className="border border-emerald-500/30 bg-emerald-500/5 px-3 py-1 font-mono text-[10px] uppercase tracking-widest text-emerald-300 hover:border-emerald-500/50 hover:text-emerald-200 disabled:cursor-not-allowed disabled:opacity-50"
|
|
>
|
|
{isPending ? "Queuing..." : "Confirm"}
|
|
</button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
} |