Refactor sidebarTabStore (#1111)

This commit is contained in:
Chenlei Hu
2024-10-04 21:20:35 -04:00
committed by GitHub
parent a852b8e6e1
commit 2649d72d3f
4 changed files with 59 additions and 34 deletions

View File

@@ -0,0 +1,45 @@
import { SidebarTabExtension } from '@/types/extensionTypes'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
export const useSidebarTabStore = defineStore('sidebarTab', () => {
const sidebarTabs = ref<SidebarTabExtension[]>([])
const activeSidebarTabId = ref<string | null>(null)
const activeSidebarTab = computed<SidebarTabExtension | null>(() => {
return (
sidebarTabs.value.find((tab) => tab.id === activeSidebarTabId.value) ??
null
)
})
const toggleSidebarTab = (tabId: string) => {
activeSidebarTabId.value = activeSidebarTabId.value === tabId ? null : tabId
}
const registerSidebarTab = (tab: SidebarTabExtension) => {
sidebarTabs.value = [...sidebarTabs.value, tab]
}
const unregisterSidebarTab = (id: string) => {
const index = sidebarTabs.value.findIndex((tab) => tab.id === id)
if (index !== -1) {
const tab = sidebarTabs.value[index]
if (tab.type === 'custom' && tab.destroy) {
tab.destroy()
}
const newSidebarTabs = [...sidebarTabs.value]
newSidebarTabs.splice(index, 1)
sidebarTabs.value = newSidebarTabs
}
}
return {
sidebarTabs,
activeSidebarTabId,
activeSidebarTab,
toggleSidebarTab,
registerSidebarTab,
unregisterSidebarTab
}
})