Skip to content

Commit

Permalink
feat/fix: fix #13 & add edge manipulation and deletion (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: t11s <greenfeatherhelp@gmail.com>
  • Loading branch information
Christofon and transmissions11 authored Apr 1, 2023
1 parent efa0c94 commit 99efb75
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useState, useCallback, useRef } from "react";

import ReactFlow, {
addEdge,
Expand All @@ -12,6 +12,7 @@ import ReactFlow, {
ReactFlowInstance,
ReactFlowJsonObject,
useReactFlow,
updateEdge,
} from "reactflow";

import "reactflow/dist/style.css";
Expand Down Expand Up @@ -45,7 +46,6 @@ import {
newFluxNode,
appendTextToFluxNodeAsGPT,
getFluxNodeLineage,
isFluxNodeInLineage,
addFluxNode,
modifyFluxNodeText,
modifyReactFlowNodeProperties,
Expand All @@ -57,6 +57,7 @@ import {
deleteSelectedFluxNodes,
addUserNodeLinkedToASystemNode,
markFluxNodeAsDoneGenerating,
getConnectionAllowed,
} from "../utils/fluxNode";
import {
FluxNodeData,
Expand Down Expand Up @@ -166,13 +167,38 @@ function App() {
const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);

const edgeUpdateSuccessful = useRef(true);

const onEdgeUpdateStart = useCallback(() => {
edgeUpdateSuccessful.current = false;
}, []);

const onEdgeUpdate = (oldEdge: Edge<any>, newConnection: Connection) => {
if (
!getConnectionAllowed(nodes, edges, {
source: newConnection.source!,
target: newConnection.target!,
})
)
return;

edgeUpdateSuccessful.current = true;
setEdges((edges) => updateEdge(oldEdge, newConnection, edges));
};

const onEdgeUpdateEnd = useCallback((_: unknown, edge: Edge<any>) => {
if (!edgeUpdateSuccessful.current) {
setEdges((edges) => edges.filter((e) => e.id !== edge.id));
}

edgeUpdateSuccessful.current = true;
}, []);

const onConnect = (connection: Edge<any> | Connection) => {
// Check the lineage of the source node to make
// sure we aren't creating a recursive connection.
if (
isFluxNodeInLineage(nodes, edges, {
nodeToCheck: connection.target!,
nodeToGetLineageOf: connection.source!,
!getConnectionAllowed(nodes, edges, {
source: connection.source!,
target: connection.target!,
})
)
return;
Expand Down Expand Up @@ -945,6 +971,9 @@ function App() {
onEdgesChange={onEdgesChange}
onEdgesDelete={takeSnapshot}
onNodesDelete={takeSnapshot}
onEdgeUpdateStart={onEdgeUpdateStart}
onEdgeUpdate={onEdgeUpdate}
onEdgeUpdateEnd={onEdgeUpdateEnd}
onConnect={onConnect}
nodeTypes={REACT_FLOW_NODE_TYPES}
// Causes clicks to also trigger auto zoom.
Expand Down
16 changes: 16 additions & 0 deletions src/utils/fluxNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ export function isFluxNodeInLineage(
return lineage.some((node) => node.id === nodeToCheck);
}

export function getConnectionAllowed(
existingNodes: Node<FluxNodeData>[],
existingEdges: Edge[],
{ source, target }: { source: string; target: string }
): boolean {
return (
// Check the lineage of the source node to make
// sure we aren't creating a recursive connection.
!isFluxNodeInLineage(existingNodes, existingEdges, {
nodeToCheck: target,
nodeToGetLineageOf: source,
// Check if the target node already has a parent.
}) && getFluxNodeParent(existingNodes, existingEdges, target) === undefined
);
}

/*//////////////////////////////////////////////////////////////
RENDERERS
//////////////////////////////////////////////////////////////*/
Expand Down

1 comment on commit 99efb75

@vercel
Copy link

@vercel vercel bot commented on 99efb75 Apr 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.