Ver Fonte

feat(store): добавлен стор для работы с настройками

horanchikk há 5 meses atrás
pai
commit
14ed234029
1 ficheiros alterados com 42 adições e 0 exclusões
  1. 42 0
      store/useSettings.ts

+ 42 - 0
store/useSettings.ts

@@ -0,0 +1,42 @@
+import { hasKeys } from '~/helpers/obj/hasKeys'
+import type { TSettings } from '~/types/settings'
+
+export const useSettings = defineStore('useSettings', () => {
+  const { public: { APP_VERSION } } = useRuntimeConfig()
+
+  function getFromLS(): TSettings {
+    const LS = localStorage.getItem('settings')
+
+    if (LS) {
+      const parsed: object = JSON.parse(LS)
+      if (typeof parsed === 'object') {
+        if (hasKeys(parsed, ['dark', 'dev', 'debug']))
+          return parsed as TSettings
+      }
+    }
+
+    const defaultSettings: TSettings = {
+      dark: true,
+      dev: APP_VERSION.includes('dev'),
+      debug: false,
+    }
+    localStorage.setItem('settings', JSON.stringify(defaultSettings))
+
+    return defaultSettings
+  }
+
+  function updateLS(val: object) {
+    localStorage.setItem('settings', JSON.stringify(val))
+  }
+
+  const settings = ref<TSettings>(getFromLS())
+
+  // Sync ref with localStorage
+  watch(settings.value, (val) => {
+    updateLS(val)
+  })
+
+  return {
+    settings,
+  }
+})