From 7bbe98d3823ed6fe6eb5c702580bf86ec72c9152 Mon Sep 17 00:00:00 2001 From: "Jack W." Date: Wed, 7 May 2025 01:31:14 -0400 Subject: [PATCH 1/2] refactor: Make more things use `Game` instead of string --- src/lib/api/card.ts | 8 ++++---- src/lib/api/settings.ts | 7 ++++--- src/lib/app/assets.ts | 4 +++- src/lib/app/formatting.ts | 2 +- src/lib/components/Navigator.svelte | 4 ++-- src/lib/components/generic/ProfileCard.svelte | 7 +++---- src/lib/components/profile/ProfileThumbnailEmbed.svelte | 2 +- src/routes/(app)/(nonuser)/settings/+page.svelte | 3 ++- src/routes/+layout.server.ts | 3 +++ 9 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/lib/api/card.ts b/src/lib/api/card.ts index a6aa0212..c85cd7ff 100644 --- a/src/lib/api/card.ts +++ b/src/lib/api/card.ts @@ -1,7 +1,7 @@ import { z } from "zod" import type { Parser } from "." import { Json, String } from "./utility/responses" -import { games } from "$lib/app/formatting" +import { games, type Game } from "$lib/app/formatting" export interface Card { luid: string @@ -16,9 +16,9 @@ export interface GameCard { name: string rating: number } -export type CardUserGames = Record +export type CardUserGames = Record -export function getAvailableGame(cardUserGames: CardUserGames, active?: string, target?: string) { +export function getAvailableGame(cardUserGames: CardUserGames, active?: Game, target?: Game) { if (target && cardUserGames[target]) return target if (active && cardUserGames[active]) return active return Object.keys(cardUserGames).find( @@ -41,7 +41,7 @@ const Routes = { username: z.string() }) }, - response: Json<{ game: string }>() + response: Json<{ game: Game }>() }, link: { authenticated: true, diff --git a/src/lib/api/settings.ts b/src/lib/api/settings.ts index a0e12a01..a8ab3d78 100644 --- a/src/lib/api/settings.ts +++ b/src/lib/api/settings.ts @@ -1,11 +1,12 @@ import { z } from "zod" import type { Parser } from "." import { Json } from "./utility/responses" +import type { Game } from "$lib/app/formatting" export interface Setting { key: string type: "String" | "Boolean" - game: string + game: Game value?: boolean | string } @@ -19,14 +20,14 @@ export const userVisibleSettings: Array = [ "waccaAlwaysVip", "waccaInfiniteWp" ] -export const userSettingMatches: Record> = { +export const userSettingMatches: Record = { chu3: ["chu3", "chu3-matching"], mai2: ["general", "mai2"], wacca: ["general", "wacca"], ongeki: ["ongeki"] } -export function settingMatches(selectedGame: string, setting: Setting) { +export function settingMatches(selectedGame: Game, setting: Setting) { return ( userSettingMatches[selectedGame].includes(setting.game) && userVisibleSettings.includes(setting.key) diff --git a/src/lib/app/assets.ts b/src/lib/app/assets.ts index c268759c..6bcb8374 100644 --- a/src/lib/app/assets.ts +++ b/src/lib/app/assets.ts @@ -1,7 +1,9 @@ +import type { Game } from "./formatting" + export function getProfilePictureUrl(profilePicture: string) { return `/net/asset/net/portrait/${profilePicture}` } -export function getMusicJacket(game: string, musicId: number) { +export function getMusicJacket(game: Game, musicId: number) { // Removing any characters at the beginning if 4+ characters long seems to fix the thumbnail resolving for some reason on mai return `/net/data/${game}/song/${musicId.toString().substring(musicId.toString().length - 4)}/jacket`; } \ No newline at end of file diff --git a/src/lib/app/formatting.ts b/src/lib/app/formatting.ts index ef9fb87a..bb46c841 100644 --- a/src/lib/app/formatting.ts +++ b/src/lib/app/formatting.ts @@ -24,7 +24,7 @@ export function fromSegacode(name: string) { export function formatWithCommas(number: number) { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") } -export function formatRatingForGame(game: string, rating: number) { +export function formatRatingForGame(game: Game, rating: number) { switch (game) { case "chu3": return (rating / 100).toFixed(2) diff --git a/src/lib/components/Navigator.svelte b/src/lib/components/Navigator.svelte index d37b7440..a5ab7925 100644 --- a/src/lib/components/Navigator.svelte +++ b/src/lib/components/Navigator.svelte @@ -20,13 +20,13 @@ import type { CardUserGames } from "$lib/api/card" import type { User } from "$lib/api/user" import SignedOutCard from "./generic/SignedOutCard.svelte" - import { games } from "$lib/app/formatting" + import { games, type Game } from "$lib/app/formatting" import NavButton from "./NavButton.svelte" interface Props { user?: User cardUserGames?: CardUserGames - activeGame?: string + activeGame?: Game } let { cardUserGames, user, activeGame = $bindable() }: Props = $props() diff --git a/src/lib/components/generic/ProfileCard.svelte b/src/lib/components/generic/ProfileCard.svelte index 7e38a2bc..cb7d1263 100644 --- a/src/lib/components/generic/ProfileCard.svelte +++ b/src/lib/components/generic/ProfileCard.svelte @@ -9,17 +9,16 @@ Do NOT pass in values that are not of the CURRENT USER or it will NOT work prope import { LL } from "$lib/i18n/i18n-svelte"; import { getAvailableGames, type CardUserGames } from "$lib/api/card" - import { games, formatRatingForGame, fromSegacode } from "$lib/app/formatting"; + import { games, formatRatingForGame, fromSegacode, type Game } from "$lib/app/formatting"; import { enhance } from "$app/forms" import { page } from "$app/state" import { getContext } from "svelte" import type { User } from "$lib/api/user" import Loader from "$lib/app/loader.svelte" import { getProfilePictureUrl } from "$lib/app/assets" - export interface Props { cardUserGames: CardUserGames, - activeGame?: string, + activeGame?: Game, user: User } let { cardUserGames, activeGame = $bindable(), user }: Props = $props(); @@ -78,7 +77,7 @@ Do NOT pass in values that are not of the CURRENT USER or it will NOT work prope {#each available_games as game}