mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
refactor: improve type safety in UI settings and theme components
This commit is contained in:
@@ -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"
|
||||
>
|
||||
<InterfaceOption
|
||||
v-model="option.model.value as boolean"
|
||||
v-model="option.model.value"
|
||||
class="ma-1"
|
||||
:title="option.title"
|
||||
:description="option.description"
|
||||
@@ -325,7 +325,7 @@ const toggleDisableAnimations = (value: boolean) => {
|
||||
md="6"
|
||||
>
|
||||
<InterfaceOption
|
||||
v-model="option.model.value as boolean"
|
||||
v-model="option.model.value"
|
||||
class="ma-1"
|
||||
:disabled="option.disabled"
|
||||
:title="option.title"
|
||||
@@ -360,7 +360,7 @@ const toggleDisableAnimations = (value: boolean) => {
|
||||
<v-row class="py-1 align-center" no-gutters>
|
||||
<v-col cols="12" md="6">
|
||||
<InterfaceOption
|
||||
v-model="showVirtualCollectionsRef as boolean"
|
||||
v-model="showVirtualCollectionsRef"
|
||||
class="mx-2"
|
||||
:title="t('settings.show-virtual-collections')"
|
||||
:description="t('settings.show-virtual-collections-desc')"
|
||||
|
||||
@@ -5,8 +5,6 @@ import { useTheme } from "vuetify";
|
||||
import ThemeOption from "@/components/Settings/UserInterface/ThemeOption.vue";
|
||||
import RSection from "@/components/common/RSection.vue";
|
||||
import { useUISettings } from "@/composables/useUISettings";
|
||||
import { autoThemeKey, themes } from "@/styles/themes";
|
||||
import { isKeyof } from "@/types";
|
||||
|
||||
const { t } = useI18n();
|
||||
const theme = useTheme();
|
||||
@@ -28,10 +26,12 @@ const themeOptions = computed(() => [
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T extends (...args: any[]) => any>(
|
||||
func: T,
|
||||
wait: number,
|
||||
): (...args: Parameters<T>) => void {
|
||||
let timeout: ReturnType<typeof setTimeout> | null = null;
|
||||
return (...args: Parameters<T>) => {
|
||||
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> = T extends string
|
||||
? string
|
||||
: T extends number
|
||||
? number
|
||||
: T extends boolean
|
||||
? boolean
|
||||
: T extends null
|
||||
? string | null
|
||||
: T;
|
||||
|
||||
type UISettingDefaultType<K extends UISettingsKey> = WidenLiteral<
|
||||
(typeof UI_SETTINGS_KEYS)[K]["default"]
|
||||
>;
|
||||
|
||||
// Strongly-typed refs for each UI setting
|
||||
type UISettingsRefs = {
|
||||
[K in UISettingsKey]: RemovableRef<UISettingDefaultType<K>>;
|
||||
};
|
||||
|
||||
// Singleton state to prevent multiple instances
|
||||
let uiSettingsInstance: ReturnType<typeof createUISettings> | 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<UISettingsKey, ReturnType<typeof useLocalStorage>>;
|
||||
) 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(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user