Skip to content

Commit

Permalink
Project Refactoring & Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed May 22, 2024
1 parent 8a86046 commit 11ad395
Show file tree
Hide file tree
Showing 40 changed files with 338 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ export default (editor: Editor, handlers: MarkdownDomainOperations) => {
const deleteSelection = (deleteHandler: DeleteFunction, options?: TextDeleteOptions) => {
const selection = getSelection(editor);

console.log('Selection: ', selection);
console.log('Editor: ', editor.selection);

// Iterate over the selected lines and delete the block styles
for (let i = selection.start.line + 1; i <= selection.end.line; i++) {
const block = editor.children[i];
Expand Down
8 changes: 2 additions & 6 deletions code/client/src/domain/workspaces/tree/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { Resource } from '@notespace/shared/src/workspace/types/resource';

export type TreeNode = {
node: WorkspaceTreeNode;
node: Resource;
children: TreeNode[];
};

export type WorkspaceTreeNode = WorkspaceResource;

export type WorkspaceTreeNodes = Record<string, WorkspaceTreeNode>;
27 changes: 9 additions & 18 deletions code/client/src/domain/workspaces/tree/useWorkspaceTree.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { useState } from 'react';
import { rootNode } from '@domain/workspaces/tree/utils';
import { WorkspaceTreeNode, WorkspaceTreeNodes } from '@domain/workspaces/tree/types';
import { WorkspaceResources } from '@notespace/shared/src/workspace/types/workspace';
import { Resource } from '@notespace/shared/src/workspace/types/resource';
import { Resources } from '@ui/contexts/workspace/WorkspaceContext';

function useWorkspaceTree() {
const [nodes, setNodes] = useState<WorkspaceTreeNodes>({});
const [nodes, setNodes] = useState<Resources>({});

function setTree(nodes: WorkspaceResources) {
const wid = Object.values(nodes)[0].workspace;
const newNodes = { ...nodes };
newNodes[wid] = rootNode(newNodes[wid] ? newNodes[wid].children : []);
setNodes(newNodes);
function setTree(resources: Resource[]) {
const nodesRecord = Object.fromEntries(resources.map(node => [node.id, node]));
setNodes(nodesRecord);
}

const getNode = (id: string) => nodes[id];

function addNode(node: WorkspaceTreeNode) {
function addNode(node: Resource) {
const newNodes = { ...nodes };
const parentNode = newNodes[node.parent];
if (!parentNode) throw new Error('Invalid parent id: ' + node.parent);
Expand All @@ -26,19 +21,16 @@ function useWorkspaceTree() {

function removeNode(id: string) {
const node = nodes[id];

if (!node) throw new Error('Invalid id: ' + id);
const { parent } = node;
const parentNode = nodes[parent];

if (!parentNode) throw new Error('Invalid parent id: ' + parent);
const newNodes = { ...nodes };
const index = parentNode.children.indexOf(id);

if (index !== -1) parentNode.children.splice(index, 1);

delete newNodes[id];

newNodes[parent] = parentNode;
setNodes(newNodes);
}
Expand Down Expand Up @@ -71,12 +63,12 @@ function useWorkspaceTree() {
}

function isDescendant(parentId: string, nodeId: string): boolean {
let currentNode = getNode(parentId);
let currentNode = nodes[parentId];
while (currentNode) {
if (currentNode.id === nodeId) {
return true;
}
currentNode = getNode(currentNode.parent);
currentNode = nodes[currentNode.parent];
}
return false;
}
Expand All @@ -85,7 +77,6 @@ function useWorkspaceTree() {
nodes,
setNodes,
setTree,
getNode,
addNode,
updateNode,
removeNode,
Expand Down
23 changes: 6 additions & 17 deletions code/client/src/domain/workspaces/tree/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { TreeNode, WorkspaceTreeNode, WorkspaceTreeNodes } from '@domain/workspaces/tree/types';
import { TreeNode } from '@domain/workspaces/tree/types';
import { Resources } from '@ui/contexts/workspace/WorkspaceContext';

export function getTree(nodes: WorkspaceTreeNodes, id: string): TreeNode {
const root = nodes[id];
export function getTree(id: string, nodes: Resources): TreeNode {
const node = nodes[id];
return {
node: root,
children: root.children.map(id => getTree(nodes, id)),
};
}

export function rootNode(wid: string, children?: string[]): WorkspaceTreeNode {
return {
id: wid,
workspace: wid,
name: 'root',
parent: '',
children: children || [],
type: ResourceType.FOLDER,
node,
children: node.children.map(id => getTree(id, nodes)),
};
}
4 changes: 2 additions & 2 deletions code/client/src/services/resource/resourceService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HttpCommunication } from '@services/communication/http/httpCommunication';
import { ResourceInputModel, ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { ResourceInputModel, ResourceType, Resource } from '@notespace/shared/src/workspace/types/resource';

function resourceService(http: HttpCommunication, wid: string) {
async function getResource(id: string): Promise<WorkspaceResource> {
async function getResource(id: string): Promise<Resource> {
return await http.get(`/workspaces/${wid}/${id}`);
}

Expand Down
6 changes: 3 additions & 3 deletions code/client/src/services/workspace/workspaceService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HttpCommunication } from '@services/communication/http/httpCommunication';
import { WorkspaceInputModel, WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace';
import { WorkspaceInputModel, WorkspaceMeta } from '@notespace/shared/src/workspace/types/workspace';
import { Workspace } from '@notespace/shared/src/workspace/types/workspace';

function workspaceService(http: HttpCommunication) {
async function getWorkspace(id: string): Promise<Workspace> {
return await http.get(`/workspaces/${id}`);
}

async function getWorkspaces(): Promise<WorkspaceMetaData[]> {
async function getWorkspaces(): Promise<WorkspaceMeta[]> {
return await http.get('/workspaces');
}

Expand All @@ -19,7 +19,7 @@ function workspaceService(http: HttpCommunication) {
await http.delete(`/workspaces/${id}`);
}

async function updateWorkspace(id: string, newProps: Partial<WorkspaceMetaData>): Promise<void> {
async function updateWorkspace(id: string, newProps: Partial<WorkspaceMeta>): Promise<void> {
await http.put(`/workspaces/${id}`, newProps);
}

Expand Down
13 changes: 3 additions & 10 deletions code/client/src/ui/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import './Sidebar.scss';
import WorkspaceTree from '@ui/components/sidebar/components/WorkspaceTree';
import { FaHome } from 'react-icons/fa';
import { IoMdSettings } from 'react-icons/io';
import { useEffect } from 'react';

function Sidebar() {
const { isOpen, isLocked, isLoaded, handleClick, handleMouseEnter, handleMouseLeave } = useSidebarState();
const { workspace, nodes, operations } = useWorkspace();

useEffect(() => {
if (workspace) {
console.log('nodes', nodes);
}
}, [nodes, workspace]);
const { workspace, resources, operations } = useWorkspace();

if (!isLoaded) return null;
return (
Expand Down Expand Up @@ -57,12 +50,12 @@ function Sidebar() {
<Link to="/settings">Settings</Link>
</li>
<hr />
{workspace && operations && (
{workspace && operations && resources && (
<>
<h3>
<Link to={`/workspaces/${workspace.id}`}>{workspace.name}</Link>
</h3>
<WorkspaceTree workspace={workspace} nodes={nodes} operations={operations} />
<WorkspaceTree workspace={workspace} resources={resources} operations={operations} />
</>
)}
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Link } from 'react-router-dom';
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { Resource, ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { FaFile, FaFolder } from 'react-icons/fa6';
import { TreeNode, WorkspaceTreeNode } from '@domain/workspaces/tree/types';
import { TreeNode } from '@domain/workspaces/tree/types';
import React, { useState } from 'react';
import { RiArrowDownSFill, RiArrowRightSFill } from 'react-icons/ri';
import { FaPlusSquare } from 'react-icons/fa';
import CreateResourceContextMenu from '@ui/components/sidebar/components/CreateResourceContextMenu';

type ResourceViewProps = {
workspace: string;
resource: WorkspaceTreeNode;
resource: Resource;
onCreateResource?: (parent: string, type: ResourceType) => void;
onDrag?: (e: React.DragEvent<HTMLDivElement>) => void;
onDrop?: (e: React.DragEvent<HTMLDivElement>) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import ResourceView from '@ui/components/sidebar/components/ResourceView';
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace';
import { WorkspaceMeta } from '@notespace/shared/src/workspace/types/workspace';
import { getTree } from '@domain/workspaces/tree/utils';
import { WorkspaceTreeNodes } from '@domain/workspaces/tree/types';
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { DragEvent, useState } from 'react';
import { UseResourcesType } from '@ui/contexts/workspace/useResources';
import { Resources, WorkspaceOperations } from '@ui/contexts/workspace/WorkspaceContext';

type WorkspaceTreeProps = {
workspace: WorkspaceMetaData;
operations: Omit<UseResourcesType['operations'], 'setResources'>;
nodes?: WorkspaceTreeNodes;
workspace: WorkspaceMeta;
resources: Resources;
operations: WorkspaceOperations;
};

function WorkspaceTree({ workspace, nodes, operations }: WorkspaceTreeProps) {
function WorkspaceTree({ workspace, resources, operations }: WorkspaceTreeProps) {
const [dragId, setDragId] = useState<string | null>(null);

async function onCreateResource(parent: string, type: ResourceType) {
Expand All @@ -32,8 +31,8 @@ function WorkspaceTree({ workspace, nodes, operations }: WorkspaceTreeProps) {
return (
<div className="workspace-tree">
<ul>
{nodes &&
getTree(nodes, workspace.id).children.map(node => (
{resources[workspace.id] &&
getTree(workspace.id, resources).children.map(node => (
<li key={node.node.id}>
<ResourceView
workspace={workspace.id}
Expand Down
46 changes: 20 additions & 26 deletions code/client/src/ui/contexts/workspace/WorkspaceContext.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import * as React from 'react';
import { useState, createContext, useEffect } from 'react';
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace';
import { WorkspaceMeta } from '@notespace/shared/src/workspace/types/workspace';
import { useCommunication } from '@ui/contexts/communication/useCommunication';
import useError from '@ui/contexts/error/useError';
import { useParams } from 'react-router-dom';
import useWorkspaceService from '@services/workspace/useWorkspaceService';
import useResources, { UseResourcesType } from '@ui/contexts/workspace/useResources';
import { WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { WorkspaceTreeNodes } from '@domain/workspaces/tree/types';
import useResources from '@ui/contexts/workspace/useResources';
import { Resource, ResourceType } from '@notespace/shared/src/workspace/types/resource';

export type Resources = Record<string, Resource>;

export type WorkspaceOperations = {
createResource: (name: string, type: ResourceType, parent?: string) => Promise<void>;
deleteResource: (id: string) => Promise<void>;
updateResource: (id: string, newProps: Partial<Resource>) => Promise<void>;
moveResource: (id: string, parent: string) => Promise<void>;
};

export type WorkspaceContextType = {
workspace?: WorkspaceMetaData;
resources?: WorkspaceResource[];
operations?: Omit<UseResourcesType['operations'], 'setResources'>;
nodes?: WorkspaceTreeNodes;
workspace?: WorkspaceMeta;
resources?: Resources;
operations?: WorkspaceOperations;
};

export const WorkspaceContext = createContext<WorkspaceContextType>({});

export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
const services = useWorkspaceService();
const [workspace, setWorkspace] = useState<WorkspaceMetaData | undefined>(undefined);
const { resources, tree, operations } = useResources();
const [workspace, setWorkspace] = useState<WorkspaceMeta | undefined>(undefined);
const { resources, operations } = useResources();
const { setResources, ...otherOperations } = operations;
const { socket } = useCommunication();
const { publishError } = useError();
const { wid } = useParams();
Expand All @@ -31,9 +39,8 @@ export function WorkspaceProvider({ children }: { children: React.ReactNode }) {

async function fetchWorkspace() {
const { id, name, resources } = await services.getWorkspace(wid!);
console.log('fetchWorkspace', resources);
setWorkspace({ id, name });
operations.setResources(resources);
setResources(resources);
}
socket.emit('joinWorkspace', wid);
fetchWorkspace().catch(publishError);
Expand All @@ -43,21 +50,8 @@ export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [wid, services, socket, publishError]);

const newOperations: WorkspaceContextType['operations'] = {
createResource: operations.createResource,
deleteResource: operations.deleteResource,
updateResource: operations.updateResource,
moveResource: operations.moveResource,
};

useEffect(() => {
console.log('resources: ', resources);
}, [resources]);

return (
<WorkspaceContext.Provider
value={{ workspace, resources: Object.values(resources), operations: newOperations, nodes: tree.nodes }}
>
<WorkspaceContext.Provider value={{ workspace, resources, operations: otherOperations }}>
{children}
</WorkspaceContext.Provider>
);
Expand Down
Loading

0 comments on commit 11ad395

Please sign in to comment.