Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix notebooks tree (#7941)

01zulfi5c80b5300e3e
diff --git a/apps/web/src/components/virtualized-tree/index.tsx b/apps/web/src/components/virtualized-tree/index.tsx
index 5295088ea..fccdb8edd 100644
--- a/apps/web/src/components/virtualized-tree/index.tsx
+++ b/apps/web/src/components/virtualized-tree/index.tsx
@@ -30,7 +30,12 @@ import { CustomScrollbarsVirtualList, waitForElement } from "../list-container";
export type VirtualizedTreeHandle<T> = {
refresh: () => void;
- refreshItem: (index: number, item?: T) => void;
+ resetAndRefresh: () => void;
+ refreshItem: (
+ index: number,
+ item?: T,
+ itemOptions?: { expand?: boolean }
+ ) => void;
};
export type TreeNode<T = any> = {
id: string;
@@ -95,7 +100,6 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
treeRef,
() => ({
async refresh() {
- setNodes([]);
const { children } = await fetchChildren(
rootId,
-1,
@@ -105,7 +109,11 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
setNodes(children);
setExpandedIds(expandedIds);
},
- async refreshItem(index, item) {
+ async resetAndRefresh() {
+ setNodes([]);
+ await this.refresh();
+ },
+ async refreshItem(index, item, itemOptions) {
const node = nodes[index];
const removeIds: string[] = [node.id];
for (const treeNode of nodes) {
@@ -120,8 +128,8 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
return;
}
- // TODO: double check
- if (node.hasChildren) {
+ const childNodes = await getChildNodes(node.id, node.depth);
+ if (childNodes.length > 0 && itemOptions?.expand) {
expandedIds[node.id] = true;
}
@@ -131,6 +139,9 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
expandedIds,
getChildNodes
);
+
+ const hasChildren = children.length > 0;
+
filtered.splice(
index,
0,
@@ -138,7 +149,7 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
id: node.id,
data: item,
depth: node.depth,
- hasChildren: children.length > 0,
+ hasChildren: hasChildren,
parentId: node.parentId
},
...children
diff --git a/apps/web/src/dialogs/move-note-dialog.tsx b/apps/web/src/dialogs/move-note-dialog.tsx
index ea8187948..c7dbe12e0 100644
--- a/apps/web/src/dialogs/move-note-dialog.tsx
+++ b/apps/web/src/dialogs/move-note-dialog.tsx
@@ -264,7 +264,9 @@ export const MoveNoteDialog = DialogManager.register(function MoveNoteDialog({
isExpanded={expanded}
toggle={expanded ? collapse : expand}
onCreateItem={() => {
- treeRef.current?.refreshItem(index, item.data);
+ treeRef.current?.refreshItem(index, item.data, {
+ expand: true
+ });
}}
/>
)}
diff --git a/apps/web/src/views/notebooks.tsx b/apps/web/src/views/notebooks.tsx
index 271967396..34c602d6a 100644
--- a/apps/web/src/views/notebooks.tsx
+++ b/apps/web/src/views/notebooks.tsx
@@ -65,9 +65,13 @@ export function Notebooks() {
store.get().refresh();
}, []);
+ useEffect(() => {
+ treeRef.current?.resetAndRefresh();
+ }, [filteredNotebooks]);
+
useEffect(() => {
treeRef.current?.refresh();
- }, [notebooks]);
+ }, [roots]);
return (
<>
@@ -158,7 +162,8 @@ export function Notebooks() {
.count();
treeRef.current?.refreshItem(
index,
- notebook ? { notebook, totalNotes } : undefined
+ notebook ? { notebook, totalNotes } : undefined,
+ { expand: true }
);
}}
collapse={collapse}