2024-10-04 18:25:37 +01:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import LeaderboardScores from "@/components/leaderboard/leaderboard-scores";
|
|
|
|
import { LeaderboardInfo } from "@/components/leaderboard/leaderboard-info";
|
2024-10-17 15:30:14 +01:00
|
|
|
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-17 15:30:14 +01:00
|
|
|
import { useState } from "react";
|
|
|
|
import { fetchLeaderboard } from "@ssr/common/utils/leaderboard.util";
|
|
|
|
import PlayerScoresResponse from "../../../../common/src/response/player-scores-response.ts";
|
2024-10-04 18:25:37 +01:00
|
|
|
|
2024-10-17 15:30:14 +01:00
|
|
|
const REFRESH_INTERVAL = 1000 * 60 * 5;
|
2024-10-04 18:25:37 +01:00
|
|
|
|
2024-10-17 15:30:14 +01:00
|
|
|
type LeaderboardDataProps = {
|
2024-10-04 18:25:37 +01:00
|
|
|
/**
|
2024-10-17 15:30:14 +01:00
|
|
|
* The initial leaderboard data.
|
2024-10-04 18:25:37 +01:00
|
|
|
*/
|
2024-10-17 15:30:14 +01:00
|
|
|
initialLeaderboard: LeaderboardResponse<ScoreSaberLeaderboard>;
|
2024-10-04 18:25:37 +01:00
|
|
|
|
|
|
|
/**
|
2024-10-17 15:30:14 +01:00
|
|
|
* The initial score data.
|
2024-10-04 18:25:37 +01:00
|
|
|
*/
|
2024-10-17 15:30:14 +01:00
|
|
|
initialScores: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
|
2024-10-04 18:25:37 +01:00
|
|
|
};
|
|
|
|
|
2024-10-17 15:30:14 +01:00
|
|
|
export function LeaderboardData({ initialLeaderboard, initialScores }: LeaderboardDataProps) {
|
|
|
|
const [currentLeaderboardId, setCurrentLeaderboardId] = useState(initialLeaderboard.leaderboard.id);
|
|
|
|
|
|
|
|
let leaderboard = 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-17 15:30:14 +01:00
|
|
|
if (data && (!isLoading || !isError)) {
|
|
|
|
leaderboard = data;
|
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-17 15:30:14 +01:00
|
|
|
leaderboard={leaderboard.leaderboard}
|
2024-10-04 18:25:37 +01:00
|
|
|
initialScores={initialScores}
|
2024-10-17 15:30:14 +01:00
|
|
|
leaderboardChanged={newId => setCurrentLeaderboardId(newId)}
|
2024-10-04 18:25:37 +01:00
|
|
|
showDifficulties
|
|
|
|
isLeaderboardPage
|
|
|
|
/>
|
2024-10-17 15:30:14 +01:00
|
|
|
<LeaderboardInfo leaderboard={leaderboard.leaderboard} beatSaverMap={leaderboard.beatsaver} />
|
2024-10-04 18:25:37 +01:00
|
|
|
</main>
|
|
|
|
);
|
|
|
|
}
|