-
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
2e4ed9d
commit 6592390
Showing
35 changed files
with
287 additions
and
420 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
2 changes: 1 addition & 1 deletion
2
.../client/src/domain/error/ErrorContext.tsx → ...t/src/domain/error/hooks/ErrorContext.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
2 changes: 1 addition & 1 deletion
2
code/client/src/domain/error/useError.ts → ...client/src/domain/error/hooks/useError.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
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 { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource.ts'; | ||
import useResourceService from '@/services/resource/useResourceService.ts'; | ||
import useSocketListeners from '@/services/communication/socket/useSocketListeners.ts'; | ||
import { useCommunication } from '@/services/communication/context/useCommunication.ts'; | ||
import { useState } from 'react'; | ||
|
||
function useResources() { | ||
const service = useResourceService(); | ||
const [resources, setResources] = useState<WorkspaceResource[]>([]); | ||
const { socket } = useCommunication(); | ||
|
||
function onCreateResource(resource: WorkspaceResource) { | ||
setResources([...resources, resource]); | ||
} | ||
|
||
async function createResource(name: string, type: ResourceType) { | ||
await service.createResource(name, type); | ||
} | ||
|
||
function onDeleteResource(id: string) { | ||
setResources(resources.filter(resource => resource.id !== id)); | ||
} | ||
|
||
async function deleteResource(id: string) { | ||
await service.deleteResource(id); | ||
} | ||
|
||
function onUpdateResource(resource: Partial<WorkspaceResource>) { | ||
setResources(resources.map(res => (res.id === resource.id ? { ...res, ...resource } : res))); | ||
} | ||
|
||
async function updateResource(id: string, newProps: Partial<WorkspaceResource>) { | ||
await service.updateResource(id, newProps); | ||
} | ||
|
||
useSocketListeners(socket, { | ||
createdResource: onCreateResource, | ||
deletedResource: onDeleteResource, | ||
updatedResource: onUpdateResource, | ||
}); | ||
|
||
return { | ||
resources, | ||
setResources, | ||
operations: { | ||
createResource, | ||
deleteResource, | ||
updateResource, | ||
}, | ||
}; | ||
} | ||
|
||
export default useResources; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
49 changes: 49 additions & 0 deletions
49
code/client/src/domain/workspaces/hooks/WorkspaceContext.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as React from 'react'; | ||
import { useState, createContext, useEffect } from 'react'; | ||
import { Workspace, WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace.ts'; | ||
import { useCommunication } from '@/services/communication/context/useCommunication.ts'; | ||
import useError from '@domain/error/hooks/useError.ts'; | ||
import { useParams } from 'react-router-dom'; | ||
import useWorkspaceService from '@/services/workspace/useWorkspaceService.ts'; | ||
import useResources from '@domain/resources/hooks/useResources.ts'; | ||
import { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource.ts'; | ||
|
||
type ResourceOperationsType = { | ||
createResource: (name: string, type: ResourceType) => Promise<void>; | ||
deleteResource: (id: string) => Promise<void>; | ||
updateResource: (id: string, newProps: Partial<WorkspaceResource>) => Promise<void>; | ||
}; | ||
|
||
export type WorkspaceContextType = { | ||
workspace?: WorkspaceMetaData; | ||
resources?: WorkspaceResource[]; | ||
operations?: ResourceOperationsType; | ||
}; | ||
|
||
export const WorkspaceContext = createContext<WorkspaceContextType>({}); | ||
|
||
export function WorkspaceProvider({ children }: { children: React.ReactNode }) { | ||
const services = useWorkspaceService(); | ||
const [workspace, setWorkspace] = useState<WorkspaceMetaData | undefined>(undefined); | ||
const { resources, setResources, operations } = useResources(); | ||
const { socket } = useCommunication(); | ||
const { publishError } = useError(); | ||
const { wid } = useParams(); | ||
|
||
useEffect(() => { | ||
if (!wid) return; | ||
async function fetchWorkspace() { | ||
const { id, name, resources }: Workspace = await services.getWorkspace(wid!); | ||
setWorkspace({ id, name }); | ||
console.log(resources); | ||
setResources(resources); | ||
} | ||
socket.emit('joinWorkspace', wid); | ||
fetchWorkspace().catch(publishError); | ||
return () => { | ||
socket.emit('leaveWorkspace'); | ||
}; | ||
}, [wid, socket, publishError, services, setResources]); | ||
|
||
return <WorkspaceContext.Provider value={{ workspace, resources, operations }}>{children}</WorkspaceContext.Provider>; | ||
} |
2 changes: 1 addition & 1 deletion
2
...ient/src/domain/workspace/useWorkspace.ts → ...c/domain/workspaces/hooks/useWorkspace.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
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,8 @@ | ||
import { useMemo } from 'react'; | ||
import { WorkspaceTree } from '@domain/workspaces/tree/WorkspaceTree.ts'; | ||
|
||
function useWorkspaceTree() { | ||
return useMemo(() => new WorkspaceTree(), []); | ||
} | ||
|
||
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
Oops, something went wrong.