Browse Source

refactor(nc-gui): remove unnecessary injection state from useSidebar

pull/3703/head
braks 2 years ago
parent
commit
1fd6e08b1c
  1. 28
      packages/nc-gui/composables/useSidebar/index.ts

28
packages/nc-gui/composables/useSidebar/index.ts

@ -1,5 +1,5 @@
import { useStorage } from '@vueuse/core' import { useStorage } from '@vueuse/core'
import { MemStorage, onScopeDispose, useInjectionState, watch } from '#imports' import { MemStorage, onScopeDispose, watch } from '#imports'
interface UseSidebarProps { interface UseSidebarProps {
hasSidebar?: boolean hasSidebar?: boolean
@ -8,14 +8,16 @@ interface UseSidebarProps {
} }
/** /**
* Injection state for sidebars * States for sidebars
* *
* Use `provideSidebar` to provide the injection state on current component level (will affect all children injections) * Use `provideSidebar` to provide the state and save to memory or local storage
* Use `useSidebar` to use the injection state on current component level * Use `useSidebar` to use the state
* *
* If `provideSidebar` is not called explicitly, `useSidebar` will trigger the provider if no injection state can be found * If `provideSidebar` is not called explicitly, `useSidebar` will trigger the provider if no state can be found
*
* Requires an id to work, id should correspond to the sidebar state you want to create or fetch
*/ */
const [setupSidebarStore, useSidebarStore] = useInjectionState(() => new MemStorage(), 'SidebarStore') const sidebarStorage = new MemStorage()
const createSidebar = (id: string, props: UseSidebarProps = {}) => { const createSidebar = (id: string, props: UseSidebarProps = {}) => {
const isOpen = ref(props.isOpen ?? false) const isOpen = ref(props.isOpen ?? false)
@ -59,18 +61,8 @@ const createSidebar = (id: string, props: UseSidebarProps = {}) => {
} }
} }
const useSidebarStorage = () => {
let sidebarStorage = useSidebarStore()
if (!sidebarStorage) {
sidebarStorage = setupSidebarStore()
}
return sidebarStorage
}
export const provideSidebar = (id: string, props: UseSidebarProps = {}) => { export const provideSidebar = (id: string, props: UseSidebarProps = {}) => {
const sidebarStorage = useSidebarStorage() if (!id) throw new Error('provideSidebar requires an id')
onScopeDispose(() => { onScopeDispose(() => {
sidebarStorage.remove(id) sidebarStorage.remove(id)
@ -95,8 +87,6 @@ export const provideSidebar = (id: string, props: UseSidebarProps = {}) => {
export function useSidebar(id: string, props: UseSidebarProps = {}) { export function useSidebar(id: string, props: UseSidebarProps = {}) {
if (!id) throw new Error('useSidebar requires an id') if (!id) throw new Error('useSidebar requires an id')
const sidebarStorage = useSidebarStorage()
if (sidebarStorage.has(id)) { if (sidebarStorage.has(id)) {
const sidebar = sidebarStorage.get(id) const sidebar = sidebarStorage.get(id)

Loading…
Cancel
Save