"use client"; import { useState, useEffect } from "react"; import Image from "next/image"; import { GamingSidebar } from "@/components/games/gaming-sidebar"; import { GameRow } from "@/components/games/game-row"; import { GameCard } from "@/components/games/game-card"; import { Search, Heart, Clock, Star, Zap, Gamepad2, AlertCircle, LayoutGrid, } from "lucide-react"; import { cn } from "@/lib/utils"; import api from "@/lib/api"; interface Provider { provider_id: string; provider_name: string; logo_dark: string; logo_light: string; enabled: boolean; } interface ApiGame { gameId: string; providerId: string; provider: string; name: string; category: string; deviceType: string; hasDemo: boolean; hasFreeBets: boolean; demoUrl?: string; image?: string; // In case it gets added thumbnail?: string; provider_id?: string; // Fallback } const DEFAULT_IMAGE = "https://st.pokgaming.com/gameThumbnails/246.jpg"; export default function VirtualPage() { const [activeCategory, setActiveCategory] = useState("all"); const [providers, setProviders] = useState([]); const [games, setGames] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchVirtualData = async () => { try { setIsLoading(true); const [providersRes, gamesRes] = await Promise.all([ api.get("/virtual-game/orchestrator/providers"), api.get("/virtual-game/orchestrator/games", { params: { limit: 2000 }, }), ]); const pData = providersRes.data; const gData = gamesRes.data; const providersList = pData.providers || pData.data || pData || []; const gamesList = gData.data || gData.games || gData || []; setProviders(Array.isArray(providersList) ? providersList : []); setGames(Array.isArray(gamesList) ? gamesList : []); } catch (err: any) { console.error("Failed to fetch virtual games data:", err); setError("Failed to load games data."); } finally { setIsLoading(false); } }; fetchVirtualData(); }, []); // Create Sidebar Categories dynamically from providers const sidebarCategories = [ { id: "all", name: "All Games", icon: LayoutGrid }, ...providers.map((p) => ({ id: p.provider_id, name: p.provider_name, icon: p.logo_dark || p.logo_light || Gamepad2, })), ]; // Filter games based on active category // If "all", group by provider let displayedGames: any[] = []; let groupedGames: { title: string; games: any[] }[] = []; const mapApiGameToCard = (game: ApiGame) => ({ id: game.gameId, title: game.name, image: game.thumbnail || game.image || DEFAULT_IMAGE, provider: game.provider, }); if (activeCategory === "all") { // Group up to 12 games per provider for the rows providers.forEach((p) => { const providerIdStr = String(p.provider_id || "") .trim() .toLowerCase(); const providerGames = games .filter((g) => { const gameProvId = String(g.providerId || g.provider_id || "") .trim() .toLowerCase(); return gameProvId === providerIdStr; }) .slice(0, 12) .map(mapApiGameToCard); if (providerGames.length > 0) { groupedGames.push({ title: p.provider_name, games: providerGames, }); } }); } else { displayedGames = games .filter((g) => { const gameProvId = String(g.providerId || g.provider_id || "") .trim() .toLowerCase(); const matches = gameProvId === String(activeCategory).trim().toLowerCase(); if ( g.providerId?.toLowerCase().includes("pop") || g.provider_id?.toLowerCase().includes("pop") ) { } return matches; }) .map(mapApiGameToCard); } const activeCategoryData = providers.find( (p) => String(p.provider_id || "") .trim() .toLowerCase() === String(activeCategory).trim().toLowerCase(), ); return (
{/* Sidebar */} {/* Main Content */}
{/* Banner */}
Virtual Games Banner
{isLoading ? (
Loading games...
) : error ? (
{error}
) : activeCategory === "all" ? ( // Show all categories
{groupedGames.map((category, index) => ( ))}
) : ( // Show only selected category

{activeCategoryData?.provider_name || "Games"}

{displayedGames.map((game, idx) => ( ))}
)}
); }