Flow page refactored

This commit is contained in:
Lucas Oliveira 2023-02-19 22:25:54 -03:00
commit 549ccdf658

View file

@ -2,7 +2,6 @@ import { useCallback, useContext, useEffect, useRef, useState } from "react";
import ReactFlow, { import ReactFlow, {
Background, Background,
Controls, Controls,
Edge,
addEdge, addEdge,
useEdgesState, useEdgesState,
useNodesState, useNodesState,
@ -12,72 +11,79 @@ import ExtraSidebar from "./components/extraSidebarComponent";
import Chat from "../../components/chatComponent"; import Chat from "../../components/chatComponent";
import GenericNode from "../../CustomNodes/GenericNode"; import GenericNode from "../../CustomNodes/GenericNode";
import connection from "./components/connection"; import connection from "./components/connection";
import { getConnectedNodes } from "../../utils";
import ChatInputNode from "../../CustomNodes/ChatInputNode"; import ChatInputNode from "../../CustomNodes/ChatInputNode";
import ChatOutputNode from "../../CustomNodes/ChatOutputNode"; import ChatOutputNode from "../../CustomNodes/ChatOutputNode";
import InputNode from "../../CustomNodes/InputNode"; import InputNode from "../../CustomNodes/InputNode";
import BooleanNode from "../../CustomNodes/BooleanNode"; import BooleanNode from "../../CustomNodes/BooleanNode";
import { alertContext } from "../../contexts/alertContext"; import { alertContext } from "../../contexts/alertContext";
const nodeTypes = { const nodeTypes = {
genericNode:GenericNode, genericNode: GenericNode,
inputNode: InputNode, inputNode: InputNode,
chatInputNode:ChatInputNode, chatInputNode: ChatInputNode,
chatOutputNode:ChatOutputNode, chatOutputNode: ChatOutputNode,
booleanNode: BooleanNode, booleanNode: BooleanNode,
}; };
export default function FlowPage() { var _ = require("lodash");
export default function FlowPage() {
const reactFlowWrapper = useRef(null); const reactFlowWrapper = useRef(null);
let id = 0; const getId = () => `dndnode_${_.uniqueId()}`;
const getId = () => `dndnode_${id++}`;
const { setExtraComponent, setExtraNavigation } = useContext(locationContext); const { setExtraComponent, setExtraNavigation } = useContext(locationContext);
const {setErrorData} = useContext(alertContext); const { setErrorData } = useContext(alertContext);
useEffect(() => {
setExtraComponent(<ExtraSidebar />);
setExtraNavigation({title: "Nodes"})
}, [setExtraComponent, setExtraNavigation]);
const [nodes, setNodes, onNodesChange] = useNodesState([]); const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]); const [edges, setEdges, onEdgesChange] = useEdgesState([]);
const onEdgesChangeMod = useCallback((s) => {
onEdgesChange(s);
setNodes((x) => {
let newX = _.cloneDeep(x);
return newX;
})
}, [_, onEdgesChange, setNodes])
const [reactFlowInstance, setReactFlowInstance] = useState(null); const [reactFlowInstance, setReactFlowInstance] = useState(null);
var _ = require('lodash');
const onConnect = useCallback( useEffect(() => {
(params) => { setExtraComponent(<ExtraSidebar />);
/* console.log(params) setExtraNavigation({ title: "Nodes" });
console.log(reactFlowInstance.getNodes()) }, [setExtraComponent, setExtraNavigation]);
console.log(getConnectedNodes(params,reactFlowInstance.getNodes())) */
setEdges((eds) => addEdge({...params,className:"animate-pulse"}, eds)) const onEdgesChangeMod = useCallback(
(s) => {
onEdgesChange(s);
setNodes((x) => { setNodes((x) => {
let newX = _.cloneDeep(x); let newX = _.cloneDeep(x);
return newX; return newX;
}) });
}, },
[reactFlowInstance] [onEdgesChange, setNodes]
); );
const onConnect = useCallback(
(params) => {
setEdges((eds) =>
addEdge({ ...params, className: "animate-pulse" }, eds)
);
setNodes((x) => {
let newX = _.cloneDeep(x);
return newX;
});
},
[setEdges, setNodes]
);
const onDragOver = useCallback((event) => { const onDragOver = useCallback((event) => {
event.preventDefault(); event.preventDefault();
event.dataTransfer.dropEffect = "move"; event.dataTransfer.dropEffect = "move";
}, []); }, []);
const onDrop = useCallback( const onDrop = useCallback(
(event) => { (event) => {
event.preventDefault(); event.preventDefault();
const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect(); const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect();
let data = JSON.parse(event.dataTransfer.getData("json")); let data = JSON.parse(event.dataTransfer.getData("json"));
if(data.name !== 'chatInput' || (data.name === 'chatInput' && !reactFlowInstance.getNodes().some((n) => (n.type === 'chatInputNode')))){ if (
data.name !== "chatInput" ||
(data.name === "chatInput" &&
!reactFlowInstance.getNodes().some((n) => n.type === "chatInputNode"))
) {
const position = reactFlowInstance.project({ const position = reactFlowInstance.project({
x: event.clientX - reactflowBounds.left, x: event.clientX - reactflowBounds.left,
y: event.clientY - reactflowBounds.top, y: event.clientY - reactflowBounds.top,
@ -85,18 +91,39 @@ export default function FlowPage() {
let newId = getId(); let newId = getId();
const newNode = { const newNode = {
id: newId, id: newId,
type: data.name === 'str' ? 'inputNode' : (data.name === 'chatInput' ? 'chatInputNode' : (data.name === 'chatOutput' ? 'chatOutputNode' : (data.name === 'bool' ? 'booleanNode' : 'genericNode'))), type:
data.name === "str"
? "inputNode"
: data.name === "chatInput"
? "chatInputNode"
: data.name === "chatOutput"
? "chatOutputNode"
: data.name === "bool"
? "booleanNode"
: "genericNode",
position, position,
data: { ...data, id: newId, input: '', enabled: false, reactFlowInstance, onDelete: () => {setNodes(reactFlowInstance.getNodes().filter((n)=>n.id !== newId))} }, data: {
...data,
id: newId,
input: "",
enabled: false,
reactFlowInstance,
onDelete: () => {
setNodes(
reactFlowInstance.getNodes().filter((n) => n.id !== newId)
);
},
},
}; };
setNodes((nds) => nds.concat(newNode)); setNodes((nds) => nds.concat(newNode));
} else { } else {
setErrorData({title: 'Error creating node', list:["There can't be more than one chat input."]}) setErrorData({
title: "Error creating node",
list: ["There can't be more than one chat input."],
});
} }
}, },
[reactFlowInstance] [reactFlowInstance, setErrorData, setNodes]
); );
return ( return (
@ -116,7 +143,7 @@ export default function FlowPage() {
<Background /> <Background />
<Controls></Controls> <Controls></Controls>
</ReactFlow> </ReactFlow>
<Chat reactFlowInstance={reactFlowInstance}/> <Chat reactFlowInstance={reactFlowInstance} />
</div> </div>
); );
} }