"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { Blocks, Plus, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Item, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"; import { useGameTick } from "@/hooks/useGameTick"; import { installModule, uninstallModule } from "@/app/(frontend)/logistics/base/actions"; export interface ModuleEffectLine { readonly type: string; readonly value: number; } export interface ModuleOptionLine { readonly name: string; readonly category: string; readonly description: string | null; readonly effects: readonly ModuleEffectLine[]; readonly materials: readonly { name: string; amount: number }[]; } export interface SlotsPanelProps { readonly gameStructureId: number; readonly slots: number; readonly installed: readonly { name: string; installedAt: string | null }[]; readonly options: readonly ModuleOptionLine[]; readonly canManage: boolean; } const EFFECT_LABELS: Record string> = { capacity_mass: (value) => `+${value.toLocaleString()} kg storage`, storage_slots: (value) => `+${value} grid slots`, production_multiplier: (value) => `+${Math.round(value * 100)}% production`, }; function effectLabel(effect: ModuleEffectLine): string { const formatter = EFFECT_LABELS[effect.type]; return formatter ? formatter(effect.value) : `${effect.type} ${effect.value}`; } export function SlotsPanel({ gameStructureId, slots, installed, options, canManage, }: SlotsPanelProps) { const router = useRouter(); const [loading, setLoading] = useState(null); useGameTick(() => router.refresh()); if (slots <= 0 && options.length === 0 && installed.length === 0) return null; const run = async (action: typeof installModule, moduleName: string, successMessage: string) => { setLoading(moduleName); try { const result = await action(gameStructureId, moduleName); if (result.success) { toast.success(successMessage); router.refresh(); } else { toast.error(result.error ?? "Could not change modules."); } } finally { setLoading(null); } }; const installedNames = new Set(installed.map((entry) => entry.name)); const available = slots - installed.length; return (
Modules 0 ? "text-muted-foreground" : "text-amber-400"}`} > {installed.length} / {slots} slots
{installed.length > 0 && (
{installed.map((entry) => ( {entry.name} {canManage && ( )} ))}
)} {canManage && available > 0 && options.length > 0 && (
{options .filter((option) => !installedNames.has(option.name)) .map((option) => (

{option.name}

{option.effects.map(effectLabel).join(", ") || "No effects"} {option.materials.length > 0 && ` | needs ${option.materials .map((material) => `${material.amount} ${material.name}`) .join(", ")}`}

))}
)} {slots > 0 && installed.length === 0 && options.length === 0 && ( No modules are defined for this structure type. )}
); }