From 81546ff3bee0c965b462ca981ad729429885802d Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 11 Jul 2022 13:21:56 +0200 Subject: [PATCH] chore(gui-v2): remove typecasting in state plugin Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com> --- packages/nc-gui-v2/composables/useGlobalState.ts | 7 +++++-- packages/nc-gui-v2/plugins/state.ts | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/nc-gui-v2/composables/useGlobalState.ts b/packages/nc-gui-v2/composables/useGlobalState.ts index ce0434d2af..d8bbfaae10 100644 --- a/packages/nc-gui-v2/composables/useGlobalState.ts +++ b/packages/nc-gui-v2/composables/useGlobalState.ts @@ -30,7 +30,10 @@ export const useGlobalState = (): GlobalState => { /** reactive timestamp to check token expiry against */ const timestamp = $(useTimestamp({ immediate: true, interval: 100 })) - const { $api, vueApp } = useNuxtApp() + const { + $api, + vueApp: { i18n }, + } = useNuxtApp() /** * Set initial language based on browser settings. @@ -42,7 +45,7 @@ export const useGlobalState = (): GlobalState => { const [lang, code] = language.split(/[_-]/) /** find all locales that match the language */ - let availableLocales = vueApp.i18n.availableLocales.filter((locale) => locale.startsWith(lang)) + let availableLocales = i18n.availableLocales.filter((locale) => locale.startsWith(lang)) /** If we can match more than one locale, we check if the code of the language matches as well */ if (availableLocales.length > 1) { diff --git a/packages/nc-gui-v2/plugins/state.ts b/packages/nc-gui-v2/plugins/state.ts index 272c797f85..a97d2e8cb3 100644 --- a/packages/nc-gui-v2/plugins/state.ts +++ b/packages/nc-gui-v2/plugins/state.ts @@ -1,11 +1,23 @@ import { defineNuxtPlugin } from '#app' import { useGlobalState } from '~/composables/useGlobalState' +/** + * Injects global state into nuxt app. + * + * @example + * ```js + * import { useNuxtApp } from '#app' + * + * const { $state } = useNuxtApp() + * + * console.log($state.lang.value) // 'en' + * ``` + */ export default defineNuxtPlugin((nuxtApp) => { const storage = useGlobalState() - // set initial app language to the first preferred language (found in state) - ;(nuxtApp.vueApp as any).i18n.locale.value = storage.lang.value + /** set i18n locale to stored language */ + nuxtApp.vueApp.i18n.locale = storage.lang.value nuxtApp.provide('state', storage) })