52 |
53 | {showItems && (
54 |
55 | {myNfts.map((nft) => (
56 |
57 |
{nft.name}
58 |
59 | {nft.url ? (
60 |
onNftClickedCallback(nft)}
62 | src={nft.url}
63 | alt="NFT Icon"
64 | width={64}
65 | height={64}
66 | />
67 | ) : (
68 | Error loading image
69 | )}
70 |
71 |
72 |
73 | ))}
74 |
75 | )}
76 |
77 | )}
78 | >
79 | );
80 | }
81 |
82 | export default DisplayNfts
83 |
--------------------------------------------------------------------------------
/app/components/InitPlayerButton.tsx:
--------------------------------------------------------------------------------
1 | import { useCallback, useState } from "react";
2 | import { Button } from "@chakra-ui/react";
3 | import { SystemProgram } from "@solana/web3.js";
4 | import { useConnection, useWallet } from "@solana/wallet-adapter-react";
5 | import { useGameState } from "@/contexts/GameStateProvider";
6 | import {
7 | GAME_DATA_SEED,
8 | gameDataPDA,
9 | getProgram,
10 | useAnchorProvider,
11 | } from "@/utils/anchor";
12 |
13 | const InitPlayerButton = () => {
14 | const { publicKey, sendTransaction, wallet } = useWallet();
15 | const { connection } = useConnection();
16 | const [isLoading, setIsLoading] = useState(false);
17 | const { gameState, playerDataPDA } = useGameState();
18 |
19 | const provider = useAnchorProvider();
20 | var program = getProgram(provider);
21 |
22 | // Init player button click handler
23 | const handleClick = useCallback(async () => {
24 | if (!publicKey || !playerDataPDA) return;
25 |
26 | setIsLoading(true);
27 |
28 | try {
29 | const transaction = await program.methods
30 | .initPlayer(GAME_DATA_SEED)
31 | .accountsStrict({
32 | player: playerDataPDA,
33 | gameData: gameDataPDA,
34 | signer: publicKey,
35 | systemProgram: SystemProgram.programId,
36 | })
37 | .transaction();
38 |
39 | const txSig = await sendTransaction(transaction, connection, {
40 | skipPreflight: true,
41 | });
42 |
43 | console.log(`https://explorer.solana.com/tx/${txSig}?cluster=devnet`);
44 | } catch (error) {
45 | console.log(error);
46 | } finally {
47 | setIsLoading(false); // set loading state back to false
48 | }
49 | }, [publicKey, playerDataPDA, connection]);
50 |
51 | return (
52 | <>
53 | {!gameState && publicKey && (
54 |