diff --git a/client/src/util/storage.ts b/client/src/util/storage.ts index a119291..d80a3f3 100644 --- a/client/src/util/storage.ts +++ b/client/src/util/storage.ts @@ -1,5 +1,5 @@ import type { JsonWebToken, SavedOAuthToken, UserTheme } from "@fumix/fu-blog-common"; -import { base64UrlToBuffer, isOAuthType } from "@fumix/fu-blog-common"; +import { base64UrlToBuffer, isOAuthType, isUserTheme } from "@fumix/fu-blog-common"; type OAuthState = { key: string; redirect_uri?: string }; const idTokenKey = "id_token"; @@ -67,7 +67,17 @@ export function saveCssPreference(css: UserTheme) { } export function loadCssPreference(): UserTheme { - return loadFromStorageAsString(window.localStorage, "cssTheme", "lightTheme") as UserTheme; + return loadFromStorage( + window.localStorage, + "cssTheme", + (saved) => { + if (isUserTheme(saved)) { + return saved; + } + return null; + }, + "light", + ); } // diff --git a/common/src/entity/User.ts b/common/src/entity/User.ts index da713b9..a51d7f5 100644 --- a/common/src/entity/User.ts +++ b/common/src/entity/User.ts @@ -28,4 +28,9 @@ export type UserWithOAuthProviders = User & { oauthProviders: OAuthProviderId[]; }; -export type UserTheme = "light" | "dark"; +const userThemes = ["light", "dark"] as const; +export type UserTheme = (typeof userThemes)[number]; + +export function isUserTheme(s: string): s is UserTheme { + return userThemes.some((it) => it === s); +}