2024-04-15 19:39:03 +02:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
|
import { useContentStore } from "~/composables/stores/useContentStore.js"
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const workspaceEndpoint = "open/workspaces/"
|
2023-12-14 16:53:05 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const useWorkspacesStore = defineStore("workspaces", () => {
|
|
|
|
|
const storedWorkspaceId = useCookie("currentWorkspace")
|
2023-12-19 18:57:31 +01:00
|
|
|
|
|
|
|
|
const contentStore = useContentStore()
|
|
|
|
|
const currentId = ref(storedWorkspaceId)
|
|
|
|
|
|
|
|
|
|
const getCurrent = computed(() => {
|
|
|
|
|
return contentStore.getByKey(currentId.value)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const setCurrentId = (id) => {
|
|
|
|
|
currentId.value = id
|
|
|
|
|
storedWorkspaceId.value = id
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-31 12:39:01 +01:00
|
|
|
const set = (items) => {
|
2024-04-15 19:39:03 +02:00
|
|
|
contentStore.content.value = new Map()
|
2023-12-31 12:39:01 +01:00
|
|
|
save(items)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 18:57:31 +01:00
|
|
|
const save = (items) => {
|
|
|
|
|
contentStore.save(items)
|
2024-04-15 19:39:03 +02:00
|
|
|
if (getCurrent.value == null && contentStore.length.value) {
|
2023-12-19 18:57:31 +01:00
|
|
|
setCurrentId(items[0].id)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
2023-12-19 18:57:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const remove = (itemId) => {
|
|
|
|
|
contentStore.remove(itemId)
|
|
|
|
|
if (currentId.value === itemId) {
|
2024-04-15 19:39:03 +02:00
|
|
|
setCurrentId(
|
|
|
|
|
contentStore.length.value > 0 ? contentStore.getAll.value[0].id : null,
|
|
|
|
|
)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-19 18:57:31 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...contentStore,
|
|
|
|
|
currentId,
|
|
|
|
|
getCurrent,
|
|
|
|
|
setCurrentId,
|
2023-12-31 12:39:01 +01:00
|
|
|
set,
|
2023-12-19 18:57:31 +01:00
|
|
|
save,
|
2024-04-15 19:39:03 +02:00
|
|
|
remove,
|
2023-12-19 18:57:31 +01:00
|
|
|
}
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|
2023-12-19 18:57:31 +01:00
|
|
|
|
|
|
|
|
export const fetchAllWorkspaces = (options = {}) => {
|
|
|
|
|
return useOpnApi(workspaceEndpoint, options)
|
|
|
|
|
}
|