scoresaber-reloaded-v2/src/components/GlobalRanking.tsx

197 lines
6.0 KiB
TypeScript
Raw Normal View History

2023-10-23 06:01:55 +00:00
"use client";
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
2023-10-24 10:18:51 +00:00
import { ScoreSaberAPI } from "@/utils/scoresaber/api";
2023-10-23 06:01:55 +00:00
import { normalizedRegionName } from "@/utils/utils";
import dynamic from "next/dynamic";
2023-10-23 06:01:55 +00:00
import Link from "next/link";
2023-10-27 22:48:42 +00:00
import { useRouter, useSearchParams } from "next/navigation";
2023-10-24 10:18:51 +00:00
import { useCallback, useEffect, useState } from "react";
2023-10-28 17:00:11 +00:00
import Card from "./Card";
import Container from "./Container";
import CountyFlag from "./CountryFlag";
import Pagination from "./Pagination";
import Spinner from "./Spinner";
import PlayerRanking from "./player/PlayerRanking";
import PlayerRankingMobile from "./player/PlayerRankingMobile";
2023-10-30 16:53:55 +00:00
import { Separator } from "./ui/separator";
2023-10-23 06:01:55 +00:00
2023-10-24 10:18:51 +00:00
const Error = dynamic(() => import("@/components/Error"));
2023-10-23 06:01:55 +00:00
2023-10-24 10:18:51 +00:00
type PageInfo = {
loading: boolean;
2023-10-23 06:01:55 +00:00
page: number;
2023-10-23 17:09:16 +00:00
totalPages: number;
players: ScoresaberPlayer[];
2023-10-23 06:01:55 +00:00
};
2023-10-24 10:18:51 +00:00
type GlobalRankingProps = {
page: number;
country?: string;
};
export default function GlobalRanking({ page, country }: GlobalRankingProps) {
2023-10-27 22:48:42 +00:00
const router = useRouter();
const searchQuery = useSearchParams();
const isMobile = searchQuery.get("mobile") == "true";
2023-10-24 10:18:51 +00:00
const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const [pageInfo, setPageInfo] = useState<PageInfo>({
loading: true,
page: page,
totalPages: 1,
players: [],
});
const updatePage = useCallback(
(page: any) => {
2023-10-27 22:48:42 +00:00
const windowSize = document.documentElement.clientWidth;
if (windowSize < 768 && !isMobile) {
router.push(`/ranking/global/${page}?mobile=true`);
router.refresh();
return;
}
2023-10-24 10:18:51 +00:00
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,
});
window.history.pushState(
{},
"",
country
? `/ranking/country/${country}/${page}`
: `/ranking/global/${page}`,
);
2023-10-24 10:18:51 +00:00
});
},
2023-10-27 22:48:42 +00:00
[country, isMobile, pageInfo, router],
2023-10-24 10:18:51 +00:00
);
useEffect(() => {
if (!pageInfo.loading || error) return;
updatePage(pageInfo.page);
}, [error, country, updatePage, pageInfo.page, pageInfo.loading]);
if (pageInfo.loading || error) {
return (
<main>
<Container>
<Card className="mt-2">
<div className="p-3 text-center">
<div role="status">
<div className="flex flex-col items-center justify-center gap-2">
{error && <Error errorMessage={errorMessage} />}
{!error && <Spinner />}
</div>
</div>
</div>
</Card>
</Container>
</main>
);
}
const players = pageInfo.players;
2023-10-23 06:01:55 +00:00
return (
2023-10-24 10:18:51 +00:00
<main>
<Container>
<Card className="mt-2">
{pageInfo.loading ? (
<div className="flex justify-center">
<Spinner />
2023-10-23 17:09:16 +00:00
</div>
2023-10-24 10:18:51 +00:00
) : (
<div className="flex flex-col gap-2">
2023-10-30 16:53:55 +00:00
<div className="flex items-center gap-2 p-2">
2023-10-24 10:18:51 +00:00
{country && (
2023-10-27 22:37:57 +00:00
<CountyFlag countryCode={country} className="!h-8 !w-8" />
2023-10-24 10:18:51 +00:00
)}
<p>
You are viewing{" "}
{country
? "scores from " + normalizedRegionName(country)
: "Global scores"}
</p>
</div>
2023-10-30 16:53:55 +00:00
<Separator />
2023-10-27 22:48:42 +00:00
{!isMobile && (
<table className="table w-full table-auto border-spacing-2 border-none text-left">
<thead>
<tr>
<th className="px-4 py-2">Rank</th>
<th className="px-4 py-2">Profile</th>
<th className="px-4 py-2">Performance Points</th>
<th className="px-4 py-2">Total Plays</th>
<th className="px-4 py-2">Total Ranked Plays</th>
<th className="px-4 py-2">Avg Ranked Accuracy</th>
2023-10-24 10:18:51 +00:00
</tr>
2023-10-27 22:48:42 +00:00
</thead>
<tbody className="border-none">
{players.map((player) => (
<tr
key={player.rank}
2023-10-30 16:53:55 +00:00
className="border-b border-gray-800"
2023-10-27 22:48:42 +00:00
>
<PlayerRanking
showCountryFlag={country ? false : true}
player={player}
/>
</tr>
))}
</tbody>
</table>
)}
{isMobile && (
<div className="flex flex-col gap-2">
{players.map((player) => (
<div
key={player.rank}
className="flex flex-col gap-2 rounded-md bg-gray-700 hover:bg-gray-600"
>
<Link href={`/player/${player.id}/top/1`}>
<PlayerRankingMobile player={player} />
</Link>
</div>
2023-10-24 10:18:51 +00:00
))}
2023-10-27 22:48:42 +00:00
</div>
)}
2023-10-24 10:18:51 +00:00
{/* Pagination */}
2023-10-30 16:53:55 +00:00
<div className="flex w-full flex-row justify-center">
<div className="pt-3">
2023-10-24 10:18:51 +00:00
<Pagination
currentPage={pageInfo.page}
totalPages={pageInfo.totalPages}
onPageChange={(page) => {
updatePage(page);
}}
/>
</div>
</div>
</div>
)}
</Card>
</Container>
</main>
2023-10-23 06:01:55 +00:00
);
}