"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { games } from "../data/games"; import { FiTrash } from "react-icons/fi"; import { useSolanaWallets } from "@privy-io/react-auth"; import { closeBet } from "@/shared/solana_helpers"; import { Bet } from "../types/Bet"; import { toast } from "sonner"; import { connection, EXPLORER_TX_TEMPLATE } from "@/data/shared"; import { CONFIRMATION_THRESHOLD } from "@/shared/constants"; interface GameModalProps { bets: Bet[]; } export default function YourGames({bets}:GameModalProps) { const { wallets, ready } = useSolanaWallets(); const [myBets, setMyBets] = useState([]); const [loading, setLoading] = useState(true); const [selectedBet, setSelectedBet] = useState(null); const [isProcessing, setIsProcessing] = useState(false); // Fetch bets const updateBets = async () => { let wallet = wallets[0]; wallets.forEach((_wallet) => { if (wallet.type === "solana") { wallet = _wallet; } }); setMyBets(bets.filter((bet) => bet.owner === wallet.address)); setLoading(false); }; useEffect(() => { if (!ready) return; updateBets(); const interval = setInterval(updateBets, 10000); return () => clearInterval(interval); }, [bets]); // Handle bet closing const handleCloseBet = async () => { if (!selectedBet) return; setIsProcessing(true); toast.loading("Closing bet"); let wallet = wallets[0]; wallets.forEach((_wallet) => { if (wallet.type === "solana") { wallet = _wallet; } }); try { const tx = await closeBet(wallet, selectedBet.id); const url = EXPLORER_TX_TEMPLATE.replace("{address}", tx); connection.confirmTransaction(tx, CONFIRMATION_THRESHOLD).finally(()=>{ toast.dismiss(); toast.success(`Closed the bet successfully!`, { action: { label: "View TX", onClick: () => window.open(url, "_blank"), }, }); updateBets(); }) } catch (error) { toast.dismiss(); console.error("Error closing bet:", error); toast.message("Failed to close the bet. Please try again."); } finally { setIsProcessing(false); setSelectedBet(null); } }; return (

Your Game

{loading ? (

Loading...

) : myBets.length === 0 ? ( <> ) : (
{myBets.map((bet) => { console.log(`Bet id:${bet.id}`); const game = games.find((g) => g.id === bet.id); if (!game) return null; return (
{/* Game Thumbnail */}
{game.name}
{/* Game Info */}

{game.name}

Entry

{bet.wager} SOL

{/* Close Button */}
); })}
)} {/* Confirmation Modal */} {selectedBet && !isProcessing && (

Close Game

Are you sure you want to close this Game? You will receive your refund back to your wallet.

)} {/* Processing Modal */} {isProcessing && (

Processing...

Please wait while we close your Game.

)}
); }