Browse Source

refactor(gui-v2): rename `GlobalState` to `UseGlobalReturn`

pull/2877/head
braks 2 years ago
parent
commit
8ed92af4c2
  1. 21
      packages/nc-gui-v2/composables/useGlobal/index.ts
  2. 4
      packages/nc-gui-v2/composables/useGlobal/types.ts
  3. 4
      packages/nc-gui-v2/nuxt-shim.d.ts

21
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 }
}

4
packages/nc-gui-v2/composables/useGlobal/types.ts

@ -10,7 +10,7 @@ export interface StoredState {
darkMode: boolean
}
export type State = ToRefs<StoredState> & {
export type State = ToRefs<Omit<StoredState, 'token'>> & {
token: WritableComputedRef<string | null>
jwtPayload: ComputedRef<(JwtPayload & User) | null>
sidebarOpen: Ref<boolean>
@ -29,4 +29,4 @@ export interface Actions {
export type ReadonlyState = Readonly<Pick<State, 'token' | 'user'>> & Omit<State, 'token' | 'user'>
export type GlobalState = Getters & Actions & ReadonlyState
export type UseGlobalReturn = Getters & Actions & ReadonlyState

4
packages/nc-gui-v2/nuxt-shim.d.ts vendored

@ -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
}
}

Loading…
Cancel
Save