add mobile check
All checks were successful
deploy / deploy (push) Successful in 1m3s

This commit is contained in:
Lee 2023-10-27 23:48:42 +01:00
parent 39efca8fb7
commit 4d6ece7a3b

@ -5,6 +5,7 @@ import { ScoreSaberAPI } from "@/utils/scoresaber/api";
import { normalizedRegionName } from "@/utils/utils"; import { normalizedRegionName } from "@/utils/utils";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import Link from "next/link"; import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import Card from "../Card"; import Card from "../Card";
import Container from "../Container"; import Container from "../Container";
@ -29,6 +30,10 @@ type GlobalRankingProps = {
}; };
export default function GlobalRanking({ page, country }: GlobalRankingProps) { export default function GlobalRanking({ page, country }: GlobalRankingProps) {
const router = useRouter();
const searchQuery = useSearchParams();
const isMobile = searchQuery.get("mobile") == "true";
const [error, setError] = useState(false); const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState(""); const [errorMessage, setErrorMessage] = useState("");
@ -41,6 +46,13 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
const updatePage = useCallback( const updatePage = useCallback(
(page: any) => { (page: any) => {
const windowSize = document.documentElement.clientWidth;
if (windowSize < 768 && !isMobile) {
router.push(`/ranking/global/${page}?mobile=true`);
router.refresh();
return;
}
console.log("Switching page to", page); console.log("Switching page to", page);
ScoreSaberAPI.fetchTopPlayers(page, country).then((response) => { ScoreSaberAPI.fetchTopPlayers(page, country).then((response) => {
if (!response) { if (!response) {
@ -65,7 +77,7 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
); );
}); });
}, },
[country, pageInfo], [country, isMobile, pageInfo, router],
); );
useEffect(() => { useEffect(() => {
@ -117,7 +129,8 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
</p> </p>
</div> </div>
<table className="hidden w-full table-auto border-spacing-2 border-none text-left md:table"> {!isMobile && (
<table className="table w-full table-auto border-spacing-2 border-none text-left">
<thead> <thead>
<tr> <tr>
<th className="px-4 py-2">Rank</th> <th className="px-4 py-2">Rank</th>
@ -130,7 +143,10 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
</thead> </thead>
<tbody className="border-none"> <tbody className="border-none">
{players.map((player) => ( {players.map((player) => (
<tr key={player.rank} className="border-b border-gray-700"> <tr
key={player.rank}
className="border-b border-gray-700"
>
<PlayerRanking <PlayerRanking
showCountryFlag={country ? false : true} showCountryFlag={country ? false : true}
player={player} player={player}
@ -139,8 +155,10 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
))} ))}
</tbody> </tbody>
</table> </table>
)}
<div className="flex flex-col gap-2 md:hidden"> {isMobile && (
<div className="flex flex-col gap-2">
{players.map((player) => ( {players.map((player) => (
<div <div
key={player.rank} key={player.rank}
@ -152,6 +170,7 @@ export default function GlobalRanking({ page, country }: GlobalRankingProps) {
</div> </div>
))} ))}
</div> </div>
)}
{/* Pagination */} {/* Pagination */}
<div className="flex w-full flex-row justify-center rounded-md bg-gray-800 md:flex-col"> <div className="flex w-full flex-row justify-center rounded-md bg-gray-800 md:flex-col">