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

57 lines
2.1 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";
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
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
*/
initialScores: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
2024-10-04 18:25:37 +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
});
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
leaderboard={leaderboard.leaderboard}
2024-10-04 18:25:37 +01:00
initialScores={initialScores}
leaderboardChanged={newId => setCurrentLeaderboardId(newId)}
2024-10-04 18:25:37 +01:00
showDifficulties
isLeaderboardPage
/>
<LeaderboardInfo leaderboard={leaderboard.leaderboard} beatSaverMap={leaderboard.beatsaver} />
2024-10-04 18:25:37 +01:00
</main>
);
}