From 8583455e34d06b6ec44b62f5ea96c9c656809e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=A4fer?= Date: Tue, 26 Nov 2024 17:46:07 +0100 Subject: [PATCH] Fix dark font on dark background by default --- client/src/util/storage.ts | 14 ++++++++++++-- common/src/entity/User.ts | 7 ++++++- 2 files changed, 18 insertions(+), 3 deletions(-) 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); +}