fix score leaderboard staying open when switching sort/page
Some checks failed
Deploy Backend / deploy (push) Failing after 1m27s
Deploy Website / deploy (push) Failing after 1m32s

This commit is contained in:
Lee 2024-10-17 03:08:27 +01:00
parent 42d133bbbb
commit c64f046df3
9 changed files with 34 additions and 10 deletions

@ -7,6 +7,11 @@ import { formatPp } from "@ssr/common/utils/number-utils";
import { isProduction } from "@ssr/common/utils/utils"; import { isProduction } from "@ssr/common/utils/utils";
export class ScoreService { export class ScoreService {
/**
* Notifies the number one score in Discord.
*
* @param playerScore the score to notify
*/
public static async notifyNumberOne(playerScore: ScoreSaberPlayerScoreToken) { public static async notifyNumberOne(playerScore: ScoreSaberPlayerScoreToken) {
// Only notify in production // Only notify in production
if (!isProduction()) { if (!isProduction()) {

@ -4,9 +4,9 @@ import { PlayerHistory } from "../player-history";
import ScoreSaberPlayerToken from "../../token/scoresaber/score-saber-player-token"; import ScoreSaberPlayerToken from "../../token/scoresaber/score-saber-player-token";
import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../../utils/time-utils"; import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../../utils/time-utils";
import { getPageFromRank } from "../../../utils/utils"; import { getPageFromRank } from "../../../utils/utils";
import { getCookieValue } from "website/src/common/cookie-utils";
import { db } from "website/src/common/database/database"; import { db } from "website/src/common/database/database";
import { isServer } from "@tanstack/react-query"; import { isServer } from "@tanstack/react-query";
import { getCookieValue } from "@ssr/utils/cookie-utils";
/** /**
* A ScoreSaber player. * A ScoreSaber player.
@ -107,7 +107,7 @@ export async function getScoreSaberPlayerFromToken(
.get<{ .get<{
statistics: { [key: string]: PlayerHistory }; statistics: { [key: string]: PlayerHistory };
}>( }>(
`${apiUrl}/player/history/${token.id}${playerIdCookie && playerIdCookie == token.id ? "?createIfMissing=true" : ""}` `${apiUrl}/player/history/${token.id}${playerIdCookie && playerIdCookie === token.id ? "?createIfMissing=true" : ""}`
) )
.json(); .json();
if (history) { if (history) {

@ -1,4 +1,4 @@
import { isServer } from "@tanstack/react-query"; import { isServer } from "./utils";
export type CookieName = "playerId" | "lastScoreSort"; export type CookieName = "playerId" | "lastScoreSort";
@ -11,7 +11,7 @@ export type CookieName = "playerId" | "lastScoreSort";
*/ */
export async function getCookieValue(name: CookieName, defaultValue?: string): Promise<string | undefined> { export async function getCookieValue(name: CookieName, defaultValue?: string): Promise<string | undefined> {
let value: string | undefined; let value: string | undefined;
if (isServer) { if (isServer()) {
const { cookies } = await import("next/headers"); const { cookies } = await import("next/headers");
const cookieStore = await cookies(); const cookieStore = await cookies();
@ -32,7 +32,7 @@ export async function getCookieValue(name: CookieName, defaultValue?: string): P
* @param value the value of the cookie * @param value the value of the cookie
*/ */
export async function setCookieValue(name: CookieName, value: string) { export async function setCookieValue(name: CookieName, value: string) {
if (isServer) { if (isServer()) {
const { cookies } = await import("next/headers"); const { cookies } = await import("next/headers");
const cookieStore = await cookies(); const cookieStore = await cookies();

@ -7,6 +7,13 @@ export function isProduction() {
return process.env.NODE_ENV === "production"; return process.env.NODE_ENV === "production";
} }
/**
* Checks if we're running on the server
*/
export function isServer() {
return typeof window === "undefined";
}
/** /**
* Delays a promise * Delays a promise
* *

@ -9,7 +9,7 @@ import ScoreSaberPlayerScoresPageToken from "@ssr/common/types/token/scoresaber/
import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/types/player/impl/scoresaber-player"; import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/types/player/impl/scoresaber-player";
import { config } from "../../../../../config"; import { config } from "../../../../../config";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { getCookieValue } from "@/common/cookie-utils"; import { getCookieValue } from "@ssr/common/utils/cookie-utils";
const UNKNOWN_PLAYER = { const UNKNOWN_PLAYER = {
title: "ScoreSaber Reloaded - Unknown Player", title: "ScoreSaber Reloaded - Unknown Player",

@ -1,13 +1,12 @@
import Dexie, { EntityTable } from "dexie"; import Dexie, { EntityTable } from "dexie";
import BeatSaverMap from "./types/beatsaver-map"; import BeatSaverMap from "./types/beatsaver-map";
import Settings from "./types/settings"; import Settings from "./types/settings";
import { setCookieValue } from "@/common/cookie-utils";
import { Friend } from "@/common/database/types/friends"; import { Friend } from "@/common/database/types/friends";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token"; import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@ssr/common/utils/cookie-utils";
const SETTINGS_ID = "SSR"; // DO NOT CHANGE const SETTINGS_ID = "SSR"; // DO NOT CHANGE
const FRIENDS_ID = "FRIENDS"; // DO NOT CHANGE
export default class Database extends Dexie { export default class Database extends Dexie {
/** /**

@ -7,7 +7,7 @@ import { useToast } from "@/hooks/use-toast";
import Tooltip from "../tooltip"; import Tooltip from "../tooltip";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { setCookieValue } from "@/common/cookie-utils"; import { setCookieValue } from "@ssr/common/utils/cookie-utils";
type Props = { type Props = {
/** /**

@ -16,7 +16,7 @@ import ScoreSaberPlayer from "@ssr/common/types/player/impl/scoresaber-player";
import ScoreSaberPlayerScoresPageToken from "@ssr/common/types/token/scoresaber/score-saber-player-scores-page-token"; import ScoreSaberPlayerScoresPageToken from "@ssr/common/types/token/scoresaber/score-saber-player-scores-page-token";
import { ScoreSort } from "@ssr/common/types/score/score-sort"; import { ScoreSort } from "@ssr/common/types/score/score-sort";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@/common/cookie-utils"; import { setCookieValue } from "@ssr/common/utils/cookie-utils";
type Props = { type Props = {
initialScoreData?: ScoreSaberPlayerScoresPageToken; initialScoreData?: ScoreSaberPlayerScoresPageToken;

@ -48,16 +48,29 @@ export default function Score({ player, playerScore, settings }: Props) {
setBeatSaverMap(beatSaverMapData); setBeatSaverMap(beatSaverMapData);
}, [leaderboard.songHash, settings?.noScoreButtons]); }, [leaderboard.songHash, settings?.noScoreButtons]);
/**
* Set the base score
*/
useEffect(() => { useEffect(() => {
if (playerScore?.score?.baseScore) { if (playerScore?.score?.baseScore) {
setBaseScore(playerScore.score.baseScore); setBaseScore(playerScore.score.baseScore);
} }
}, [playerScore]); }, [playerScore]);
/**
* Fetch the beatSaver data on page load
*/
useEffect(() => { useEffect(() => {
fetchBeatSaverData(); fetchBeatSaverData();
}, [fetchBeatSaverData]); }, [fetchBeatSaverData]);
/**
* Close the leaderboard when the score changes
*/
useEffect(() => {
setIsLeaderboardExpanded(false);
}, [score]);
const accuracy = (baseScore / leaderboard.maxScore) * 100; const accuracy = (baseScore / leaderboard.maxScore) * 100;
const pp = baseScore === score.baseScore ? score.pp : scoresaberService.getPp(leaderboard.stars, accuracy); const pp = baseScore === score.baseScore ? score.pp : scoresaberService.getPp(leaderboard.stars, accuracy);