// stores/wiki.ts import { defineStore } from 'pinia' export const useWikiStore = defineStore('wiki', () => { const flatItems = ref([]) const isLoading = ref(false) // Computed: Wandelt flatItems automatisch in Baum um, wenn sich Daten ändern const tree = computed(() => buildTree(flatItems.value)) // Action: Baum laden async function fetchTree(params: { entityType?: string, entityId?: number, entityUuid?: string } = {}) { isLoading.value = true try { const { data } = await useFetch('/api/wiki/tree', { query: params }) if (data.value) { flatItems.value = data.value } } catch (e) { console.error('Wiki tree fetch error', e) } finally { isLoading.value = false } } // Action: Eintrag verschieben (Drag & Drop im UI) async function moveItem(itemId: string, newParentId: string | null, newSortOrder: number) { // 1. Optimistic Update im State (damit es sich sofort schnell anfühlt) const itemIndex = flatItems.value.findIndex(i => i.id === itemId) if (itemIndex > -1) { flatItems.value[itemIndex].parentId = newParentId flatItems.value[itemIndex].sortOrder = newSortOrder } // 2. API Call im Hintergrund await $fetch(`/api/wiki/${itemId}`, { method: 'PATCH', body: { parentId: newParentId, sortOrder: newSortOrder } }) // Fallback: Wenn Error, müsste man hier den alten State wiederherstellen } return { flatItems, tree, isLoading, fetchTree, moveItem } })