From 132d8de689cfce2975d7232cbd67e1e91551c1ab Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 5 Jul 2022 20:51:48 +0200 Subject: [PATCH] feat(gui-v2): add global state --- .../nc-gui-v2/composables/useGlobalState.ts | 11 ++ packages/nc-gui-v2/composables/user.ts | 19 +-- packages/nc-gui-v2/layouts/default.vue | 22 ++- packages/nc-gui-v2/lib/types.ts | 5 + packages/nc-gui-v2/nuxt-shim.d.ts | 7 + packages/nc-gui-v2/nuxt.config.ts | 2 +- packages/nc-gui-v2/package-lock.json | 154 ++++++++++-------- packages/nc-gui-v2/package.json | 1 + packages/nc-gui-v2/plugins/api.ts | 6 +- packages/nc-gui-v2/plugins/state.ts | 7 + packages/nc-gui-v2/plugins/tele.ts | 10 +- packages/nc-gui-v2/tsconfig.json | 3 +- 12 files changed, 151 insertions(+), 96 deletions(-) create mode 100644 packages/nc-gui-v2/composables/useGlobalState.ts create mode 100644 packages/nc-gui-v2/lib/types.ts create mode 100644 packages/nc-gui-v2/plugins/state.ts diff --git a/packages/nc-gui-v2/composables/useGlobalState.ts b/packages/nc-gui-v2/composables/useGlobalState.ts new file mode 100644 index 0000000000..c971b98881 --- /dev/null +++ b/packages/nc-gui-v2/composables/useGlobalState.ts @@ -0,0 +1,11 @@ +import { createGlobalState, usePreferredLanguages, useStorage } from '@vueuse/core' +import type { GlobalState } from '~/lib/types' + +const storageKey = 'nocodb-gui-v2' + +export const useGlobalState = () => { + const preferredLanguages = $(usePreferredLanguages()) + return createGlobalState(() => + useStorage(storageKey, { token: undefined, user: undefined, lang: preferredLanguages[0] || 'en' }), + ) +} diff --git a/packages/nc-gui-v2/composables/user.ts b/packages/nc-gui-v2/composables/user.ts index 974de96655..5be0542793 100644 --- a/packages/nc-gui-v2/composables/user.ts +++ b/packages/nc-gui-v2/composables/user.ts @@ -1,25 +1,16 @@ -import { store } from 'nuxt3-store' import type { Api } from 'nocodb-sdk' import { useNuxtApp } from '#app' -const user = store({ - name: 'user', - type: 'localstorage', - value: { token: null, user: null }, - reactiveType: 'reactive', - version: '1.0.0', -}) - export const useUser = () => { - const { $api } = useNuxtApp() + const { $api, $state } = useNuxtApp() const getUser = async (...args: Parameters['auth']['me']>) => { - user.user = await $api.auth.me(...args) + $state.user = await $api.auth.me(...args) } - const setToken = (token: string) => { - user.token = token + const setToken = (token?: string) => { + $state.token = token } - return { user, setToken, getUser } + return { user: $state.user, setToken, getUser } } diff --git a/packages/nc-gui-v2/layouts/default.vue b/packages/nc-gui-v2/layouts/default.vue index f798b6a44e..016d27ffba 100644 --- a/packages/nc-gui-v2/layouts/default.vue +++ b/packages/nc-gui-v2/layouts/default.vue @@ -1,9 +1,15 @@