-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a860e67
commit 494dcd3
Showing
14 changed files
with
200 additions
and
169 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { WorkspaceResourceMetadata } from '@notespace/shared/src/workspace/types/resource'; | ||
|
||
export type TreeNode = { | ||
node: WorkspaceTreeNode; | ||
children: TreeNode[]; | ||
}; | ||
|
||
export type WorkspaceTreeNode = WorkspaceResourceMetadata; | ||
|
||
export type WorkspaceTreeNodes = Map<string, WorkspaceTreeNode>; |
79 changes: 76 additions & 3 deletions
79
code/client/src/domain/workspaces/tree/useWorkspaceTree.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,81 @@ | ||
import { useMemo } from 'react'; | ||
import { WorkspaceTree } from '@domain/workspaces/tree/WorkspaceTree'; | ||
import { useState } from 'react'; | ||
import { rootNode } from '@domain/workspaces/tree/utils'; | ||
import { WorkspaceTreeNode } from '@domain/workspaces/tree/types'; | ||
|
||
function useWorkspaceTree() { | ||
return useMemo(() => new WorkspaceTree(), []); | ||
const [nodes, setNodes] = useState<Map<string, WorkspaceTreeNode>>(new Map()); | ||
|
||
function setTree(nodes: WorkspaceTreeNode[]) { | ||
const nodesMap = new Map(nodes.map(node => [node.id, node])); | ||
const root = rootNode( | ||
Array.from(nodes?.values() || []) | ||
.filter(node => node.parent === 'root') | ||
.map(node => node.id) | ||
); | ||
nodesMap.set('root', root); | ||
setNodes(nodesMap); | ||
} | ||
|
||
function addNode(node: WorkspaceTreeNode) { | ||
const newNodes = new Map(nodes); | ||
const parentNode = newNodes.get(node.parent); | ||
if (!parentNode) throw new Error('Invalid parent id: ' + node.parent); | ||
newNodes.set(node.id, node); | ||
newNodes.set(node.parent, { ...parentNode, children: [...parentNode.children, node.id] }); | ||
setNodes(newNodes); | ||
} | ||
|
||
function removeNode(id: string) { | ||
const node = nodes.get(id); | ||
if (!node) throw new Error('Invalid id: ' + id); | ||
const { parent } = node; | ||
const parentNode = nodes.get(parent); | ||
if (!parentNode) throw new Error('Invalid parent id: ' + parent); | ||
const newNodes = new Map(nodes); | ||
const index = parentNode.children.indexOf(id); | ||
if (index !== -1) parentNode.children.splice(index, 1); | ||
newNodes.delete(id); | ||
newNodes.set(parent, parentNode); | ||
setNodes(newNodes); | ||
} | ||
|
||
function updateNode(id: string, name: string) { | ||
const node = nodes.get(id); | ||
if (!node) throw new Error('Invalid id: ' + id); | ||
const newNode = { ...node, name }; | ||
nodes.set(id, newNode); | ||
setNodes(new Map(nodes)); | ||
} | ||
|
||
function moveNode(id: string, newParent: string) { | ||
const node = nodes.get(id); | ||
if (!node) throw new Error('Invalid id: ' + id); | ||
const { parent } = node; | ||
const parentNode = nodes.get(parent); | ||
|
||
if (parentNode) { | ||
const index = parentNode.children.indexOf(node.id); | ||
if (index !== -1) parentNode.children.splice(index, 1); | ||
nodes.set(parent, parentNode); | ||
} | ||
const newParentNode = nodes.get(newParent); | ||
if (!newParentNode) throw new Error('Invalid parent id: ' + newParent); | ||
newParentNode.children.push(node.id); | ||
node.parent = newParent; | ||
nodes.set(id, node); | ||
nodes.set(newParent, newParentNode); | ||
setNodes(new Map(nodes)); | ||
} | ||
|
||
return { | ||
nodes, | ||
setNodes, | ||
setTree, | ||
addNode, | ||
updateNode, | ||
removeNode, | ||
moveNode, | ||
}; | ||
} | ||
|
||
export default useWorkspaceTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ResourceType } from '@notespace/shared/src/workspace/types/resource'; | ||
import { TreeNode, WorkspaceTreeNode } from '@domain/workspaces/tree/types'; | ||
|
||
export function getTree(nodes: Map<string, WorkspaceTreeNode>, id: string = 'root'): TreeNode { | ||
const root = nodes.get(id)!; | ||
return { | ||
node: root, | ||
children: root.children.map(id => getTree(nodes, id)), | ||
}; | ||
} | ||
|
||
export function rootNode(children?: string[]): WorkspaceTreeNode { | ||
return { | ||
id: 'root', | ||
name: 'root', | ||
parent: '', | ||
children: children || [], | ||
type: ResourceType.FOLDER, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 8 additions & 7 deletions
15
code/client/src/ui/components/sidebar/components/WorkspaceTree.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.