diff --git a/packages/nc-gui-v2/composables/useGlobal/index.ts b/packages/nc-gui-v2/composables/useGlobal/index.ts index 6e0e0b7191..16b3e26d93 100644 --- a/packages/nc-gui-v2/composables/useGlobal/index.ts +++ b/packages/nc-gui-v2/composables/useGlobal/index.ts @@ -1,8 +1,8 @@ import { useGlobalState } from './state' import { useGlobalActions } from './actions' +import type { UseGlobalReturn } from './types' +import { useGlobalGetters } from './getters' import { toRefs, useNuxtApp, watch } from '#imports' -import type { GlobalState } from '~/lib' -import { useGlobalGetters } from '~/composables/useGlobal/getters' /** * Global state is injected by {@link import('~/plugins/state') state} plugin into our nuxt app (available as `$state`). @@ -20,15 +20,11 @@ import { useGlobalGetters } from '~/composables/useGlobal/getters' * const user = $state.user.value * ``` */ -export const useGlobal = (): GlobalState => { +export const useGlobal = (): UseGlobalReturn => { const { $state, provide } = useNuxtApp() - if ($state) { - console.warn( - '[useGlobalState] Global state is injected by state plugin. Manual initialization is unnecessary and should be avoided.', - ) - return $state - } + /** If state already exists, return it */ + if ($state) return $state const state = $(useGlobalState()) @@ -38,16 +34,17 @@ export const useGlobal = (): GlobalState => { /** try to refresh token before expiry (5 min before expiry) */ watch( - () => !!(state.payload && state.payload.exp && state.payload.exp - 5 * 60 < state.timestamp / 1000), + () => !!(state.jwtPayload && state.jwtPayload.exp && state.jwtPayload.exp - 5 * 60 < state.timestamp / 1000), async (expiring) => { - if (getters.signedIn.value && state.payload && expiring) { + if (getters.signedIn.value && state.jwtPayload && expiring) { await actions.refreshToken() } }, { immediate: true }, ) + /** provide a fresh state instance into nuxt app */ provide('state', state) - return { ...toRefs(state), ...getters, ...actions } + return { ...toRefs($$(state)), ...getters, ...actions } } diff --git a/packages/nc-gui-v2/composables/useGlobal/types.ts b/packages/nc-gui-v2/composables/useGlobal/types.ts index 9672f7b14a..90c438aae7 100644 --- a/packages/nc-gui-v2/composables/useGlobal/types.ts +++ b/packages/nc-gui-v2/composables/useGlobal/types.ts @@ -10,7 +10,7 @@ export interface StoredState { darkMode: boolean } -export type State = ToRefs & { +export type State = ToRefs> & { token: WritableComputedRef jwtPayload: ComputedRef<(JwtPayload & User) | null> sidebarOpen: Ref @@ -29,4 +29,4 @@ export interface Actions { export type ReadonlyState = Readonly> & Omit -export type GlobalState = Getters & Actions & ReadonlyState +export type UseGlobalReturn = Getters & Actions & ReadonlyState diff --git a/packages/nc-gui-v2/nuxt-shim.d.ts b/packages/nc-gui-v2/nuxt-shim.d.ts index 4733f7f3e0..e771efe62a 100644 --- a/packages/nc-gui-v2/nuxt-shim.d.ts +++ b/packages/nc-gui-v2/nuxt-shim.d.ts @@ -1,6 +1,6 @@ import type { Api as BaseAPI } from 'nocodb-sdk' import type { I18n } from 'vue-i18n' -import type { GlobalState } from './composables/useGlobal/types' +import type { UseGlobalReturn } from './composables/useGlobal/types' import type en from './lang/en.json' @@ -15,7 +15,7 @@ declare module '#app/nuxt' { } /** {@link import('./plugins/tele') Telemetry} Emit telemetry event */ $e: (event: string, data?: any) => void - $state: GlobalState + $state: UseGlobalReturn } }