"use client"; import { useTranslations } from "next-intl"; import { useEffect, useState } from "react"; import { ItemCard } from "@/components/item-card"; import { BlurFade } from "@/components/ui/blur-fade"; import { Skeleton } from "@/components/ui/skeleton"; interface MarketItem { id: string; name: string; image: string; category: string; level?: number; weight?: number; rarity?: string; price?: { buy: number; sell: number; }; priceHistory?: { highest: number; lowest: number; average: number; }; } export function MarketExample() { const t = useTranslations("home.marketExample"); const [items, setItems] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchItems() { try { setIsLoading(true); // Buscar mix de categorias para variedade visual const categories = ["weapon", "armor", "helmet"]; const response = await fetch( `/api/items?category=${categories.join(",")}&pageSize=8&sort=name`, ); if (!response.ok) { throw new Error("Falha ao carregar itens"); } const data = await response.json(); const fetchedItems = data.items || []; // Enriquecer com preços mockados e raridades const enrichedItems = fetchedItems.map((item: any, index: number) => { const basePrice = Math.floor(Math.random() * 100000) + 10000; const buyPrice = basePrice; const sellPrice = Math.floor(basePrice * 0.85); const average = basePrice; const highest = Math.floor(average * 1.3); const lowest = Math.floor(average * 0.7); return { ...item, // Garantir ID único para evitar duplicação de keys id: `${item.id}-${index}`, price: { buy: buyPrice, sell: sellPrice, }, priceHistory: { highest, lowest, average, }, rarity: item.rarity || (["common", "uncommon", "rare", "epic"] as const)[ Math.floor(Math.random() * 4) ], }; }); setItems(enrichedItems); setError(null); } catch (err) { console.error("Erro ao buscar itens:", err); setError("Não foi possível carregar os itens"); } finally { setIsLoading(false); } } fetchItems(); }, []); // Skeleton para loading const LoadingSkeleton = () => (
{Array.from({ length: 8 }).map((_, index) => (
))}
); return (

{t("title")}

{t("subtitle")}

{/* Grid de itens */} {isLoading ? ( ) : error ? (
⚠️

{t("errorTitle")}

{error}

) : (
{items.map((item, index) => ( ))}
)} {/* Mensagem quando não há itens */} {!isLoading && !error && items.length === 0 && (
📦

Nenhum item encontrado

Tente novamente mais tarde

)}
); }