Skip to content

Commit d8ea14d

Browse files
feat: add join button on game page (#13)
1 parent e0b7f5d commit d8ea14d

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

game/src/app/game/[slug]/page.tsx

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import { GameStatus, getGame } from "@/api/api";
33
import { useAction } from "@/api/useAction";
44
import { GameActions } from "@/components/game-actions/game-actions";
5+
import { Button } from "@/components/ui/button";
56
import { useAddress } from "@/hooks/useAddress";
67
import { ZeroAddress } from "@/lib/constants";
7-
import { boardInfo, formatAddress } from "@/lib/utils";
8+
import { boardInfo } from "@/lib/utils";
89
import { usePrivy } from "@privy-io/react-auth";
910
import { Chess, Move } from "chess.js";
1011
import { useRouter } from "next/navigation";
11-
import { useCallback, useEffect, useState, useRef } from "react";
12+
import { useCallback, useEffect, useRef, useState } from "react";
1213
import { Chessboard } from "react-chessboard";
1314
import useSWR from "swr";
1415
import useSound from "use-sound";
@@ -23,7 +24,8 @@ export default function Game(props: GameProps) {
2324
const { params } = props;
2425
const { slug } = params;
2526
const { walletAddress, renderString } = useAddress();
26-
const { ready } = usePrivy();
27+
const { ready, authenticated } = usePrivy();
28+
const [joining, setJoining] = useState(false);
2729
const router = useRouter();
2830
const {
2931
data: remoteGame,
@@ -168,6 +170,33 @@ export default function Game(props: GameProps) {
168170
}
169171
};
170172

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+
171200
return (
172201
<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">
173202
<div className="flex flex-col gap-4">
@@ -205,17 +234,13 @@ export default function Game(props: GameProps) {
205234
<div className="p-2 bg-gray-800 rounded">
206235
<p className="uppercase font-mono tracking-widest">Player W</p>
207236
<p className="font-mono font-extrabold text-sm break-all">
208-
{remoteGame.w === walletAddress
209-
? "You"
210-
: formatAddress(remoteGame.w)}
237+
{renderPlayerInfo(remoteGame.w)}
211238
</p>
212239
</div>
213240
<div className="p-2 bg-gray-800 rounded text-right">
214241
<p className="uppercase font-mono tracking-widest">Player B</p>
215242
<p className="font-mono font-extrabold text-sm break-all">
216-
{remoteGame.b === walletAddress
217-
? "You"
218-
: formatAddress(remoteGame.b)}
243+
{renderPlayerInfo(remoteGame.b)}
219244
</p>
220245
</div>
221246
</div>

game/src/hooks/useAddress.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { ZeroAddress } from "@/lib/constants";
22
import { formatAddress } from "@/lib/utils";
3-
import { usePrivy, WalletWithMetadata } from "@privy-io/react-auth";
3+
import { usePrivy } from "@privy-io/react-auth";
44

55
export const useAddress = () => {
66
const { user } = usePrivy();
77
const walletAddress = user?.wallet?.address;
8-
const connectedWallet = (user?.linkedAccounts as WalletWithMetadata[])?.find(
9-
(a) => a.connectorType !== "embedded"
10-
)?.address!;
118

129
const renderString = (address: string) => {
1310
if (walletAddress === address) {

0 commit comments

Comments
 (0)