Skip to content

📚 The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Fix sidebar reactivity

Smaug673972fcd24faec9
diff --git a/frontend/app/components/Sidebar/Sidebar.vue b/frontend/app/components/Sidebar/Sidebar.vue
index 1dbc7cdf..4604e88c 100644
--- a/frontend/app/components/Sidebar/Sidebar.vue
+++ b/frontend/app/components/Sidebar/Sidebar.vue
@@ -22,7 +22,7 @@
<div class="icons">
<NuxtLink @click="onClick" to="/dashboard/docs/new"><Icon name="add_file" :mid="true" fill="var(--font-color)" /></NuxtLink>
<NuxtLink @click="newCategory"><Icon name="add_folder" :mid="true" fill="var(--font-color)" /></NuxtLink>
- <NuxtLink @click="collapseAllStates"><Icon name="collapse" :mid="true" fill="var(--font-color)" /></NuxtLink>
+ <NuxtLink @click="sidebarTree.collapseAll"><Icon name="collapse" :mid="true" fill="var(--font-color)" /></NuxtLink>
<NuxtLink @click="toggleDock"><Icon name="dock" :mid="true" fill="var(--font-color)" /></NuxtLink>
</div>
</div>
diff --git a/frontend/app/composables/collapseStates.ts b/frontend/app/composables/collapseStates.ts
index 6533ff15..1c90cc98 100644
--- a/frontend/app/composables/collapseStates.ts
+++ b/frontend/app/composables/collapseStates.ts
@@ -30,14 +30,12 @@ export function collapseAllStates() {
saveCollapseStates();
}
-// Getter réactif
-export function getCollapseState(id: string): Ref<boolean> {
+export function getCollapseState(id: string): boolean {
const states = loadCollapseStates();
- if (!(id in states)) states[id] = true; // défaut : ouvert
- return ref(states[id] ?? true);
+ if (!(id in states)) states[id] = true; // default open
+ return states[id] ?? true;
}
-// Setter (lié à la ref)
export function setCollapseState(id: string, value: boolean) {
const states = loadCollapseStates();
states[id] = value;
diff --git a/frontend/app/composables/useSidebarTree.ts b/frontend/app/composables/useSidebarTree.ts
index 30efbfb9..a17b3ff6 100644
--- a/frontend/app/composables/useSidebarTree.ts
+++ b/frontend/app/composables/useSidebarTree.ts
@@ -1,13 +1,15 @@
+// useSidebarTree.ts
import type { Category, Document, Ressource } from '~/stores';
let initialized = false;
-const documents = shallowRef<Item<Document>[]>([]);
-const categories = shallowRef<Item<Category>[]>([]);
-const ressources = shallowRef<Item<Ressource>[]>([]);
-const collapseStates = new Map<string, Ref<boolean>>();
-const allItems = shallowRef<Item[]>([]);
-const structure = shallowRef<TreeStructure>(new TreeStructure(allItems.value));
-const tree = shallowRef<Item[]>([]);
+
+let categories: ComputedRef<Item<Category>[]>;
+let documents: ComputedRef<Item[]>;
+let ressources: ComputedRef<Item[]>;
+let allItems: ComputedRef<Item[]>;
+let structure: ComputedRef<TreeStructure>;
+let tree: ComputedRef<Item[]>;
+let filtered: ComputedRef<Item[]>;
export function useSidebarTree() {
const categoriesStore = useCategoriesStore();
@@ -16,42 +18,42 @@ export function useSidebarTree() {
const preferencesStore = usePreferences();
const { workspaceId } = useSidebar();
- // **************************************************************
- // Helper to get "Itemized" categories, documents, and ressources
- // **************************************************************
if (!initialized) {
- for (const c of categoriesStore.categories) {
- collapseStates.set(c.id, ref(getCollapseState(c.id)));
- }
- for (const d of documentsStore.documents) {
- collapseStates.set(d.id, ref(getCollapseState(d.id)));
- }
- categories.value = categoriesStore.categories.map(cat => ({
- id: cat.id,
- parent_id: cat.parent_id || '',
- label: cat.name,
- route: `/dashboard/categories/${cat.id}`,
- icon: cat.icon || 'folder',
- data: cat,
- show: collapseStates.get(cat.id)!,
- }));
+ initialized = true;
+
+ // Categories
+ categories = computed(() =>
+ categoriesStore.categories.map(cat => ({
+ id: cat.id,
+ parent_id: cat.parent_id || '',
+ label: cat.name,
+ route: `/dashboard/categories/${cat.id}`,
+ icon: cat.icon || 'folder',
+ data: cat,
+ show: ref(getCollapseState(cat.id)),
+ })),
+ );
- documents.value = documentsStore.documents
- .slice()
- .sort((a, b) => b.pinned - a.pinned || a.name.localeCompare(b.name))
- .map(doc => ({
- id: doc.id,
- parent_id: doc.parent_id || doc.category || '',
- label: doc.name,
- route: `/dashboard/docs/${doc.id}`,
- icon: doc.accessibility == 1 ? 'file' : doc.accessibility == 2 ? 'draft' : 'archive',
- data: doc,
- show: collapseStates.get(doc.id)!,
- }));
+ // Documents
+ documents = computed(() =>
+ documentsStore.documents
+ .slice() // éviter mutation
+ .sort((a, b) => b.pinned - a.pinned || a.name.localeCompare(b.name))
+ .map(doc => ({
+ id: doc.id,
+ parent_id: doc.parent_id || doc.category || '',
+ label: doc.name,
+ route: `/dashboard/docs/${doc.id}`,
+ icon: doc.accessibility == 1 ? 'file' : doc.accessibility == 2 ? 'draft' : 'archive',
+ data: doc,
+ show: ref(getCollapseState(doc.id)),
+ })),
+ );
- ressources.value = preferencesStore.get('hideSidebarRessources')
- ? []
- : ressourcesStore.getAll
+ // Ressources
+ ressources = computed(() => {
+ if (!preferencesStore.get('hideSidebarRessources'))
+ return ressourcesStore.getAll
.filter(res => res.parent_id)
.map(res => ({
id: res.id,
@@ -62,40 +64,49 @@ export function useSidebarTree() {
data: res,
show: ref(true),
}));
+ return [];
+ });
- allItems.value = [...documents.value, ...ressources.value, ...categories.value.filter(cat => (cat as Item<Category>).data.role == 1)];
-
- structure.value = new TreeStructure(allItems.value);
+ allItems = computed(() => [...documents.value, ...ressources.value, ...categories.value.filter(cat => (cat as Item<Category>).data.role == 1)]);
+ // Structure
+ structure = computed(() => new TreeStructure(allItems.value));
- // gestion des icônes spéciales
- if (!preferencesStore.get('normalizeFileIcons')) {
- for (const i of structure.value.childrenMap.keys()) {
- const ref = structure.value.itemMap.get(i);
- const subs = structure.value.childrenMap.get(i);
- if (ref?.data.type === 'document' && subs?.some(c => c.data.type != 'ressource')) {
- structure.value.itemMap.get(i)!.icon = 'file_parent';
+ // Tree with normalized icons
+ tree = computed(() => {
+ if (!preferencesStore.get('normalizeFileIcons')) {
+ for (const i of structure.value.childrenMap.keys()) {
+ const ref = structure.value.itemMap.get(i);
+ const subs = structure.value.childrenMap.get(i);
+ if (ref?.data.type === 'document' && subs?.some(c => c.data.type != 'ressource')) {
+ structure.value.itemMap.get(i)!.icon = 'file_parent';
+ }
}
}
- }
+ return structure.value.generateTree();
+ });
- tree.value = structure.value.generateTree();
+ // Filtered tree based on workspace
+ filtered = computed(() =>
+ tree.value.filter(item => {
+ if (item.data.type === 'category' && workspaceId.value) return item.data.workspace_id === workspaceId.value && item.data.role == 1;
+ if (item.data.type === 'document' && workspaceId.value) return item.data.category === workspaceId.value;
+ return true;
+ }),
+ );
}
- // **************************************************************
-
- const filtered = computed(() => {
- return tree.value.filter(item => {
- if (item.data.type === 'category' && workspaceId.value) return item.data.workspace_id === workspaceId.value && item.data.role == 1;
- if (item.data.type === 'document' && workspaceId.value) return item.data.category === workspaceId.value;
- return true;
+ const collapseAll = () => {
+ allItems.value.forEach(item => {
+ if (item.show.value) item.show.value = false;
+ collapseAllStates();
});
- });
-
+ };
const getSubTreeById = (id: string) => {
return structure.value.treeToArray(structure.value.getSubTreeById(id)?.childrens || []);
};
return {
structure,
+ collapseAll,
getSubTreeById,
categories,
tree,
diff --git a/frontend/app/stores/documents.store.ts b/frontend/app/stores/documents.store.ts
index 233bfb64..c63b3ce7 100644
--- a/frontend/app/stores/documents.store.ts
+++ b/frontend/app/stores/documents.store.ts
@@ -3,7 +3,7 @@ import type { DB_Document, Document } from './db_strustures';
export const useDocumentsStore = defineStore('documents', {
state: () => ({
- documents: shallowRef<Document[]>([]),
+ documents: ref<Document[]>([]),
}),
getters: {
getAll: state => state.documents,