This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/projects/website/src/components/leaderboard/leaderboard-data.tsx

70 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-10-04 18:25:37 +01:00
"use client";
import LeaderboardScores from "@/components/leaderboard/leaderboard-scores";
import { LeaderboardInfo } from "@/components/leaderboard/leaderboard-info";
import ScoreSaberScore from "@ssr/common/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import { LeaderboardResponse } from "@ssr/common/response/leaderboard-response";
2024-10-04 18:25:37 +01:00
import { useQuery } from "@tanstack/react-query";
2024-10-19 14:11:43 +01:00
import { useEffect, useState } from "react";
import { fetchLeaderboard } from "@ssr/common/utils/leaderboard.util";
2024-10-17 15:47:20 +01:00
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";
2024-10-21 06:47:53 +01:00
import LeaderboardPpChart from "@/components/leaderboard/leaderboard-pp-chart";
2024-10-04 18:25:37 +01:00
const REFRESH_INTERVAL = 1000 * 60 * 5;
2024-10-04 18:25:37 +01:00
type LeaderboardDataProps = {
2024-10-04 18:25:37 +01:00
/**
* The initial leaderboard data.
2024-10-04 18:25:37 +01:00
*/
initialLeaderboard: LeaderboardResponse<ScoreSaberLeaderboard>;
2024-10-04 18:25:37 +01:00
/**
* The initial score data.
2024-10-04 18:25:37 +01:00
*/
2024-10-17 15:47:20 +01:00
initialScores?: LeaderboardScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
2024-10-19 14:11:43 +01:00
/**
* The initial page.
*/
initialPage?: number;
2024-10-04 18:25:37 +01:00
};
2024-10-19 14:11:43 +01:00
export function LeaderboardData({ initialLeaderboard, initialScores, initialPage }: LeaderboardDataProps) {
const [currentLeaderboardId, setCurrentLeaderboardId] = useState(initialLeaderboard.leaderboard.id);
2024-10-19 14:11:43 +01:00
const [currentLeaderboard, setCurrentLeaderboard] = useState(initialLeaderboard);
const { data, isLoading, isError } = useQuery({
queryKey: ["leaderboard", currentLeaderboardId],
queryFn: async (): Promise<LeaderboardResponse<ScoreSaberLeaderboard> | undefined> => {
return fetchLeaderboard<ScoreSaberLeaderboard>("scoresaber", currentLeaderboardId + "");
},
refetchInterval: REFRESH_INTERVAL,
refetchIntervalInBackground: false,
2024-10-04 18:25:37 +01:00
});
2024-10-19 14:11:43 +01:00
useEffect(() => {
if (data) {
setCurrentLeaderboard(data);
}
}, [data]);
2024-10-04 18:25:37 +01:00
2024-10-21 06:47:53 +01:00
const leaderboard = currentLeaderboard.leaderboard;
2024-10-04 18:25:37 +01:00
return (
<main className="flex flex-col-reverse xl:flex-row w-full gap-2">
<LeaderboardScores
2024-10-21 06:47:53 +01:00
leaderboard={leaderboard}
2024-10-04 18:25:37 +01:00
initialScores={initialScores}
2024-10-19 14:11:43 +01:00
initialPage={initialPage}
leaderboardChanged={newId => setCurrentLeaderboardId(newId)}
2024-10-04 18:25:37 +01:00
showDifficulties
isLeaderboardPage
/>
2024-10-21 06:47:53 +01:00
<div className="flex flex-col gap-2 w-full xl:w-[500px]">
<LeaderboardInfo leaderboard={leaderboard} beatSaverMap={currentLeaderboard.beatsaver} />
{leaderboard.stars > 0 && <LeaderboardPpChart leaderboard={leaderboard} />}
</div>
2024-10-04 18:25:37 +01:00
</main>
);
}