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

View File

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

View File

@ -4,9 +4,9 @@ import { PlayerHistory } from "../player-history";
import ScoreSaberPlayerToken from "../../token/scoresaber/score-saber-player-token";
import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../../utils/time-utils";
import { getPageFromRank } from "../../../utils/utils";
import { getCookieValue } from "website/src/common/cookie-utils";
import { db } from "website/src/common/database/database";
import { isServer } from "@tanstack/react-query";
import { getCookieValue } from "@ssr/utils/cookie-utils";
/**
* A ScoreSaber player.
@ -107,7 +107,7 @@ export async function getScoreSaberPlayerFromToken(
.get<{
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();
if (history) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 { ScoreSort } from "@ssr/common/types/score/score-sort";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@/common/cookie-utils";
import { setCookieValue } from "@ssr/common/utils/cookie-utils";
type Props = {
initialScoreData?: ScoreSaberPlayerScoresPageToken;

View File

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