From 7269a46b9733b0e938afef5601d2dba6a73389d5 Mon Sep 17 00:00:00 2001 From: ametel01 Date: Tue, 6 Feb 2024 13:27:04 +0800 Subject: [PATCH] fixed leaderboards --- .../components/ui/LogoutAndRankContainer.tsx | 70 ++++++++++++++----- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/packages/frontend/src/components/ui/LogoutAndRankContainer.tsx b/packages/frontend/src/components/ui/LogoutAndRankContainer.tsx index c1838f1..c514eca 100644 --- a/packages/frontend/src/components/ui/LogoutAndRankContainer.tsx +++ b/packages/frontend/src/components/ui/LogoutAndRankContainer.tsx @@ -1,11 +1,8 @@ -import React, { useMemo } from 'react'; +import React, { useMemo, useEffect, useState } from 'react'; import styled from 'styled-components'; import { Typography } from '@mui/material'; -import { useContractRead } from '@starknet-react/core'; import nogameLogo from '../../assets/logos/NoGameLogo.webp'; -import { BlockTag } from 'starknet'; -import { GAMEADDRESS } from '../../constants/addresses'; -import game from '../../constants/nogame.json'; + import { numberWithCommas } from '../../shared/utils'; const LogoContainer = styled.div` @@ -56,23 +53,64 @@ const StyledImage = styled.img` objectfit: contain; `; +interface LeaderboardEntry { + planet_id: string; // Assuming planet_id is a string based on your initial data + account: string; + net_amount: string; // Assuming net_amount is a string that should be converted to a number +} + interface Props { planetId: number; } const LogoAndRankContainer = ({ planetId }: Props) => { - const { data: points } = useContractRead({ - address: GAMEADDRESS, - abi: game.abi, - functionName: 'get_planet_points', - args: [Number(planetId)], - watch: false, - blockIdentifier: BlockTag.pending, - }); + const [leaderboard, setLeaderboard] = useState([]); + const [error, setError] = useState(''); + const [isLoading, setIsLoading] = useState(true); + + const nodeEnv = import.meta.env.VITE_NODE_ENV; + const apiUrl = + nodeEnv === 'production' + ? 'https://www.api.testnet.no-game.xyz/leaderboard' + : 'http://localhost:3001/leaderboard'; + + useEffect(() => { + const fetchLeaderboard = async () => { + try { + const response = await fetch(apiUrl); + if (!response.ok) { + throw new Error('Something went wrong!'); + } + const data = await response.json(); + setLeaderboard(data); + } catch (error) { + if (error instanceof Error) { + setError(error.message); + } else { + setError('An unexpected error occurred'); + } + } finally { + setIsLoading(false); + } + }; + + fetchLeaderboard(); + }, [apiUrl]); const score = useMemo(() => { - return points ? Number(points) : ''; - }, [points]); + const planetData = leaderboard.find( + (planet) => planet.planet_id === planetId.toString() + ); + return planetData ? Number(planetData.net_amount) : 0; + }, [leaderboard, planetId]); + + if (isLoading) { + return
Loading...
; + } + + if (error) { + return
Error: {error}
; + } return ( @@ -82,7 +120,7 @@ const LogoAndRankContainer = ({ planetId }: Props) => { Score - {numberWithCommas(Number(score))} + {numberWithCommas(Math.ceil(score / 1000))}