@@ -5,9 +5,10 @@ import { useCloseGame } from "../_hooks/useCloseGame";
55import { AnimalSelector } from "./AnimalSelector" ;
66import { BetCard } from "./BetCard" ;
77import { BetForm } from "./BetForm" ;
8+ import { useQueryClient } from "@tanstack/react-query" ;
89import { toast } from "react-hot-toast" ;
910import { formatEther , parseEther } from "viem" ;
10- import { useAccount } from "wagmi" ;
11+ import { useAccount , useChainId } from "wagmi" ;
1112import { ArrowPathIcon , InformationCircleIcon , ShareIcon } from "@heroicons/react/24/outline" ;
1213import { Address } from "~~/components/scaffold-eth" ;
1314import { HoverCard , HoverCardContent , HoverCardTrigger } from "~~/components/ui/hover-card" ;
@@ -24,9 +25,11 @@ export const GameDetailsModal: React.FC<GameDetailsModalProps> = ({ gameId, onCl
2425 const [ selectedNumber , setSelectedNumber ] = useState < number | null > ( null ) ;
2526 const [ betAmount , setBetAmount ] = useState ( "" ) ;
2627 const { address, connector, chainId } = useAccount ( ) ;
28+ const queryClient = useQueryClient ( ) ;
2729 const { data : balance } = useWatchBalance ( {
2830 address,
2931 } ) ;
32+ const defaultChainId = useChainId ( ) ;
3033
3134 const { data : game , refetch : refetchGame } = useScaffoldReadContract ( {
3235 contractName : "Jodobix" ,
@@ -43,7 +46,6 @@ export const GameDetailsModal: React.FC<GameDetailsModalProps> = ({ gameId, onCl
4346
4447 const {
4548 data : myBets ,
46- refetch : refetchMyBets ,
4749 isLoading : isLoadingMyBets ,
4850 error : errorMyBets ,
4951 } = useBetsPlaced ( {
@@ -100,9 +102,27 @@ export const GameDetailsModal: React.FC<GameDetailsModalProps> = ({ gameId, onCl
100102 setSelectedNumber ( number ) ;
101103 } ;
102104
103- const handleBetPlaced = ( betId : bigint ) => {
105+ const handleBetPlaced = ( betId : bigint , gameId : bigint , number : number ) => {
106+ // Optimistic UI update
107+ queryClient . setQueryData (
108+ [ "betPlaceds" , defaultChainId , gameId ?. toString ( ) , undefined , address ] ,
109+ [
110+ ...( myBets ?? [ ] ) ,
111+ {
112+ betId : betId . toString ( ) ,
113+ gameId : gameId . toString ( ) ,
114+ number,
115+ bettor : address ,
116+ value : parseEther ( betAmount ) . toString ( ) ,
117+ timestamp : Date . now ( ) . toString ( ) ,
118+ blockNumber : "0" ,
119+ blockTimestamp : "0" ,
120+ transactionHash : "0x" ,
121+ } ,
122+ ] ,
123+ ) ;
124+
104125 resetBetForm ( ) ;
105- refetchMyBets ( ) ;
106126 onBetPlaced ?.( betId ) ;
107127 } ;
108128
@@ -151,7 +171,7 @@ export const GameDetailsModal: React.FC<GameDetailsModalProps> = ({ gameId, onCl
151171
152172 return (
153173 < div className = "modal modal-open" >
154- < div className = "modal-box w-11/12 max-w-5xl h-full flex flex-col dark:bg- " >
174+ < div className = "modal-box w-11/12 max-w-5xl h-full flex flex-col" >
155175 < div className = "flex justify-between items-center mb-4" >
156176 < h3 className = "font-bold text-lg" >
157177 Game #{ gameId . toString ( ) }
@@ -179,15 +199,15 @@ export const GameDetailsModal: React.FC<GameDetailsModalProps> = ({ gameId, onCl
179199 </ button >
180200 </ div >
181201
182- < div className = "flex gap-8 md:h-[calc(100%-3.3rem)] items-start flex-col md:flex-row" >
202+ < div className = "flex gap-8 items-start flex-col md:flex-row pb-8 " >
183203 < AnimalSelector
184204 selectedNumber = { selectedNumber }
185205 onSelectNumber = { handleSelectNumber }
186206 betsPerNumber = { Array . from ( game ?. betsPerNumber || [ ] ) }
187207 drawnNumber = { game ?. bettingPeriodEnded ? game ?. drawnNumber : undefined }
188208 />
189209
190- < div className = "flex flex-col gap-2 h-full overflow-y-auto overflow-x-hidden self-center" >
210+ < div className = "flex flex-col gap-2 h-full self-center" >
191211 < div >
192212 Total Value: { formatEther ( game ?. totalValue || 0n ) } ETH ({ game ?. numberOfBets . toString ( ) } bets)
193213 </ div >
0 commit comments