Rework sidebar pages

* Move sidebar pages from workspace to space level
* Replace array sorting with lexicographical fractional indexing
* Fixes and updates
This commit is contained in:
Philipinho
2024-04-16 21:55:24 +01:00
parent f1bdce1662
commit df9110268c
35 changed files with 775 additions and 982 deletions
@@ -0,0 +1,99 @@
import { IPage } from "@/features/page/types/page.types.ts";
import { SpaceTreeNode } from "@/features/page/tree/types.ts";
function sortPositionKeys(keys: any[]) {
return keys.sort((a, b) => {
if (a.position < b.position) return -1;
if (a.position > b.position) return 1;
return 0;
});
}
export function buildTree(pages: IPage[]): SpaceTreeNode[] {
const pageMap: Record<string, SpaceTreeNode> = {};
const tree: SpaceTreeNode[] = [];
pages.forEach((page) => {
pageMap[page.id] = {
id: page.id,
name: page.title,
icon: page.icon,
position: page.position,
hasChildren: page.hasChildren,
spaceId: page.spaceId,
children: [],
};
});
pages.forEach((page) => {
tree.push(pageMap[page.id]);
});
return sortPositionKeys(tree);
}
export function findBreadcrumbPath(
tree: SpaceTreeNode[],
pageId: string,
path: SpaceTreeNode[] = [],
): SpaceTreeNode[] | null {
for (const node of tree) {
if (!node.name || node.name.trim() === "") {
node.name = "untitled";
}
if (node.id === pageId) {
return [...path, node];
}
if (node.children) {
const newPath = findBreadcrumbPath(node.children, pageId, [
...path,
node,
]);
if (newPath) {
return newPath;
}
}
}
return null;
}
export const updateTreeNodeName = (
nodes: SpaceTreeNode[],
nodeId: string,
newName: string,
): SpaceTreeNode[] => {
return nodes.map((node) => {
if (node.id === nodeId) {
return { ...node, name: newName };
}
if (node.children && node.children.length > 0) {
return {
...node,
children: updateTreeNodeName(node.children, nodeId, newName),
};
}
return node;
});
};
export const updateTreeNodeIcon = (
nodes: SpaceTreeNode[],
nodeId: string,
newIcon: string,
): SpaceTreeNode[] => {
return nodes.map((node) => {
if (node.id === nodeId) {
return { ...node, icon: newIcon };
}
if (node.children && node.children.length > 0) {
return {
...node,
children: updateTreeNodeIcon(node.children, nodeId, newIcon),
};
}
return node;
});
};