"use client"; import { 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 { EXPLORER_TX_TEMPLATE } from "@/data/shared"; interface GameModalProps { isOpen: boolean; onClose: () => void; } export default function GameModal({ isOpen, onClose }: GameModalProps) { const { wallets } = useSolanaWallets(); const { authenticated } = usePrivy(); const [selectedGame, setSelectedGame] = useState(null); const [selectedPrice, setSelectedPrice] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const handleCreateGame = async () => { if (!authenticated) { toast.error("Please log in with Privy."); return; } const wallet = wallets[0]; 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); try { const tx = await createBet(wallet, selectedPrice, selectedGame); const url = EXPLORER_TX_TEMPLATE.replace("{address}", tx); toast.success(`Bet 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 bet. Please try again."); } finally { setIsProcessing(false); } }; if (!isOpen) return null; return (
e.stopPropagation()}> {isProcessing ? (

Processing...

Creating your bet, please wait...

) : ( <>

Create Game

)}
); }