add default score sort (last selected)
All checks were successful
Deploy Website / deploy (push) Successful in 5m1s
All checks were successful
Deploy Website / deploy (push) Successful in 5m1s
This commit is contained in:
47
projects/website/src/common/cookie-utils.ts
Normal file
47
projects/website/src/common/cookie-utils.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { isServer } from "@tanstack/react-query";
|
||||
|
||||
export type CookieName = "playerId" | "lastScoreSort";
|
||||
|
||||
/**
|
||||
* Gets the value of a cookie
|
||||
*
|
||||
* @param name the name of the cookie
|
||||
* @param defaultValue the fallback value
|
||||
* @returns the value of the cookie, or the fallback value (undefined if no fallback value is provided)
|
||||
*/
|
||||
export async function getCookieValue(name: CookieName, defaultValue?: string): Promise<string | undefined> {
|
||||
let value: string | undefined;
|
||||
if (isServer) {
|
||||
const { cookies } = await import("next/headers");
|
||||
|
||||
const cookieStore = await cookies();
|
||||
const cookieValue = cookieStore.get(name)?.value;
|
||||
value = cookieValue ? cookieValue : defaultValue ? defaultValue : undefined;
|
||||
} else {
|
||||
const { get } = (await import("js-cookie")).default;
|
||||
value = get(name) || defaultValue ? defaultValue : undefined;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of a cookie
|
||||
*
|
||||
* @param name the name of the cookie
|
||||
* @param value the value of the cookie
|
||||
*/
|
||||
export async function setCookieValue(name: CookieName, value: string) {
|
||||
if (isServer) {
|
||||
const { cookies } = await import("next/headers");
|
||||
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set(name, value, {
|
||||
path: "/",
|
||||
});
|
||||
}
|
||||
const { set } = (await import("js-cookie")).default;
|
||||
return set(name, value, {
|
||||
path: "/",
|
||||
});
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import Dexie, { EntityTable } from "dexie";
|
||||
import { setPlayerIdCookie } from "../website-utils";
|
||||
import BeatSaverMap from "./types/beatsaver-map";
|
||||
import Settings from "./types/settings";
|
||||
import { setCookieValue } from "@/common/cookie-utils";
|
||||
|
||||
const SETTINGS_ID = "SSR"; // DO NOT CHANGE
|
||||
|
||||
@ -38,7 +38,7 @@ export default class Database extends Dexie {
|
||||
if (settings == undefined || settings.playerId == undefined) {
|
||||
return;
|
||||
}
|
||||
setPlayerIdCookie(settings.playerId);
|
||||
await setCookieValue("playerId", settings.playerId);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,23 +1,3 @@
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
/**
|
||||
* Sets the player id cookie
|
||||
*
|
||||
* @param playerId the player id to set
|
||||
*/
|
||||
export function setPlayerIdCookie(playerId: string) {
|
||||
Cookies.set("playerId", playerId, { path: "/" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player id cookie
|
||||
*
|
||||
* @returns the player id cookie
|
||||
*/
|
||||
export function getPlayerIdCookie() {
|
||||
return Cookies.get("playerId");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets if we're in production
|
||||
*/
|
||||
|
Reference in New Issue
Block a user