"use client"; import Card from "@/components/Card"; import Container from "@/components/Container"; import Error from "@/components/Error"; import Pagination from "@/components/Pagination"; import { Spinner } from "@/components/Spinner"; import PlayerRanking from "@/components/player/PlayerRanking"; import PlayerRankingMobile from "@/components/player/PlayerRankingMobile"; import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { ScoreSaberAPI } from "@/utils/scoresaber/api"; import { normalizedRegionName } from "@/utils/utils"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import { useCallback, useEffect, useState } from "react"; import ReactCountryFlag from "react-country-flag"; type PageInfo = { loading: boolean; page: number; totalPages: number; players: ScoresaberPlayer[]; }; type RankingCountryProps = { params: { country: string }; }; export default function RankingCountry({ params }: RankingCountryProps) { const searchParams = useSearchParams(); const router = useRouter(); const country = params.country; let page; const pageString = searchParams.get("page"); if (pageString == null) { page = 1; } else { page = Number.parseInt(pageString) || 1; } const [error, setError] = useState(false); const [errorMessage, setErrorMessage] = useState(""); const [pageInfo, setPageInfo] = useState({ loading: true, page: page, totalPages: 1, players: [], }); const updatePage = useCallback( (page: any) => { console.log("Switching page to", page); ScoreSaberAPI.fetchTopPlayers(page, country).then((response) => { if (!response) { setError(true); setErrorMessage("No players found"); setPageInfo({ ...pageInfo, loading: false }); return; } setPageInfo({ ...pageInfo, players: response.players, totalPages: response.pageInfo.totalPages, loading: false, page: page, }); if (page > 1) { router.push(`/ranking/country/${country}?page=${page}`, { scroll: false, }); } else { router.push(`/ranking/country/${country}`, { scroll: false, }); } }); }, [country, pageInfo, router], ); useEffect(() => { if (!pageInfo.loading || error) return; updatePage(pageInfo.page); }, [error, country, updatePage, pageInfo.page, pageInfo.loading]); if (pageInfo.loading || error) { return (
{error && } {!error && }
); } const players = pageInfo.players; return (
{pageInfo.loading ? (
) : (

You are viewing scores from {normalizedRegionName(country)}

{players.map((player) => ( ))}
Rank Profile Performance Points Total Plays Total Ranked Plays Avg Ranked Accuracy
{players.map((player) => (
))}
{/* Pagination */}
{ updatePage(page); }} />
)}
); }