mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
2 years ago
|
import { useGlobalState } from './state'
|
||
|
import { useGlobalActions } from './actions'
|
||
2 years ago
|
import type { UseGlobalReturn } from './types'
|
||
|
import { useGlobalGetters } from './getters'
|
||
2 years ago
|
import { useNuxtApp, watch } from '#imports'
|
||
2 years ago
|
|
||
|
/**
|
||
|
* Global state is injected by {@link import('~/plugins/state') state} plugin into our nuxt app (available as `$state`).
|
||
2 years ago
|
* You can still call `useGlobal` to receive the `$state` object and access the global state.
|
||
|
* If it's not available yet, a new global state object is created and injected into the nuxt app.
|
||
2 years ago
|
*
|
||
2 years ago
|
* Part of the state is stored in {@link WindowLocalStorage localStorage}, so it will be available even if the user closes the browser tab.
|
||
|
* Check the {@link StoredState StoredState} type for more information.
|
||
2 years ago
|
*
|
||
|
* @example
|
||
|
* ```js
|
||
|
* import { useNuxtApp } from '#app'
|
||
|
*
|
||
|
* const { $state } = useNuxtApp()
|
||
|
*
|
||
|
* const token = $state.token.value
|
||
|
* const user = $state.user.value
|
||
|
* ```
|
||
2 years ago
|
*
|
||
|
* @example
|
||
|
* ```js
|
||
|
* import { useGlobal } from '#imports'
|
||
|
*
|
||
|
* const globalState = useGlobal()
|
||
|
*
|
||
|
* cont token = globalState.token.value
|
||
|
* const user = globalState.user.value
|
||
|
*
|
||
|
* console.log(state.isLoading.value) // isLoading = true if any api request is still running
|
||
|
* ```
|
||
2 years ago
|
*/
|
||
2 years ago
|
export const useGlobal = (): UseGlobalReturn => {
|
||
2 years ago
|
const { $state, provide } = useNuxtApp()
|
||
|
|
||
2 years ago
|
/** If state already exists, return it */
|
||
2 years ago
|
if (typeof $state !== 'undefined') return $state
|
||
2 years ago
|
|
||
2 years ago
|
const state = useGlobalState()
|
||
2 years ago
|
|
||
2 years ago
|
const getters = useGlobalGetters(state)
|
||
2 years ago
|
|
||
2 years ago
|
const actions = useGlobalActions(state)
|
||
2 years ago
|
|
||
|
/** try to refresh token before expiry (5 min before expiry) */
|
||
|
watch(
|
||
2 years ago
|
() =>
|
||
|
!!(
|
||
|
state.jwtPayload.value &&
|
||
|
state.jwtPayload.value.exp &&
|
||
|
state.jwtPayload.value.exp - 5 * 60 < state.timestamp.value / 1000
|
||
|
),
|
||
2 years ago
|
async (expiring) => {
|
||
2 years ago
|
if (getters.signedIn.value && state.jwtPayload.value && expiring) {
|
||
2 years ago
|
await actions.refreshToken()
|
||
|
}
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
|
||
2 years ago
|
const globalState = { ...state, ...getters, ...actions } as UseGlobalReturn
|
||
2 years ago
|
|
||
2 years ago
|
/** provide a fresh state instance into nuxt app */
|
||
|
provide('state', globalState)
|
||
2 years ago
|
|
||
2 years ago
|
return globalState
|
||
2 years ago
|
}
|