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.

improvments

Veauville Raphael4b67a42a4521
diff --git a/backend/controllers/documents.controller.ts b/backend/controllers/documents.controller.ts
index 586a6531..0da6c613 100644
--- a/backend/controllers/documents.controller.ts
+++ b/backend/controllers/documents.controller.ts
@@ -13,17 +13,10 @@ export default class DocumentsController {
}
getAllDocuments(req: Request, res: Response) {
- if (req.query.category) {
- this.db_a
- .getAllByCategory(req.query.category.toString())
- .then((value: any) => res.status(200).json(success(value)))
- .catch((err: Error) => res.status(500).json(error(err.message)));
- } else {
- this.db_a
- .getAll(req.query.all?.toString())
- .then((result: Partial<Document>[]) => res.status(200).json(success(result)))
- .catch(e => res.status(500).json(error(e)));
- }
+ this.db_a
+ .getAll(req.query.all?.toString())
+ .then((result: Partial<Document>[]) => res.status(200).json(success(result)))
+ .catch(e => res.status(500).json(error(e)));
}
getDocument(req: Request, res: Response) {
this.db_a
diff --git a/dashboard/components/sidebar/CollapseItem.vue b/dashboard/components/sidebar/CollapseItem.vue
index e2103e2c..571d2f77 100644
--- a/dashboard/components/sidebar/CollapseItem.vue
+++ b/dashboard/components/sidebar/CollapseItem.vue
@@ -1,5 +1,5 @@
<template>
- <div v-if="item.childrens.length">
+ <div v-if="item.childrens?.length">
<div v-if="root" class="collapse-header">
<SidebarItem :item="item" @click="toggleShow">
<svg style="margin-left:auto;" :class="{ 'rotated': !show }" xmlns="http://www.w3.org/2000/svg" height="24"
@@ -23,7 +23,7 @@
<SidebarItem v-else :item="item" />
</template>
<script setup lang="ts">
-import { type Item } from './helpers';
+import { type Item } from './tree_builder';
import div from './Collapse.vue';
import SidebarItem from './SidebarItem.vue';
diff --git a/dashboard/components/sidebar/Sidebar.vue b/dashboard/components/sidebar/Sidebar.vue
index 21ca7c49..c2e2e530 100644
--- a/dashboard/components/sidebar/Sidebar.vue
+++ b/dashboard/components/sidebar/Sidebar.vue
@@ -7,7 +7,7 @@
<IconClose class="btn" />
</section>
<SidebarDropdown :categories="categoriesStore.getParents" v-model="category_dropdown_filter" />
- <CollapseItem v-for="(item, index ) in items " :key="index" :item="item" :root="true" />
+ <CollapseItem v-for="(item, index) in items " :key="index" :item="item" :root="true" />
</div>
</Resizable>
</template>
@@ -17,10 +17,9 @@ import CollapseItem from './CollapseItem.vue';
import Resizable from './Resizable.vue';
import IconClose from './IconClose.vue';
import IconApp from './IconApp.vue';
-import { useCategoriesStore, useDocumentsStore, type Document } from '~/store';
-import { type Item, defaultItems } from "./helpers";
-
-type DocumentTree = Document & { childrens: DocumentTree[] };
+import { useCategoriesStore, useDocumentsStore, type Category } from '~/store';
+import { ItemsManager, type Item } from './tree_builder';
+import { navigationItems } from "./helpers";
const { isOpened, hasSidebar } = useSidebar();
const category_dropdown_filter = ref('');
@@ -30,79 +29,29 @@ const documentsStore = useDocumentsStore();
const isMobile = () => process.client ? window.innerWidth <= 768 : false;
-const items = computed((): Item[] => [
- ...defaultItems,
- ...formatTree().filter((item) => {
- if (category_dropdown_filter.value === '') return true;
- return item.id === category_dropdown_filter.value;
- })
-]);
-
-
-function formatTree(): Item[] {
- const items: Item[] = [];
- categoriesStore.getParents.forEach((parent) => {
- const childrens: Item[] = [];
- categoriesStore.getChilds(parent.id).forEach((child) => {
- childrens.push({
- id: child.id,
- title: child.name,
- icon: child.icon,
- route: '',
- childrens: documentsTreeToItems(convertDocumentsToTree(documentsStore.getByCategories(child.id)))
- });
- });
- items.push({
- id: parent.id,
- title: parent.name,
- icon: `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--yellow);" d="M160-160q-33 0-56.5-23.5T80-240v-480q0-33 23.5-56.5T160-800h240l80 80h320q33 0 56.5 23.5T880-640H447l-80-80H160v480l96-320h684L837-217q-8 26-29.5 41.5T760-160H160Zm84-80h516l72-240H316l-72 240Zm0 0 72-240-72 240Zm-84-400v-80 80Z"/></svg>`,
- childrens,
- route: ``
- });
- });
- return items;
-}
-
-function convertDocumentsToTree(documents: Document[]): DocumentTree[] {
- const documentMap: { [id: string]: DocumentTree } = {};
-
- // Create a map of documents indexed by their id
- documents.forEach(doc => {
- documentMap[doc.id] = { ...doc, childrens: [] };
- });
-
- // Build the tree structure
- const documentTree: DocumentTree[] = [];
- documents.forEach(doc => {
- if (doc.parent_id && documentMap[doc.parent_id]) {
- documentMap[doc.parent_id].childrens.push(documentMap[doc.id]);
- } else {
- documentTree.push(documentMap[doc.id]);
- }
- });
- return documentTree;
-}
-
-function documentsTreeToItems(documentsTree: DocumentTree[]): Item[] {
- const items: Item[] = [];
- documentsTree.forEach((doc) => {
- const childrens: Item[] = documentsTreeToItems(doc.childrens);
- items.push({
- id: doc.id,
- title: doc.name,
- icon: defaultIcon(childrens.length > 0),
- route: `/dashboard?doc=${doc.id}`,
- childrens: childrens,
- });
- });
- return items;
-}
-
-function defaultIcon(hasChild: boolean): string {
- if (hasChild) return '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--green);" d="M200-120q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h560q33 0 56.5 23.5T840-760v560q0 33-23.5 56.5T760-120H200Zm0-80h240v-560H200v560Zm320 0h240v-280H520v280Zm0-360h240v-200H520v200Z"/></svg>';
- return '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--pink);" d="M320-240h320v-80H320v80Zm0-160h320v-80H320v80ZM240-80q-33 0-56.5-23.5T160-160v-640q0-33 23.5-56.5T240-880h320l240 240v480q0 33-23.5 56.5T720-80H240Zm280-520v-200H240v640h480v-440H520ZM240-800v200-200 640-640Z"/></svg>';
-}
+const items = computed((): Item[] => {
+ const navigation: Item[] = navigationItems.map((item) => ({ id: item.id, parent_id: '', title: item.title, route: item.route, icon: item.icon, type: 'navigation', data: item }))
+ const categories: Item[] = categoriesStore.categories.map((category: Category) => ({
+ id: category.id,
+ parent_id: category.parent_id || '',
+ title: category.name,
+ route: '',
+ icon: category.icon,
+ data: category,
+ }));
+ const documents: Item[] = documentsStore.documents.map((document) => ({
+ id: document.id,
+ parent_id: document.parent_id || document.category || '',
+ title: document.name,
+ route: `/dashboard?doc=${document.id}`,
+ data: document
+ }))
+
+ const itemsManager = new ItemsManager([...navigation, ...categories, ...documents]);
+ const tree = itemsManager.generateTree()
+ return tree;
+});
const handleClickOutside = (e: MouseEvent) => {
@@ -121,7 +70,6 @@ onBeforeUnmount(() => {
window.removeEventListener('click', handleClickOutside);
});
-
// Watch isOpened
watch(isOpened, (val) => {
if (process.client && val && isMobile()) document.getElementById('backdrop')?.classList.add('backdrop');
diff --git a/dashboard/components/sidebar/SidebarItem.vue b/dashboard/components/sidebar/SidebarItem.vue
index 5fa18c4c..ef5237da 100644
--- a/dashboard/components/sidebar/SidebarItem.vue
+++ b/dashboard/components/sidebar/SidebarItem.vue
@@ -1,18 +1,28 @@
<template>
<NuxtLink class="item" :to="item.route" :class="{ active: isActive(item.id) }">
- <i v-html="item.icon" class="icon"></i>
+ <i v-html="getIcon(item)" class="icon"></i>
<span>{{ item.title }}</span>
<slot></slot>
</NuxtLink>
</template>
<script setup lang="ts">
-import { type Item } from './helpers';
+import type { Item } from './tree_builder';
+import { icons } from './helpers';
const route = useRoute();
defineProps<{ item: Item }>();
const doc_id = computed(() => route.query.doc as string);
const isActive = (id: string) => doc_id.value === id;
+
+function getIcon(item: Item) {
+ if ('icon' in item.data && item.data.icon) return item.data.icon;
+ if (item.data.type === 'category') return icons.folder;
+ if (item.data.type === 'document' && item.childrens?.length) return icons.file_parent;
+ if (item.data.type === 'document' && item.data.accessibility != 1) return icons.draft;
+ return icons.file;
+}
+
</script>
<style lang="scss" scoped>
diff --git a/dashboard/components/sidebar/helpers.ts b/dashboard/components/sidebar/helpers.ts
index bd74ca76..ae286afb 100644
--- a/dashboard/components/sidebar/helpers.ts
+++ b/dashboard/components/sidebar/helpers.ts
@@ -1,14 +1,16 @@
-export interface Item {
+export interface DefaultItem {
id: string;
+ type: 'default';
title: string;
icon?: string;
route: string;
- childrens: Item[];
+ childrens: DefaultItem[];
}
-export const defaultItems: Item[] = [
+export const navigationItems: DefaultItem[] = [
{
id: 'manage-categories',
+ type: 'default',
title: 'Manage categories',
icon: '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M320-320h480v-480h-80v280l-100-60-100 60v-280H320v480Zm0 80q-33 0-56.5-23.5T240-320v-480q0-33 23.5-56.5T320-880h480q33 0 56.5 23.5T880-800v480q0 33-23.5 56.5T800-240H320ZM160-80q-33 0-56.5-23.5T80-160v-560h80v560h560v80H160Zm360-720h200-200Zm-200 0h480-480Z"/></svg>',
route: '/dashboard/categories',
@@ -16,6 +18,7 @@ export const defaultItems: Item[] = [
},
{
id: 'cdn',
+ type: 'default',
title: 'CDN',
icon: '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M485-440h163q26 0 44-18t18-44q0-26-18-44.5T648-565h-2q-5-32-29-53.5T560-640q-26 0-47 13.5T481-590q-30 2-50.5 23.5T410-515q0 30 21.5 52.5T485-440ZM120-120q-33 0-56.5-23.5T40-200v-520h80v520h680v80H120Zm160-160q-33 0-56.5-23.5T200-360v-440q0-33 23.5-56.5T280-880h200l80 80h280q33 0 56.5 23.5T920-720v360q0 33-23.5 56.5T840-280H280Zm0-80h560v-360H527l-80-80H280v440Zm0 0v-440 440Z"/></svg>',
route: '/dashboard/cdn',
@@ -23,6 +26,7 @@ export const defaultItems: Item[] = [
},
{
id: 'backup',
+ type: 'default',
title: 'Backup',
icon: '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M260-160q-91 0-155.5-63T40-377q0-78 47-139t123-78q17-72 85-137t145-65q33 0 56.5 23.5T520-716v242l64-62 56 56-160 160-160-160 56-56 64 62v-242q-76 14-118 73.5T280-520h-20q-58 0-99 41t-41 99q0 58 41 99t99 41h480q42 0 71-29t29-71q0-42-29-71t-71-29h-60v-80q0-48-22-89.5T600-680v-93q74 35 117 103.5T760-520q69 8 114.5 59.5T920-340q0 75-52.5 127.5T740-160H260Zm220-358Z"/></svg>',
route: '/dashboard/backup',
@@ -30,9 +34,19 @@ export const defaultItems: Item[] = [
},
{
id: 'new-page',
+ type: 'default',
title: 'New page',
icon: '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M440-280h80v-160h160v-80H520v-160h-80v160H280v80h160v160ZM200-120q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h560q33 0 56.5 23.5T840-760v560q0 33-23.5 56.5T760-120H200Zm0-80h560v-560H200v560Zm0-560v560-560Z"/></svg>',
route: '/dashboard/new',
childrens: [],
},
];
+export const icons = {
+ draft:
+ '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--yellow)" d="M240-80q-33 0-56.5-23.5T160-160v-640q0-33 23.5-56.5T240-880h320l240 240v480q0 33-23.5 56.5T720-80H240Zm280-520v-200H240v640h480v-440H520ZM240-800v200-200 640-640Z"/></svg>',
+ folder:
+ '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--yellow);" d="M160-160q-33 0-56.5-23.5T80-240v-480q0-33 23.5-56.5T160-800h240l80 80h320q33 0 56.5 23.5T880-640H447l-80-80H160v480l96-320h684L837-217q-8 26-29.5 41.5T760-160H160Zm84-80h516l72-240H316l-72 240Zm0 0 72-240-72 240Zm-84-400v-80 80Z"/></svg>',
+ file: '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--pink);" d="M320-240h320v-80H320v80Zm0-160h320v-80H320v80ZM240-80q-33 0-56.5-23.5T160-160v-640q0-33 23.5-56.5T240-880h320l240 240v480q0 33-23.5 56.5T720-80H240Zm280-520v-200H240v640h480v-440H520ZM240-800v200-200 640-640Z"/></svg>',
+ file_parent:
+ '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path style="fill:var(--green);" d="M200-120q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h560q33 0 56.5 23.5T840-760v560q0 33-23.5 56.5T760-120H200Zm0-80h240v-560H200v560Zm320 0h240v-280H520v280Zm0-360h240v-200H520v200Z"/></svg>',
+};
diff --git a/dashboard/components/sidebar/tree_builder.ts b/dashboard/components/sidebar/tree_builder.ts
new file mode 100644
index 00000000..f6ef3ca1
--- /dev/null
+++ b/dashboard/components/sidebar/tree_builder.ts
@@ -0,0 +1,37 @@
+import type { Document, Category } from '~/store';
+import type { DefaultItem } from './helpers';
+
+export interface Item {
+ id: string;
+ title: string;
+ route: string;
+ parent_id: string;
+ data: Document | Category | DefaultItem;
+ childrens?: Item[];
+}
+
+export class ItemsManager {
+ constructor(public readonly items: Item[]) {}
+
+ public getItem(id: string): Item | undefined {
+ return this.items.find(item => item.id === id);
+ }
+
+ public generateTree(): Item[] {
+ const tree: Item[] = [];
+ const rootItems = this.items.filter(item => item.parent_id === ''); // Get root items
+ rootItems.forEach(rootItem => {
+ const childrens = this.getChildrens(rootItem);
+ tree.push({ ...rootItem, childrens });
+ });
+ return tree;
+ }
+
+ private getChildrens(parent: Item): Item[] {
+ const childrens = this.items.filter(item => item.parent_id === parent.id);
+ childrens.forEach(child => {
+ child.childrens = this.getChildrens(child);
+ });
+ return childrens;
+ }
+}
diff --git a/dashboard/store/categories.store.ts b/dashboard/store/categories.store.ts
index 2d279ad4..0357d938 100644
--- a/dashboard/store/categories.store.ts
+++ b/dashboard/store/categories.store.ts
@@ -16,14 +16,14 @@ export const useCategoriesStore = defineStore('categories', {
actions: {
fetch: async function (opts?: FetchOptions<Array<keyof Category>>) {
return new Promise(async (resolve, reject) => {
- const request = await makeRequest(`categories/${opts?.id || ''}?fields=${opts?.fields?.join(',')}`, 'GET', {});
+ const request = await makeRequest(`categories/${opts?.id || ''}`, 'GET', {});
if (request.status == 'success') {
if (opts?.id) {
// replace the document with the new one
const index = this.categories.findIndex(d => d.id == opts?.id);
if (index == -1) this.categories.push(request.result);
- else this.categories[index] = request.result;
- } else this.categories = request.result;
+ else this.categories[index] = { ...request.result, type: 'category' };
+ } else this.categories = request.result.map((d: Category) => ({ ...d, type: 'category' }));
resolve(this.categories);
} else reject(request.message);
});
diff --git a/dashboard/store/db_strustures.d.ts b/dashboard/store/db_strustures.d.ts
index 7bf68bf8..c189e1b1 100644
--- a/dashboard/store/db_strustures.d.ts
+++ b/dashboard/store/db_strustures.d.ts
@@ -14,6 +14,7 @@ export interface Document {
// not in the database
partial?: boolean;
+ type: 'document';
}
export interface Category {
@@ -22,6 +23,9 @@ export interface Category {
icon?: string;
order?: number;
parent_id?: string;
+
+ // not in the database
+ type: 'category';
}
export interface User {
diff --git a/dashboard/store/documents.store.ts b/dashboard/store/documents.store.ts
index dadf8143..8b53c792 100644
--- a/dashboard/store/documents.store.ts
+++ b/dashboard/store/documents.store.ts
@@ -29,22 +29,17 @@ export const useDocumentsStore = defineStore('documents', {
actions: {
fetch: async function <T extends FetchOptions<Array<keyof Document>>>(opts?: T): Promise<'id' extends keyof T ? Document : Document[]> {
console.log(`[store/documents] Fetching documents with options: ${JSON.stringify(opts)}`);
-
return new Promise(async (resolve, reject) => {
- const request = await makeRequest(
- `documents/${opts?.id || ''}${opts?.fields?.length ? '?all=true&fields=' + opts?.fields?.join(',') : ''}`,
- 'GET',
- {},
- );
+ const request = await makeRequest(`documents/${opts?.id || ''}?all=true`, 'GET', {});
if (request.status == 'success') {
if (opts?.id) {
const index = this.documents.findIndex(d => d.id == opts?.id);
- const updatedDocument: Document = { ...request.result, partial: false };
+ const updatedDocument: Document = { ...request.result, partial: false, type: 'document' };
if (index == -1) this.documents.push(updatedDocument);
else this.documents[index] = updatedDocument;
resolve(updatedDocument as 'id' extends keyof T ? Document : Document[]);
} else {
- this.documents = request.result.map((d: Document) => ({ ...d, partial: true }));
+ this.documents = request.result.map((d: Document) => ({ ...d, partial: true, type: 'document' }));
resolve(this.documents as 'id' extends keyof T ? Document : Document[]);
}
} else reject(request.message);
diff --git a/dashboard/store/utils.ts b/dashboard/store/utils.ts
index 78687eed..c996fcf8 100644
--- a/dashboard/store/utils.ts
+++ b/dashboard/store/utils.ts
@@ -6,7 +6,6 @@ export interface APIResult<Data> {
export interface FetchOptions<K> {
id: string; // id of the ressource
- fields?: K; // fields to include in the responce
}
export const baseUrl = import.meta.env.VITE_BASE_API?.toString() || '';