From 968c2c9b8aeb4368657ebf7d0b147ce78aa7b597 Mon Sep 17 00:00:00 2001 From: Sewmina Date: Sun, 13 Apr 2025 14:06:06 +0000 Subject: [PATCH] prod v1 --- src/components/HeroSection.tsx | 14 +++++++++----- src/components/OpenGames.tsx | 16 ++++++++++------ src/components/YourGames.tsx | 24 +++++++++++++++--------- src/data/shared.ts | 4 +++- src/shared/solana_helpers.ts | 2 +- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/components/HeroSection.tsx b/src/components/HeroSection.tsx index ec39fc1..c466e8d 100644 --- a/src/components/HeroSection.tsx +++ b/src/components/HeroSection.tsx @@ -54,14 +54,19 @@ export default function HeroSection() { const updateBets = async () => { - if (!ready || wallets.length === 0) return; - - const wallet = wallets.find((_wallet) => _wallet.type === "solana") || wallets[0]; - setSolWallet(wallet); + if(ready){ + const wallet = wallets.find((_wallet) => _wallet.type === "solana") || wallets[0]; + setSolWallet(wallet); + } const fetchedBets = await fetchOpenBets(); const filteredBets = fetchedBets.filter((bet) => !(bet.owner_id && bet.joiner_id)); const filledBets = fetchedBets.filter((bet) => bet.owner_id && bet.joiner_id); + setBets(filteredBets); + + if (!ready || wallets.length === 0) return; + + let activeBet = filledBets.find((bet) => bet.owner_id === user?.id || bet.joiner_id === user?.id); if(activeBet){ @@ -81,7 +86,6 @@ export default function HeroSection() { setMyActiveBet(undefined); return; } - setBets(filteredBets); if (!myActiveBet && lastActiveBet?.address !== activeBet?.address) { setMyActiveBet(activeBet); diff --git a/src/components/OpenGames.tsx b/src/components/OpenGames.tsx index 75813a5..4d27080 100644 --- a/src/components/OpenGames.tsx +++ b/src/components/OpenGames.tsx @@ -14,7 +14,7 @@ interface GameModalProps { } export default function YourGames({ bets }: GameModalProps) { - const { wallets, ready } = useSolanaWallets(); + const { wallets } = useSolanaWallets(); const [myBets, setMyBets] = useState([]); const [loading, setLoading] = useState(true); const { user } = usePrivy(); @@ -62,14 +62,19 @@ export default function YourGames({ bets }: GameModalProps) { }; const updateBets = async () => { - let wallet = wallets[0]; - wallets.forEach((_wallet) => { + let filteredBets = bets; + if(user){ + let wallet = wallets[0]; + wallets.forEach((_wallet) => { if (wallet.type === "solana") { wallet = _wallet; } - }); + }); + filteredBets = bets.filter((bet) => bet.owner !== wallet.address); + }else{ + console.log("No user found, showing all bets"); + } - const filteredBets = bets.filter((bet) => bet.owner !== wallet.address); const enrichedBets = await Promise.all( filteredBets.map(async (bet) => { const ownerProfile = await fetchUserById(bet.owner_id); @@ -86,7 +91,6 @@ export default function YourGames({ bets }: GameModalProps) { }; useEffect(() => { - if (!ready) return; updateBets(); const interval = setInterval(updateBets, 10000); return () => clearInterval(interval); diff --git a/src/components/YourGames.tsx b/src/components/YourGames.tsx index f002835..38d897f 100644 --- a/src/components/YourGames.tsx +++ b/src/components/YourGames.tsx @@ -3,7 +3,7 @@ import { useEffect, useState } from "react"; import Image from "next/image"; import { games } from "../data/games"; import { FiTrash } from "react-icons/fi"; -import { useSolanaWallets } from "@privy-io/react-auth"; +import { usePrivy, useSolanaWallets } from "@privy-io/react-auth"; import { closeBet } from "@/shared/solana_helpers"; import { Bet } from "../types/Bet"; import { toast } from "sonner"; @@ -13,6 +13,7 @@ interface GameModalProps { bets: Bet[]; } export default function YourGames({bets}:GameModalProps) { + const {user} = usePrivy(); const { wallets, ready } = useSolanaWallets(); const [myBets, setMyBets] = useState([]); const [loading, setLoading] = useState(true); @@ -21,13 +22,16 @@ export default function YourGames({bets}:GameModalProps) { // Fetch bets const updateBets = async () => { - let wallet = wallets[0]; - wallets.forEach((_wallet) => { - if (wallet.type === "solana") { - wallet = _wallet; - } - }); + if(ready && user){ + let wallet = wallets[0]; + wallets.forEach((_wallet) => { + if (wallet.type === "solana") { + wallet = _wallet; + } + }); setMyBets(bets.filter((bet) => bet.owner === wallet.address)); + + } setLoading(false); }; @@ -78,13 +82,14 @@ export default function YourGames({bets}:GameModalProps) { return (
-

Your Game

+ {loading ? (

Loading...

- ) : myBets.length === 0 ? ( + ) : !user || !ready || myBets.length === 0 ? ( <> ) : ( + <>

Your Game

{myBets.map((bet) => { console.log(`Bet id:${bet.id}`); @@ -122,6 +127,7 @@ export default function YourGames({bets}:GameModalProps) { ); })}
+ )} {/* Confirmation Modal */} diff --git a/src/data/shared.ts b/src/data/shared.ts index 52fb071..37c2fe2 100644 --- a/src/data/shared.ts +++ b/src/data/shared.ts @@ -2,7 +2,9 @@ import { Connection } from "@solana/web3.js"; // Replace this URL with your dedicated RPC endpoint // You can get one from providers like QuickNode, Alchemy, Helius, or GenesysGo -export const CLUSTER_URL = "https://tiniest-cold-darkness.solana-mainnet.quiknode.pro/72332d636ff78d498b880bd8fdc3eb646c827da8/"; +// export const CLUSTER_URL = "https://tiniest-cold-darkness.solana-mainnet.quiknode.pro/72332d636ff78d498b880bd8fdc3eb646c827da8/"; +// export const CLUSTER_URL = "https://go.getblock.io/908837801b534ae7a6f0869fc44cc567"; +export const CLUSTER_URL = "https://solana-mainnet.core.chainstack.com/c54e14eef17693283a0323efcc4ce731"; export const EXPLORER_ADDRESS_TEMPLATE = "https://explorer.solana.com/address/{address}"; export const EXPLORER_TX_TEMPLATE = "https://explorer.solana.com/tx/{address}"; export const connection = new Connection(CLUSTER_URL); diff --git a/src/shared/solana_helpers.ts b/src/shared/solana_helpers.ts index e10c189..805f9cd 100644 --- a/src/shared/solana_helpers.ts +++ b/src/shared/solana_helpers.ts @@ -12,7 +12,7 @@ export async function fetchOpenBets(): Promise { const response = await fetch(`${VALIDATOR_URL}fetchBets`); const data = await response.json(); - + console.log(`Fetched ${data.length} open bets`); return data; }