MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / buildTree

Function buildTree

packages/app-core/src/components/Sidebar.tsx:3772–3851  ·  view source on GitHub ↗
(
  notes: NoteMeta[],
  assets: AssetMeta[],
  topFolder: NoteFolder,
  folders: FolderEntry[],
  vaultSettings: ReturnType<typeof useStore.getState>["vaultSettings"],
)

Source from the content-addressed store, hash-verified

3770}
3771
3772function buildTree(
3773 notes: NoteMeta[],
3774 assets: AssetMeta[],
3775 topFolder: NoteFolder,
3776 folders: FolderEntry[],
3777 vaultSettings: ReturnType<typeof useStore.getState>["vaultSettings"],
3778): TreeNode {
3779 const root: TreeNode = {
3780 name: topFolder,
3781 subpath: "",
3782 siblingOrder: -1,
3783 notes: [],
3784 assets: [],
3785 children: [],
3786 };
3787 const byPath = new Map<string, TreeNode>();
3788 byPath.set("", root);
3789 const folderOrder = new Map(
3790 folders.map((folder) => [folder.subpath, folder.siblingOrder] as const),
3791 );
3792 const symlinkBySubpath = new Map(
3793 folders.map((folder) => [folder.subpath, folder.isSymlink ?? false] as const),
3794 );
3795
3796 const ensureFolder = (subpath: string): TreeNode => {
3797 const existing = byPath.get(subpath);
3798 if (existing) return existing;
3799 const segments = subpath.split("/");
3800 let parent = root;
3801 let acc = "";
3802 for (const seg of segments) {
3803 acc = acc ? `${acc}/${seg}` : seg;
3804 let node = byPath.get(acc);
3805 if (!node) {
3806 node = {
3807 name: seg,
3808 subpath: acc,
3809 siblingOrder: folderOrder.get(acc) ?? Number.MAX_SAFE_INTEGER,
3810 notes: [],
3811 assets: [],
3812 children: [],
3813 isSymlink: symlinkBySubpath.get(acc) ?? false,
3814 };
3815 byPath.set(acc, node);
3816 parent.children.push(node);
3817 }
3818 parent = node;
3819 }
3820 return parent;
3821 };
3822
3823 // First pass: create nodes for every folder on disk (this is what
3824 // keeps empty folders visible in the tree).
3825 for (const f of folders) {
3826 if (!f.subpath) continue;
3827 ensureFolder(f.subpath);
3828 }
3829

Callers 1

SidebarFunction · 0.85

Calls 3

noteFolderSubpathFunction · 0.90
assetFolderSubpathFunction · 0.90
ensureFolderFunction · 0.85

Tested by

no test coverage detected