fix score leaderboard staying open when switching sort/page
This commit is contained in:
parent
42d133bbbb
commit
c64f046df3
@ -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()) {
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
@ -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
|
||||
*
|
||||
|
@ -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",
|
||||
|
@ -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 {
|
||||
/**
|
||||
|
@ -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 = {
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user