-
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.
[FIXS NEEDED] Updates on Workspace management
* Workspace context not fetching workspace
- Loading branch information
1 parent
2aaeae0
commit e21b69a
Showing
19 changed files
with
248 additions
and
120 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { WorkspaceMetaData, WorkspaceResources } from '@notespace/shared/src/workspace/types/workspace.ts'; | ||
import { ResourceType, WorkspaceResourceMetadata } from '@notespace/shared/src/workspace/types/resource.ts'; | ||
|
||
export class WorkspaceTree{ | ||
private root: WorkspaceResourceMetadata | ||
|
||
private nodes : WorkspaceResources = new Map() | ||
|
||
constructor(meta : WorkspaceMetaData){ | ||
this.root = {...meta, type: ResourceType.DOCUMENT, parent: '', children: []} | ||
this.nodes.set(meta.id, this.root); | ||
} | ||
|
||
setNodes(_nodes: WorkspaceResources, root : WorkspaceResourceMetadata){ | ||
this.nodes = _nodes | ||
this.root = root | ||
} | ||
|
||
addNode(node: WorkspaceResourceMetadata){ | ||
const { parent } = node | ||
const parentNode = this.nodes.get(parent); | ||
if(parentNode) parentNode.children.push(node.id) | ||
this.nodes.set(node.id, node) | ||
} | ||
|
||
updateNode(id: string, props: Partial<WorkspaceResourceMetadata>){ | ||
const node = this.nodes.get(id) | ||
if(!node) throw new Error("Invalid id:" + id) | ||
Object.assign(node, props) | ||
} | ||
|
||
removeNode(id: string){ | ||
const node = this.nodes.get(id) | ||
if(!node) throw new Error("Invalid id:" + id) | ||
const { parent } = node; | ||
const parentNode = this.nodes.get(parent); | ||
if(parentNode){ | ||
const index = parentNode.children.indexOf(node.id); | ||
if(index !== -1) parentNode.children.splice(index, 1); | ||
this.nodes.delete(node.id) | ||
} | ||
} | ||
|
||
moveNode(id: string, newParent: string){ | ||
const node = this.nodes.get(id) | ||
if(!node) throw new Error("Invalid id:" + id) | ||
const { parent } = node; | ||
const parentNode = this.nodes.get(parent); | ||
|
||
if(parentNode){ | ||
const index = parentNode.children.indexOf(node.id); | ||
if(index !== -1) parentNode.children.splice(index, 1); | ||
} | ||
const newParentNode = this.nodes.get(newParent); | ||
if(!newParentNode) throw new Error("Invalid parent id:" + newParent) | ||
newParentNode.children.push(node.id) | ||
|
||
node.parent = newParent | ||
} | ||
|
||
*traverse() : IterableIterator<WorkspaceResourceMetadata>{ | ||
const stack: WorkspaceResourceMetadata[] = []; | ||
stack.push(this.root); | ||
|
||
while (stack.length > 0) { | ||
const node = stack.pop()!; | ||
yield node; | ||
stack.push(...node.children.map(id => this.nodes.get(id)!).reverse()); | ||
} | ||
} | ||
|
||
get resources() { | ||
return this.nodes | ||
} | ||
} |
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,7 @@ | ||
import { useMemo } from 'react'; | ||
import { WorkspaceTree } from '@domain/workspace/tree/WorkspaceTree.ts'; | ||
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace.ts'; | ||
|
||
export function useWorkspaceTree(meta : WorkspaceMetaData) { | ||
return useMemo(() => new WorkspaceTree(meta), [meta]); | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { HttpCommunication } from '@/services/communication/http/httpCommunication.ts'; | ||
import { | ||
ResourceInputModel, | ||
ResourceType, | ||
WorkspaceResource, | ||
} from '@notespace/shared/src/workspace/types/resource.ts'; | ||
|
||
function resourceService(http: HttpCommunication, wid: string) { | ||
async function getResource(id: string): Promise<WorkspaceResource> { | ||
return await http.get(`/workspaces/${wid}/${id}`); | ||
} | ||
|
||
async function createResource(name: string, type: ResourceType): Promise<string> { | ||
const resource: ResourceInputModel = { name, type }; | ||
const { id } = await http.post(`/workspaces/${wid}`, resource); | ||
return id; | ||
} | ||
|
||
async function deleteResource(id: string): Promise<void> { | ||
await http.delete(`/workspaces/${wid}/${id}`); | ||
} | ||
|
||
async function updateResource(id: string, newProps: Partial<ResourceInputModel>): Promise<void> { | ||
await http.put(`/workspaces/${wid}/${id}`, newProps); | ||
} | ||
|
||
return { | ||
getResource, | ||
createResource, | ||
deleteResource, | ||
updateResource, | ||
}; | ||
} | ||
|
||
export default resourceService; |
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,13 @@ | ||
import { useMemo } from 'react'; | ||
import { useCommunication } from '@/services/communication/context/useCommunication.ts'; | ||
import { useParams } from 'react-router-dom'; | ||
import resourceService from '@/services/resource/resourceService.ts'; | ||
|
||
function useResourceService() { | ||
const { http } = useCommunication(); | ||
const { wid } = useParams(); | ||
if (!wid) throw new Error('Cannot use document services outside of a workspace'); | ||
return useMemo(() => resourceService(http, wid), [http, wid]); | ||
} | ||
|
||
export default useResourceService; |
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 was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
code/client/src/ui/pages/workspace/hooks/useWorkspaceTreeOperations.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { WorkspaceResourceMetadata, } from '@notespace/shared/src/workspace/types/resource.ts'; | ||
import useSocketListeners from '@/services/communication/socket/useSocketListeners.ts'; | ||
import { ResourceType } from '@notespace/shared/src/workspace/types/resource.ts'; | ||
import { useCommunication } from '@/services/communication/context/useCommunication.ts'; | ||
import useResourceService from '@/services/resource/useResourceService.ts'; | ||
import { WorkspaceTree } from '@domain/workspace/tree/WorkspaceTree.ts'; | ||
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace.ts'; | ||
import useWorkspace from '@domain/workspace/useWorkspace.ts'; | ||
|
||
export function useWorkspaceTreeOperations() { | ||
const { workspace } = useWorkspace(); | ||
const tree = new WorkspaceTree(workspace as WorkspaceMetaData); | ||
const { socket } = useCommunication(); | ||
const services = useResourceService() | ||
|
||
async function createResource(type: ResourceType, title: string = '') { | ||
const id = await services.createResource(title, type); | ||
|
||
const resourceMeta: WorkspaceResourceMetadata = { | ||
id, | ||
name: title, | ||
type, | ||
parent: '', | ||
children:[] | ||
}; | ||
tree.addNode(resourceMeta); | ||
} | ||
|
||
async function deleteResource(id: string) { | ||
await services.deleteResource(id); | ||
tree.removeNode(id) | ||
} | ||
|
||
async function updateResource(id: string, props : Partial<WorkspaceResourceMetadata>) { | ||
await services.updateResource(id, props); | ||
tree.updateNode(id, props) | ||
} | ||
|
||
useSocketListeners(socket, { | ||
resourceCreated: tree.addNode, | ||
resourceDeleted: tree.removeNode, | ||
resourceUpdated: tree.updateNode, | ||
}); | ||
|
||
return { | ||
resources: Array.from(tree.traverse()), | ||
createResource, | ||
deleteResource, | ||
updateResource, | ||
}; | ||
} | ||
|
||
export default useWorkspaceTreeOperations; |
Oops, something went wrong.