"use client"; import { useState, useEffect, useTransition, useCallback } from "react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { addResource, removeResource, transferResource, } from "@/app/(frontend)/logistics/structures/actions"; import { GameStructure, Structure, Resource } from "@/payload-types"; import { ArrowDownToLineIcon, ArrowUpFromLineIcon, ArrowRightLeftIcon, Loader2Icon, } from "lucide-react"; interface ManageStorageDialogProps { structureId: number; open: boolean; onOpenChange: (open: boolean) => void; } export function ManageStorageDialog({ structureId, open, onOpenChange, }: ManageStorageDialogProps) { const [structure, setStructure] = useState< (GameStructure & { type: Structure }) | null >(null); const [allStructures, setAllStructures] = useState< (GameStructure & { type: Structure })[] >([]); const [allResources, setAllResources] = useState([]); const [loading, setLoading] = useState(false); const [isPending, startTransition] = useTransition(); const [message, setMessage] = useState<{ type: "success" | "error"; text: string } | null>(null); // Form state const [depositResourceId, setDepositResourceId] = useState(""); const [depositAmount, setDepositAmount] = useState("1"); const [withdrawResourceId, setWithdrawResourceId] = useState(""); const [withdrawAmount, setWithdrawAmount] = useState("1"); const [transferToId, setTransferToId] = useState(""); const [transferResourceId, setTransferResourceId] = useState(""); const [transferAmount, setTransferAmount] = useState("1"); const fetchData = useCallback(async () => { setLoading(true); try { const [structRes, allRes, resourcesRes] = await Promise.all([ fetch(`/api/game-structures/${structureId}?depth=2`), fetch(`/api/game-structures?depth=2&limit=500`), fetch(`/api/resources?depth=0&limit=500`), ]); const structData = await structRes.json(); const allData = await allRes.json(); const resourcesData = await resourcesRes.json(); setStructure(structData); setAllStructures( allData.docs.filter( (s: GameStructure & { type: Structure }) => s.id !== structureId, ), ); setAllResources(resourcesData.docs ?? []); } catch { setMessage({ type: "error", text: "Failed to load structure data." }); } finally { setLoading(false); } }, [structureId]); useEffect(() => { if (open) { fetchData(); setMessage(null); setDepositResourceId(""); setDepositAmount("1"); setWithdrawResourceId(""); setWithdrawAmount("1"); setTransferToId(""); setTransferResourceId(""); setTransferAmount("1"); } }, [open, fetchData]); const storedResources = structure?.storedResources ?? []; const structureType = structure?.type; const isResourceAllowed = (resourceId: number): boolean => { if (!structureType) return true; const prohibited = structureType.prohibitedStorage?.some((rule) => { if (!rule.item) return false; const item = rule.item; if (typeof item === "object" && "id" in item) { return item.relationTo === "resources" && item.value === resourceId; } return false; }); return !prohibited; }; const handleDeposit = () => { if (!depositResourceId) return; const resId = Number(depositResourceId); const amt = Number(depositAmount); if (isNaN(amt) || amt <= 0) { setMessage({ type: "error", text: "Amount must be a positive number." }); return; } startTransition(async () => { const result = await addResource(structureId, resId, amt); if (result.success) { setMessage({ type: "success", text: "Resource deposited successfully." }); setDepositAmount("1"); fetchData(); } else { setMessage({ type: "error", text: result.error ?? "Unknown error." }); } }); }; const handleWithdraw = () => { if (!withdrawResourceId) return; const resId = Number(withdrawResourceId); const amt = Number(withdrawAmount); if (isNaN(amt) || amt <= 0) { setMessage({ type: "error", text: "Amount must be a positive number." }); return; } startTransition(async () => { const result = await removeResource(structureId, resId, amt); if (result.success) { setMessage({ type: "success", text: "Resource withdrawn successfully." }); setWithdrawAmount("1"); fetchData(); } else { setMessage({ type: "error", text: result.error ?? "Unknown error." }); } }); }; const handleTransfer = () => { if (!transferToId || !transferResourceId) return; const destId = Number(transferToId); const resId = Number(transferResourceId); const amt = Number(transferAmount); if (isNaN(amt) || amt <= 0) { setMessage({ type: "error", text: "Amount must be a positive number." }); return; } startTransition(async () => { const result = await transferResource(structureId, destId, resId, amt); if (result.success) { setMessage({ type: "success", text: "Transfer completed successfully." }); setTransferAmount("1"); fetchData(); } else { setMessage({ type: "error", text: result.error ?? "Unknown error." }); } }); }; return ( Manage Storage {structure?.name ?? "Loading..."} {loading ? (
) : ( Deposit Withdraw Transfer
setDepositAmount(e.target.value)} />
setWithdrawAmount(e.target.value)} />
setTransferAmount(e.target.value)} />
)} {message && (
{message.text}
)}
); }