"use client"; import { useEffect, useState } from "react"; import { usePrivy, useSolanaWallets } from "@privy-io/react-auth"; import { toast } from "sonner"; import { games } from "../data/games"; import { PriceSelection } from "./PriceSelection"; import { GameSelection } from "./GameSelection"; import { createBet } from "@/shared/solana_helpers"; import { Game } from "@/types/Game"; import { connection, EXPLORER_TX_TEMPLATE } from "@/data/shared"; import { CONFIRMATION_THRESHOLD } from "@/shared/constants"; interface GameModalProps { isOpen: boolean; onClose: () => void; } export default function GameModal({ isOpen, onClose }: GameModalProps) { const { wallets } = useSolanaWallets(); const { authenticated, user } = usePrivy(); const [selectedGame, setSelectedGame] = useState(null); const [selectedPrice, setSelectedPrice] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const [shouldRender, setShouldRender] = useState(isOpen); const [animationClass, setAnimationClass] = useState(""); useEffect(() => { if (isOpen) { setShouldRender(true); setAnimationClass("modal-enter"); } else { setAnimationClass("modal-exit"); const timer = setTimeout(() => setShouldRender(false), 200); return () => clearTimeout(timer); } }, [isOpen]); const handleCreateGame = async () => { if (!authenticated) { toast.error("Please log in with Privy."); return; } let wallet = wallets[0]; wallets.forEach((_wallet) => { if (_wallet.type === "solana") { wallet = _wallet; } }); if (!wallet) { toast.error("Please connect your wallet."); return; } if (!selectedGame || selectedPrice === null) { toast.error("Please select a game and a price."); return; } setIsProcessing(true); toast.loading("Creating Game"); try { const tx = await createBet(wallet, user?.id ?? "", selectedPrice, selectedGame.id, false); const url = EXPLORER_TX_TEMPLATE.replace("{address}", tx); if (tx.length > 5) { connection.confirmTransaction(tx, CONFIRMATION_THRESHOLD).finally(()=>{ toast.dismiss(); toast.success(`Game created successfully!`, { action: { label: "View TX", onClick: () => window.open(url, "_blank"), }, }); }) } onClose(); } catch (error) { console.error("Error creating bet:", error); toast.error("Failed to create Game. Please try again."); toast.dismiss(); } finally { setIsProcessing(false); } }; if (!shouldRender) return null; return (
e.stopPropagation()} > {isProcessing ? (

Processing...

Creating your bet, please wait...

) : ( <>

Create Game

)}
); }