2
2
import { GameStatus , getGame } from "@/api/api" ;
3
3
import { useAction } from "@/api/useAction" ;
4
4
import { GameActions } from "@/components/game-actions/game-actions" ;
5
+ import { Button } from "@/components/ui/button" ;
5
6
import { useAddress } from "@/hooks/useAddress" ;
6
7
import { ZeroAddress } from "@/lib/constants" ;
7
- import { boardInfo , formatAddress } from "@/lib/utils" ;
8
+ import { boardInfo } from "@/lib/utils" ;
8
9
import { usePrivy } from "@privy-io/react-auth" ;
9
10
import { Chess , Move } from "chess.js" ;
10
11
import { useRouter } from "next/navigation" ;
11
- import { useCallback , useEffect , useState , useRef } from "react" ;
12
+ import { useCallback , useEffect , useRef , useState } from "react" ;
12
13
import { Chessboard } from "react-chessboard" ;
13
14
import useSWR from "swr" ;
14
15
import useSound from "use-sound" ;
@@ -23,7 +24,8 @@ export default function Game(props: GameProps) {
23
24
const { params } = props ;
24
25
const { slug } = params ;
25
26
const { walletAddress, renderString } = useAddress ( ) ;
26
- const { ready } = usePrivy ( ) ;
27
+ const { ready, authenticated } = usePrivy ( ) ;
28
+ const [ joining , setJoining ] = useState ( false ) ;
27
29
const router = useRouter ( ) ;
28
30
const {
29
31
data : remoteGame ,
@@ -168,6 +170,33 @@ export default function Game(props: GameProps) {
168
170
}
169
171
} ;
170
172
173
+ const handleJoinGame = async ( gameId : string ) => {
174
+ setJoining ( true ) ;
175
+ await submit ( "joinGame" , { gameId } ) ;
176
+ setJoining ( false ) ;
177
+ } ;
178
+
179
+ const renderPlayerInfo = ( player : string ) => {
180
+ if ( player === walletAddress ) {
181
+ return "You" ;
182
+ }
183
+ if ( player === ZeroAddress ) {
184
+ if ( authenticated ) {
185
+ return (
186
+ < Button
187
+ className = "h-[22px]"
188
+ size = { "sm" }
189
+ onClick = { ( ) => handleJoinGame ( slug ) }
190
+ >
191
+ { joining ? "Joining..." : "Join" }
192
+ </ Button >
193
+ ) ;
194
+ }
195
+ return "Not Joined" ;
196
+ }
197
+ return renderString ( player ) ;
198
+ } ;
199
+
171
200
return (
172
201
< div className = "flex md:mt-8 m-auto w-full px-4 lg:py-0 lg:w-[1500px] flex-col md:flex-row gap-8 md:gap-0" >
173
202
< div className = "flex flex-col gap-4" >
@@ -205,17 +234,13 @@ export default function Game(props: GameProps) {
205
234
< div className = "p-2 bg-gray-800 rounded" >
206
235
< p className = "uppercase font-mono tracking-widest" > Player W</ p >
207
236
< p className = "font-mono font-extrabold text-sm break-all" >
208
- { remoteGame . w === walletAddress
209
- ? "You"
210
- : formatAddress ( remoteGame . w ) }
237
+ { renderPlayerInfo ( remoteGame . w ) }
211
238
</ p >
212
239
</ div >
213
240
< div className = "p-2 bg-gray-800 rounded text-right" >
214
241
< p className = "uppercase font-mono tracking-widest" > Player B</ p >
215
242
< p className = "font-mono font-extrabold text-sm break-all" >
216
- { remoteGame . b === walletAddress
217
- ? "You"
218
- : formatAddress ( remoteGame . b ) }
243
+ { renderPlayerInfo ( remoteGame . b ) }
219
244
</ p >
220
245
</ div >
221
246
</ div >
0 commit comments