Skip to content

Commit

Permalink
refactor node functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AykutSarac committed Aug 27, 2022
1 parent 31a57d4 commit e89cbf1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 66 deletions.
20 changes: 6 additions & 14 deletions src/hooks/store/useGraph.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import create from "zustand";
import { EdgeData, NodeData } from "reaflow/dist/types";
import { Graph } from "src/components/Graph";
import { findEdgeChildren } from "src/utils/findEdgeChildren";
import { findNodeChildren } from "src/utils/findNodeChildren";
import { getChildrenEdges } from "src/utils/getChildrenEdges";
import { getOutgoers } from "src/utils/getOutgoers";

export interface Graph {
nodes: NodeData[];
Expand Down Expand Up @@ -34,12 +34,8 @@ const useGraph = create<Graph & GraphActions>((set) => ({
}),
expandNodes: (nodeId) =>
set((state) => {
const childrenNodes = findNodeChildren(nodeId, state.nodes, state.edges);
const childrenEdges = findEdgeChildren(
nodeId,
childrenNodes,
state.edges
);
const childrenNodes = getOutgoers(nodeId, state.nodes, state.edges);
const childrenEdges = getChildrenEdges(childrenNodes, state.edges);

const nodeIds = childrenNodes.map((node) => node.id);
const edgeIds = childrenEdges.map((edge) => edge.id);
Expand All @@ -56,12 +52,8 @@ const useGraph = create<Graph & GraphActions>((set) => ({
}),
collapseNodes: (nodeId) =>
set((state) => {
const childrenNodes = findNodeChildren(nodeId, state.nodes, state.edges);
const childrenEdges = findEdgeChildren(
nodeId,
childrenNodes,
state.edges
);
const childrenNodes = getOutgoers(nodeId, state.nodes, state.edges);
const childrenEdges = getChildrenEdges(childrenNodes, state.edges);

const nodeIds = childrenNodes.map((node) => node.id);
const edgeIds = childrenEdges.map((edge) => edge.id);
Expand Down
17 changes: 0 additions & 17 deletions src/utils/findEdgeChildren.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/utils/findNodeChildren.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/utils/getChildrenEdges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NodeData, EdgeData } from "reaflow/dist/types";

export const getChildrenEdges = (
nodes: NodeData[],
edges: EdgeData[]
): EdgeData[] => {
const nodeIds = nodes.map((node) => node.id);

return edges.filter(
(edge) =>
nodeIds.includes(edge.from as string) ||
nodeIds.includes(edge.to as string)
);
};
20 changes: 20 additions & 0 deletions src/utils/getOutgoers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NodeData, EdgeData } from "reaflow/dist/types";

export const getOutgoers = (
nodeId: string,
nodes: NodeData[],
edges: EdgeData[]
): NodeData[] => {
const allOutgoers: NodeData[] = [];

const runner = (nodeId: string) => {
const outgoerIds = edges.filter((e) => e.from === nodeId).map((e) => e.to);
const nodeList = nodes.filter((n) => outgoerIds.includes(n.id));
allOutgoers.push(...nodeList);
nodeList.forEach((node) => runner(node.id));
};

runner(nodeId);

return allOutgoers;
};

0 comments on commit e89cbf1

Please sign in to comment.