mirror of https://github.com/nocodb/nocodb
Braks
3 years ago
committed by
Pranav C
7 changed files with 45 additions and 46 deletions
@ -1,15 +1,28 @@
|
||||
import { createGlobalState, usePreferredDark, usePreferredLanguages, useStorage } from '@vueuse/core' |
||||
import type { GlobalState } from '~/lib/types' |
||||
import { usePreferredDark, usePreferredLanguages, useStorage } from '@vueuse/core' |
||||
import { computed, toRefs } from '#build/imports' |
||||
import type { GlobalState, State } from '~/lib/types' |
||||
|
||||
const storageKey = 'nocodb-gui-v2' |
||||
|
||||
/** |
||||
* Global State is injected by state plugin, so manual initialization is unnecessary and should be avoided |
||||
*/ |
||||
export const useGlobalState = () => { |
||||
export const useGlobalState = (): GlobalState => { |
||||
const preferredLanguages = $(usePreferredLanguages()) |
||||
const darkMode = $(usePreferredDark()) |
||||
return createGlobalState(() => |
||||
useStorage<GlobalState>(storageKey, { token: undefined, user: undefined, lang: preferredLanguages[0] || 'en', darkMode }), |
||||
) |
||||
|
||||
const initialState = { token: undefined, user: undefined, lang: preferredLanguages[0] || 'en', darkMode } |
||||
|
||||
const storage = useStorage<State>(storageKey, initialState) |
||||
|
||||
// getters
|
||||
const signedIn = computed(() => storage.value.token !== undefined && storage.value.user !== undefined) |
||||
|
||||
// actions
|
||||
function signOut() { |
||||
storage.value.token = undefined |
||||
storage.value.user = undefined |
||||
} |
||||
|
||||
return { ...toRefs(storage.value), signedIn, signOut } |
||||
} |
||||
|
Loading…
Reference in new issue