import React, { useState, useEffect } from "react"; import { useSocket } from "../../../hooks/useSocket"; import GameDataManager from "../../../services/GameDataManager"; import "./styles/CraftingTab.css"; import CategorySelector from "../components/CategorySelector"; import CraftModal from "./components/CraftModal"; const CraftingTab = () => { const { socket } = useSocket(); const [categories, setCategories] = useState([]); const [recipes, setRecipes] = useState([]); const [userInventory, setUserInventory] = useState([]); const [activeCategory, setActiveCategory] = useState(""); const [selectedRecipe, setSelectedRecipe] = useState(null); const [activeCraft, setActiveCraft] = useState(null); useEffect(() => { const manifestCategories = GameDataManager.getRecipeCategories(); setCategories(manifestCategories); if (manifestCategories.length > 0 && !activeCategory) { setActiveCategory(manifestCategories[0].id); } }, []); useEffect(() => { if (activeCategory) { const filteredRecipes = GameDataManager.getRecipesByCategory(activeCategory); console.log(filteredRecipes); setRecipes(filteredRecipes); } }, [activeCategory]); useEffect(() => { if (!socket) return; socket.emit("player:get_inventory"); const handleInventory = (data) => setUserInventory(data); socket.on("player:inventory_data", handleInventory); return () => { socket.off("player:inventory_data", handleInventory); }; }, [socket]); useEffect(() => { let timer; if (activeCraft && activeCraft.timeLeft > 0) { timer = setInterval(() => { setActiveCraft((prev) => ({ ...prev, timeLeft: Math.max(0, prev.timeLeft - 1), })); }, 1000); } else if (activeCraft && activeCraft.timeLeft === 0) { setActiveCraft(null); socket.emit("player:get_inventory"); } return () => clearInterval(timer); }, [activeCraft, socket]); const getOwnedAmount = (itemId) => { const item = userInventory.find((i) => (i.itemId || i.id) === itemId); return item ? item.quantity : 0; }; const handleStartCrafting = (recipe) => { if (activeCraft) return; socket.emit("player:craft_item", { recipeId: recipe.id, category: activeCategory, }); setActiveCraft({ name: recipe.displayName, timeLeft: recipe.constructionTime, totalTime: recipe.constructionTime, }); setSelectedRecipe(null); }; return (