diff --git a/frontend/src/components/Settings/UserInterface/Interface.vue b/frontend/src/components/Settings/UserInterface/Interface.vue index 4aeb951d4..4b0bf6b1d 100644 --- a/frontend/src/components/Settings/UserInterface/Interface.vue +++ b/frontend/src/components/Settings/UserInterface/Interface.vue @@ -186,8 +186,8 @@ const boxartStyleOptions = computed(() => [ { title: t("settings.boxart-miximage"), value: "miximage_path" }, ]); -const setPlatformDrawerGroupBy = (value: unknown) => { - platformsGroupByRef.value = value as string; +const setPlatformDrawerGroupBy = (value: string | null) => { + platformsGroupByRef.value = value; }; const toggleShowContinuePlaying = (value: boolean) => { showContinuePlayingRef.value = value; @@ -201,11 +201,11 @@ const toggleShowCollections = (value: boolean) => { const toggleShowVirtualCollections = (value: boolean) => { showVirtualCollectionsRef.value = value; }; -const setVirtualCollectionType = async (value: unknown) => { - virtualCollectionTypeRef.value = value as string; - collectionsStore.fetchVirtualCollections(value as string); +const setVirtualCollectionType = async (value: string) => { + virtualCollectionTypeRef.value = value; + collectionsStore.fetchVirtualCollections(value); }; -const setBoxartStyle = (value: unknown) => { +const setBoxartStyle = (value: string) => { boxartStyleRef.value = value as BoxartStyleOption; }; const toggleShowStats = (value: boolean) => { @@ -265,7 +265,7 @@ const toggleDisableAnimations = (value: boolean) => { md="6" > { md="6" > { [ function applyTheme() { const mediaMatch = window.matchMedia("(prefers-color-scheme: dark)"); - if (selectedTheme.value === autoThemeKey) { + const themeValue = selectedTheme.value; + + if (themeValue === "auto") { theme.change(mediaMatch.matches ? "dark" : "light"); - } else if (isKeyof(selectedTheme.value as PropertyKey, themes)) { - theme.change(themes[selectedTheme.value as keyof typeof themes]); + } else if (themeValue === "dark" || themeValue === "light") { + theme.change(themeValue); } } diff --git a/frontend/src/composables/useUISettings.ts b/frontend/src/composables/useUISettings.ts index 40ee3a845..280782518 100644 --- a/frontend/src/composables/useUISettings.ts +++ b/frontend/src/composables/useUISettings.ts @@ -1,24 +1,18 @@ import { useLocalStorage } from "@vueuse/core"; +import type { RemovableRef } from "@vueuse/core"; import { storeToRefs } from "pinia"; -import { computed, watch, ref } from "vue"; +import { watch, ref } from "vue"; import type { UserSchema } from "@/__generated__"; import userApi from "@/services/api/user"; import storeAuth from "@/stores/auth"; -// Debounce utility -function debounce any>( - func: T, - wait: number, -): (...args: Parameters) => void { - let timeout: ReturnType | null = null; - return (...args: Parameters) => { - if (timeout) clearTimeout(timeout); - timeout = setTimeout(() => func(...args), wait); - }; -} - -// UI Settings keys and their default values export const UI_SETTINGS_KEYS = { + // Language + locale: { key: "settings.locale", default: "" }, + + // Theme + theme: { key: "settings.theme", default: "auto" }, + // Home section showStats: { key: "settings.showStats", default: true }, showRecentRoms: { key: "settings.showRecentRoms", default: true }, @@ -26,16 +20,6 @@ export const UI_SETTINGS_KEYS = { showPlatforms: { key: "settings.showPlatforms", default: true }, showCollections: { key: "settings.showCollections", default: true }, - // Virtual collections - showVirtualCollections: { - key: "settings.showVirtualCollections", - default: true, - }, - virtualCollectionType: { - key: "settings.virtualCollectionType", - default: "collection", - }, - // Platforms drawer platformsGroupBy: { key: "settings.platformsGroupBy", default: null }, @@ -55,15 +39,40 @@ export const UI_SETTINGS_KEYS = { }, boxartStyle: { key: "settings.boxartStyle", default: "cover_path" }, - // Theme - theme: { key: "settings.theme", default: "auto" }, - - // Language - locale: { key: "settings.locale", default: "" }, + // Virtual collections + showVirtualCollections: { + key: "settings.showVirtualCollections", + default: true, + }, + virtualCollectionType: { + key: "settings.virtualCollectionType", + default: "collection", + }, } as const; export type UISettingsKey = keyof typeof UI_SETTINGS_KEYS; +// Helper type to extract the default value type for each setting +// Widens literal types to their base types (e.g., true -> boolean, "auto" -> string) +type WidenLiteral = T extends string + ? string + : T extends number + ? number + : T extends boolean + ? boolean + : T extends null + ? string | null + : T; + +type UISettingDefaultType = WidenLiteral< + (typeof UI_SETTINGS_KEYS)[K]["default"] +>; + +// Strongly-typed refs for each UI setting +type UISettingsRefs = { + [K in UISettingsKey]: RemovableRef>; +}; + // Singleton state to prevent multiple instances let uiSettingsInstance: ReturnType | null = null; let isSyncing = ref(false); // Global flag to prevent sync loops @@ -78,10 +87,10 @@ function createUISettings() { name, useLocalStorage(config.key, config.default), ]), - ) as Record>; + ) as UISettingsRefs; // Initialize settings from backend user data - function initializeFromBackend() { + function initialize() { const userWithSettings = user.value as UserSchema | null; if (!userWithSettings?.ui_settings) return; @@ -108,8 +117,7 @@ function createUISettings() { ); } - // Debounced function to save settings to backend - const saveToBackend = debounce(async () => { + const saveUISettings = async () => { if (!user.value || isSyncing.value) return; const currentSettings = collectSettings(); @@ -132,13 +140,13 @@ function createUISettings() { } catch (error) { console.error("Failed to save UI settings to backend:", error); } - }, 500); + }; // Watch all localStorage refs for changes (only set up once) Object.values(localStorageRefs).forEach((ref) => { watch(ref, () => { if (!isSyncing.value) { - saveToBackend(); + saveUISettings(); } }); }); @@ -149,7 +157,7 @@ function createUISettings() { (newUser) => { const userWithSettings = newUser as UserSchema | null; if (userWithSettings?.ui_settings) { - initializeFromBackend(); + initialize(); } }, { immediate: true }, @@ -157,8 +165,8 @@ function createUISettings() { return { ...localStorageRefs, - initializeFromBackend, - saveToBackend: () => saveToBackend(), + initialize: initialize, + saveUISettings: () => saveUISettings(), }; }