fix(frontend): harden font/theme preferences for production edge cases
Edge-case review identified several issues that would degrade UX or silently break under unusual conditions: - Anon settings chosen on the login screen were lost on first login because the storage namespace switched from "anon" to the user id; resolveStorageValue now falls through user -> anon -> legacy, so pre-login choices carry over and v1 unscoped keys are still picked up. - localStorage.setItem can throw under quota / private mode; setters now swallow the error so the UI still updates for the session. - applyFont reduced to String(undefined) when currentSize held an invalid value; runtime fallbacks to the default scale guard against manipulated storage. - Added var(--app-font-scale, 1) defaults to all 32 calc() expressions in theme.css and the body font-size, so a missing variable cannot void the calc() value. - Validation guards in setSansFont / setMonoFont / setFontSize / setTheme reject invalid keys instead of writing them to storage. - GeneralSettings.vue now watches the composable refs so the form stays in sync if preferences change externally (e.g. user switch while on the page). - Dropped the unverified t-radio-group variant prop in favour of the default style. https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
This commit is contained in:
@@ -83,22 +83,55 @@ function readUserId(): string {
|
||||
}
|
||||
}
|
||||
|
||||
function safeGetItem(key: string): string | null {
|
||||
try {
|
||||
return localStorage.getItem(key)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function safeSetItem(key: string, value: string): void {
|
||||
try {
|
||||
localStorage.setItem(key, value)
|
||||
} catch {
|
||||
// Quota / disabled storage / private mode — preference applies to this session only.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a stored preference, falling back through:
|
||||
* 1. The current user's namespace
|
||||
* 2. The "anon" namespace (settings chosen on the login screen)
|
||||
* 3. A legacy un-namespaced key (from earlier versions of this branch)
|
||||
*/
|
||||
function resolveStorageValue(suffix: string): string | null {
|
||||
const userId = readUserId()
|
||||
const userValue = safeGetItem(`WeKnora_${userId}_${suffix}`)
|
||||
if (userValue !== null) return userValue
|
||||
if (userId !== 'anon') {
|
||||
const anonValue = safeGetItem(`WeKnora_anon_${suffix}`)
|
||||
if (anonValue !== null) return anonValue
|
||||
}
|
||||
return safeGetItem(`WeKnora_${suffix}`)
|
||||
}
|
||||
|
||||
function nsKey(suffix: string): string {
|
||||
return `WeKnora_${readUserId()}_${suffix}`
|
||||
}
|
||||
|
||||
function loadSans(): FontKey {
|
||||
const v = localStorage.getItem(nsKey(SANS_KEY))
|
||||
const v = resolveStorageValue(SANS_KEY)
|
||||
return isFontKey(v) ? v : DEFAULT_SANS
|
||||
}
|
||||
|
||||
function loadMono(): MonoFontKey {
|
||||
const v = localStorage.getItem(nsKey(MONO_KEY))
|
||||
const v = resolveStorageValue(MONO_KEY)
|
||||
return isMonoFontKey(v) ? v : DEFAULT_MONO
|
||||
}
|
||||
|
||||
function loadSize(): FontSizeKey {
|
||||
const v = localStorage.getItem(nsKey(SIZE_KEY))
|
||||
const v = resolveStorageValue(SIZE_KEY)
|
||||
return isFontSizeKey(v) ? v : DEFAULT_SIZE
|
||||
}
|
||||
|
||||
@@ -108,27 +141,34 @@ const currentSize = ref<FontSizeKey>(loadSize())
|
||||
|
||||
function applyFont() {
|
||||
const root = document.documentElement
|
||||
root.style.setProperty('--app-font-family', SANS_STACKS[currentSans.value])
|
||||
root.style.setProperty('--app-font-family-mono', MONO_STACKS[currentMono.value])
|
||||
root.style.setProperty('--app-font-scale', String(FONT_SCALES[currentSize.value]))
|
||||
if (!root) return
|
||||
const sansStack = SANS_STACKS[currentSans.value] ?? SANS_STACKS[DEFAULT_SANS]
|
||||
const monoStack = MONO_STACKS[currentMono.value] ?? MONO_STACKS[DEFAULT_MONO]
|
||||
const scale = FONT_SCALES[currentSize.value] ?? FONT_SCALES[DEFAULT_SIZE]
|
||||
root.style.setProperty('--app-font-family', sansStack)
|
||||
root.style.setProperty('--app-font-family-mono', monoStack)
|
||||
root.style.setProperty('--app-font-scale', String(scale))
|
||||
}
|
||||
|
||||
export function useFont() {
|
||||
function setSansFont(key: FontKey) {
|
||||
if (!isFontKey(key)) return
|
||||
currentSans.value = key
|
||||
localStorage.setItem(nsKey(SANS_KEY), key)
|
||||
safeSetItem(nsKey(SANS_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
function setMonoFont(key: MonoFontKey) {
|
||||
if (!isMonoFontKey(key)) return
|
||||
currentMono.value = key
|
||||
localStorage.setItem(nsKey(MONO_KEY), key)
|
||||
safeSetItem(nsKey(MONO_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
function setFontSize(key: FontSizeKey) {
|
||||
if (!isFontSizeKey(key)) return
|
||||
currentSize.value = key
|
||||
localStorage.setItem(nsKey(SIZE_KEY), key)
|
||||
safeSetItem(nsKey(SIZE_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { ref } from 'vue'
|
||||
|
||||
export type ThemeMode = 'light' | 'dark' | 'system'
|
||||
|
||||
const STORAGE_SUFFIX = 'theme'
|
||||
const THEME_KEY = 'theme'
|
||||
|
||||
function readUserId(): string {
|
||||
try {
|
||||
@@ -15,12 +15,45 @@ function readUserId(): string {
|
||||
}
|
||||
}
|
||||
|
||||
function safeGetItem(key: string): string | null {
|
||||
try {
|
||||
return localStorage.getItem(key)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function safeSetItem(key: string, value: string): void {
|
||||
try {
|
||||
localStorage.setItem(key, value)
|
||||
} catch {
|
||||
// Quota / disabled storage / private mode — preference applies to this session only.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a stored preference, falling back through:
|
||||
* 1. The current user's namespace
|
||||
* 2. The "anon" namespace (settings chosen on the login screen)
|
||||
* 3. A legacy un-namespaced key (from earlier versions of this branch)
|
||||
*/
|
||||
function resolveStorageValue(suffix: string): string | null {
|
||||
const userId = readUserId()
|
||||
const userValue = safeGetItem(`WeKnora_${userId}_${suffix}`)
|
||||
if (userValue !== null) return userValue
|
||||
if (userId !== 'anon') {
|
||||
const anonValue = safeGetItem(`WeKnora_anon_${suffix}`)
|
||||
if (anonValue !== null) return anonValue
|
||||
}
|
||||
return safeGetItem(`WeKnora_${suffix}`)
|
||||
}
|
||||
|
||||
function nsKey(): string {
|
||||
return `WeKnora_${readUserId()}_${STORAGE_SUFFIX}`
|
||||
return `WeKnora_${readUserId()}_${THEME_KEY}`
|
||||
}
|
||||
|
||||
function loadTheme(): ThemeMode {
|
||||
const v = localStorage.getItem(nsKey())
|
||||
const v = resolveStorageValue(THEME_KEY)
|
||||
if (v === 'light' || v === 'dark' || v === 'system') return v
|
||||
return 'light'
|
||||
}
|
||||
@@ -76,8 +109,9 @@ function applyTheme(mode: ThemeMode) {
|
||||
|
||||
export function useTheme() {
|
||||
function setTheme(mode: ThemeMode) {
|
||||
if (mode !== 'light' && mode !== 'dark' && mode !== 'system') return
|
||||
currentTheme.value = mode
|
||||
localStorage.setItem(nsKey(), mode)
|
||||
safeSetItem(nsKey(), mode)
|
||||
applyTheme(mode)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user