import React, { useState, useEffect } from 'react'; import './Inventory.css'; function Inventory({ cursors, currentCursorIndex, selectCursor }) { const [searchQuery, setSearchQuery] = useState(''); const [selectedRarity, setSelectedRarity] = useState('all'); const [filteredSlots, setFilteredSlots] = useState([]); // Minecraft inventory typically has 27 slots (9x3) const totalSlots = 36; // 9x4 grid // This will eventually come from backend with proper data const cursorRarities = { common: 'Common', uncommon: 'Uncommon', rare: 'Rare', epic: 'Epic', legendary: 'Legendary' }; // Assign mock rarities to cursors for demonstration const getCursorRarity = (index) => { const rarities = Object.keys(cursorRarities); return rarities[index % rarities.length]; }; // Get currently selected cursor const currentCursor = cursors.length > 0 && currentCursorIndex < cursors.length ? cursors[currentCursorIndex] : null; // Create array of slots for the inventory grid const inventorySlots = Array(totalSlots).fill(null).map((_, slotIndex) => { // Map cursor to slot if available const cursorIndex = slotIndex < cursors.length ? slotIndex : null; const cursor = cursorIndex !== null ? cursors[cursorIndex] : null; const rarity = cursor ? getCursorRarity(cursorIndex) : null; return { slotIndex, cursor, cursorIndex, rarity }; }); // Apply filters to inventory slots useEffect(() => { // First, filter the slots that match our criteria let filtered = inventorySlots.filter(slot => { // Filter by search query const matchesSearch = !searchQuery || (slot.cursor && slot.cursor.name.toLowerCase().includes(searchQuery.toLowerCase())); // Filter by rarity const matchesRarity = selectedRarity === 'all' || (slot.rarity && slot.rarity === selectedRarity); return matchesSearch && matchesRarity; }); // Create a new array for the filtered slots, prioritizing slots with items const filledSlots = filtered.filter(slot => slot.cursor); const emptySlots = Array(totalSlots - filledSlots.length).fill(null) .map((_, index) => ({ slotIndex: filledSlots.length + index, cursor: null, cursorIndex: null, rarity: null })); // Combine filled slots at the beginning and empty ones at the end const result = [...filledSlots, ...emptySlots]; setFilteredSlots(result); }, [searchQuery, selectedRarity, inventorySlots]); return (
{/* Search and Filter Controls */} {/* Equipped cursor display */} {currentCursor && (
{currentCursor.name}

Equipped

{currentCursor.name} {cursorRarities[getCursorRarity(currentCursorIndex)]}
)}
setSearchQuery(e.target.value)} />
{/* Minecraft-like inventory grid */}
{filteredSlots.map((slot) => (
slot.cursor && selectCursor(slot.cursorIndex)} > {slot.cursor && ( <> {slot.cursor.name}
{slot.cursor.name} {cursorRarities[slot.rarity]}
)}
))}
); } export default Inventory;