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.

Peformance improvment 1

Smaug6739e107fabd9fc7
diff --git a/frontend/app/composables/TreeStructure.ts b/frontend/app/composables/TreeStructure.ts
index 478306d2..88ffb2aa 100644
--- a/frontend/app/composables/TreeStructure.ts
+++ b/frontend/app/composables/TreeStructure.ts
@@ -16,21 +16,28 @@ export interface Item<T = Document | Category | DefaultItem | Ressource> extends
childrens?: Item<T>[];
icon?: string;
}
+
export class TreeStructure {
public readonly items: Item[];
- public itemMap: Map<string, Item>; // Utilisation de Map pour les recherches rapides
- public childrenMap: Map<string, Item[]>; // Utilisation de Map pour les recherches rapides
+
+ public readonly itemMap: Map<string, Item>; // Map for quick access by ID
+
+ public readonly childrenMap: Map<string, Item[]>; // Map for quick access to children by parent ID
+
+ private rootTree: Item[];
+
constructor(items: Item[]) {
+ console.log('MAIN: TreeStructure initialized with items:', items.length);
this.items = items;
this.itemMap = new Map(items.map(item => [item.id, item]));
this.childrenMap = new Map();
for (const item of items) {
- if (!this.childrenMap.has(item.parent_id || '')) {
- this.childrenMap.set(item.parent_id || '', []);
- }
+ if (!this.childrenMap.has(item.parent_id || '')) this.childrenMap.set(item.parent_id || '', []);
this.childrenMap.get(item.parent_id || '')!.push(item);
}
+
+ this.rootTree = this.buildRootTree(); // calculé une seule fois
}
// Get item by ID
@@ -39,10 +46,13 @@ export class TreeStructure {
}
// Generate the tree structure without indexing
+ private buildRootTree(): Item[] {
+ console.log('TreeStructure reconstructed');
+ return this.items.filter(item => item.parent_id === '' || !this.itemMap.has(item.parent_id || '') || (item.data.type === 'category' && item.data.role === 2)).map(root => this.buildTree(root));
+ }
+
public generateTree(): Item[] {
- return this.items
- .filter(item => item.parent_id === '' || !this.itemMap.has(item.parent_id || '') || (item.data.type === 'category' && item.data.role === 2)) // Racines
- .map(rootItem => this.buildTree(rootItem));
+ return this.items.filter(item => item.parent_id === '' || !this.itemMap.has(item.parent_id || '') || (item.data.type === 'category' && item.data.role === 2)).map(root => this.buildTree(root));
}
public getSubTreeById(id: string): Item | undefined {
const root = this.getItem(id);
diff --git a/frontend/app/composables/useSidebarTree.ts b/frontend/app/composables/useSidebarTree.ts
index bfb41705..db07b653 100644
--- a/frontend/app/composables/useSidebarTree.ts
+++ b/frontend/app/composables/useSidebarTree.ts
@@ -1,4 +1,13 @@
-import { type Category } from '~/stores';
+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[]>([]);
function getCollapseState(id: string): boolean {
const stored = localStorage.getItem(`collapse-${id}`);
@@ -12,20 +21,28 @@ export function useSidebarTree() {
const preferencesStore = usePreferences();
const { workspaceId } = useSidebar();
- const categories = computed<Item[]>(() =>
- categoriesStore.categories.map(cat => ({
+ // **************************************************************
+ // 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: ref(getCollapseState(cat.id)),
- })),
- );
+ show: collapseStates.get(cat.id)!,
+ }));
- const documents = computed<Item[]>(() =>
- documentsStore.documents
+ documents.value = documentsStore.documents
+ .slice()
.sort((a, b) => b.pinned - a.pinned || a.name.localeCompare(b.name))
.map(doc => ({
id: doc.id,
@@ -34,39 +51,41 @@ export function useSidebarTree() {
route: `/dashboard/docs/${doc.id}`,
icon: doc.accessibility == 1 ? 'file' : doc.accessibility == 2 ? 'draft' : 'archive',
data: doc,
- show: ref(getCollapseState(doc.id)),
- })),
- );
- const ressources = computed<Item[]>(() => {
- if (!preferencesStore.get('hideSidebarRessources'))
- return ressourcesStore.getAll
- .filter(res => res.parent_id)
- .map(res => ({
- id: res.id,
- parent_id: res.parent_id || '',
- label: res.filename,
- route: `/dashboard/cdn/${res.id}/preview`,
- icon: res.filetype == 'application/pdf' ? 'pdf' : 'image',
- data: res,
- show: ref(true),
- }));
- return [];
- });
+ show: collapseStates.get(doc.id)!,
+ }));
- const allItems = computed(() => [...documents.value, ...ressources.value, ...categories.value.filter(cat => (cat as Item<Category>).data.role == 1)]);
- const structure = computed(() => new TreeStructure(allItems.value));
+ ressources.value = preferencesStore.get('hideSidebarRessources')
+ ? []
+ : ressourcesStore.getAll
+ .filter(res => res.parent_id)
+ .map(res => ({
+ id: res.id,
+ parent_id: res.parent_id || '',
+ label: res.filename,
+ route: `/dashboard/cdn/${res.id}/preview`,
+ icon: res.filetype == 'application/pdf' ? 'pdf' : 'image',
+ data: res,
+ show: ref(true),
+ }));
- const tree = computed(() => {
- // Special icons:
+ allItems.value = [...documents.value, ...ressources.value, ...categories.value.filter(cat => (cat as Item<Category>).data.role == 1)];
+
+ structure.value = 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';
+ 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();
+ }
+ // **************************************************************
const filtered = computed(() => {
return tree.value.filter(item => {
diff --git a/frontend/app/stores/documents.store.ts b/frontend/app/stores/documents.store.ts
index c63b3ce7..233bfb64 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: ref<Document[]>([]),
+ documents: shallowRef<Document[]>([]),
}),
getters: {
getAll: state => state.documents,