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 memory leak whan rerender sidebar with collapse component (lose state)

Smaug6739f43bf99c7bc1
diff --git a/frontend/components/Sidebar/CollapseItem.vue b/frontend/components/Sidebar/CollapseItem.vue
index 1412ac3f..8950f733 100644
--- a/frontend/components/Sidebar/CollapseItem.vue
+++ b/frontend/components/Sidebar/CollapseItem.vue
@@ -1,13 +1,13 @@
<template>
<div v-if="item.childrens?.length">
<SidebarItem v-if="root" :item="item" @click="toggleShow" class="collapse-header">
- <svg style="margin-left: auto" :class="{ rotated: !show }" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24">
+ <svg style="margin-left: auto" :class="{ rotated: !props.item.show.value }" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24">
<path d="M480-345 240-585l56-56 184 184 184-184 56 56-240 240Z" />
</svg>
</SidebarItem>
<SidebarItem v-else :item="item" class="collapse-header" />
- <div v-if="show" class="collapse-body" v-for="(child, index) in item.childrens" style="margin-left: 20px">
- <CollapseItem v-if="child.childrens?.length" :item="child" :key="index" />
+ <div v-if="props.item.show.value" class="collapse-body" v-for="child in item.childrens" style="margin-left: 20px">
+ <CollapseItem v-if="child.childrens?.length" :item="child" />
<SidebarItem v-else :item="child" />
</div>
</div>
@@ -17,11 +17,10 @@
import type { Item } from './tree_builder';
const props = defineProps<{ item: Item; root?: boolean }>();
-const status = localStorage.getItem(`collapse-${props.item.id}`);
-const show = ref(status ? status === 'true' : true);
+
const toggleShow = () => {
- show.value = !show.value;
- localStorage.setItem(`collapse-${props.item.id}`, show.value.toString());
+ props.item.show.value = !props.item.show.value;
+ localStorage.setItem(`collapse-${props.item.id}`, props.item.show.value.toString());
};
</script>
diff --git a/frontend/components/Sidebar/Sidebar.vue b/frontend/components/Sidebar/Sidebar.vue
index fb01763d..3c9d9cdf 100644
--- a/frontend/components/Sidebar/Sidebar.vue
+++ b/frontend/components/Sidebar/Sidebar.vue
@@ -60,7 +60,7 @@ if (import.meta.client && useUserStore().user?.role === 2) {
}
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 navigation: Item[] = navigationItems.map(item => ({ id: item.id, parent_id: '', title: item.title, route: item.route, icon: item.icon, type: 'navigation', data: item, show: ref(true) }));
const categories: Item[] = categoriesStore.categories
.map((category: Category) => ({
id: category.id,
@@ -69,6 +69,7 @@ const items = computed((): Item[] => {
route: category.parent_id ? `/dashboard/categories/${category.id}` : '',
icon: category.icon,
data: category,
+ show: !category.parent_id && localStorage.getItem(`collapse-${category.id}`) === 'false' ? ref(false) : ref(true),
}))
.filter(c => c.data.name.toLowerCase().includes(filter.value.toLowerCase()));
const documents: Item[] = documentsStore.documents
@@ -78,6 +79,7 @@ const items = computed((): Item[] => {
title: document.name,
route: `/dashboard/docs/${document.id}`,
data: document,
+ show: ref(true),
}))
.filter(c => c.data.name.toLowerCase().includes(filter.value.toLowerCase()));
diff --git a/frontend/components/Sidebar/tree_builder.ts b/frontend/components/Sidebar/tree_builder.ts
index d86dc26a..770ac49a 100644
--- a/frontend/components/Sidebar/tree_builder.ts
+++ b/frontend/components/Sidebar/tree_builder.ts
@@ -8,6 +8,7 @@ export interface Item {
parent_id: string;
data: Document | Category | DefaultItem;
childrens?: Item[];
+ show: Ref<boolean>;
}
export class ItemsManager {