1
0
mirror of https://github.com/elk-zone/elk synced 2024-12-12 13:48:04 +09:00
elk/composables/settings/storage.ts
renovate[bot] 320ddc0e28
chore(deps): update dependency @antfu/eslint-config to v3 (#2984)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
2024-09-30 08:11:56 +00:00

46 lines
1.7 KiB
TypeScript

import type { LocaleObject } from '@nuxtjs/i18n'
import type { Ref } from 'vue'
import type { FontSize, OldFontSize, PreferencesSettings, UserSettings } from './definition'
import { oldFontSizeMap } from '~~/constants/options'
import { STORAGE_KEY_SETTINGS } from '~/constants'
export function useUserSettings() {
const { locales } = useNuxtApp().$i18n
const supportLanguages = (unref(locales) as LocaleObject[]).map(locale => locale.code)
const settingsStorage = useUserLocalStorage<UserSettings>(STORAGE_KEY_SETTINGS, () => getDefaultUserSettings(supportLanguages))
// Backward compatibility, font size was xs, sm, md, lg, xl before
if (settingsStorage.value.fontSize && !settingsStorage.value.fontSize.includes('px'))
settingsStorage.value.fontSize = oldFontSizeMap[settingsStorage.value.fontSize as OldFontSize] as FontSize
return settingsStorage
}
// TODO: refactor & simplify this
export function usePreferences<T extends keyof PreferencesSettings>(name: T): Ref<PreferencesSettings[T]> {
const userSettings = useUserSettings()
return computed({
get() {
return getPreferences(userSettings.value, name)
},
set(value) {
userSettings.value.preferences[name] = value
},
})
}
export function getPreferences<T extends keyof PreferencesSettings>(userSettings: UserSettings, name: T): PreferencesSettings[T] {
const preference = userSettings?.preferences?.[name] ?? DEFAULT__PREFERENCES_SETTINGS[name]
if (name === 'enableAutoplay')
return getPreferences(userSettings, 'enableDataSaving') ? false : preference
return preference
}
export function togglePreferences(key: keyof PreferencesSettings) {
const flag = usePreferences(key)
flag.value = !flag.value
}