All files / src/components/Playbooks/create CreateFlow.tsx

0% Statements 0/26
0% Branches 0/4
0% Functions 0/2
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
/* eslint-disable react-hooks/exhaustive-deps */
import ReactFlow, {
  Background,
  Controls,
  useNodesState,
  useEdgesState,
  BackgroundVariant,
} from "reactflow";
import "reactflow/dist/style.css";
import { useEffect } from "react";
import { useReactFlow } from "reactflow";
import CustomEdge from "./CustomEdge.js";
import StepNode from "./nodes/StepNode.tsx";
import handleEdgesDelete from "./utils/handleEdgesDelete.ts";
import handleConnection from "./utils/handleConnection.ts";
import useDimensions from "../../../hooks/playbooks/useDimensions.ts";
import useGraphDimensions from "../../../hooks/playbooks/useGraphDimensions.ts";
 
const fitViewOptions = {
  maxZoom: 0.75,
  duration: 500,
};
 
const nodeTypes = {
  step: StepNode,
};
 
const edgeTypes = {
  custom: CustomEdge,
};
 
const CreateFlow = () => {
  const reactFlowInstance = useReactFlow();
  const [graphRef, { width, height }] = useDimensions();
  const { graphData, dagreData } = useGraphDimensions(
    width,
    height,
    reactFlowInstance,
  );
  const [nodes, setNodes, onNodesChange] = useNodesState([]);
  const [edges, setEdges, onEdgesChange] = useEdgesState(graphData.edges ?? []);
 
  useEffect(() => {
    Iif (dagreData?.nodes?.length > 0) {
      setNodes(dagreData?.nodes);
    }
    Iif (dagreData?.edges?.length > 0) {
      setEdges(dagreData.edges);
    }
  }, [dagreData]);
 
  return (
    <div ref={graphRef} className="h-full w-full">
      <ReactFlow
        nodes={nodes}
        edges={edges}
        onNodesChange={onNodesChange}
        onEdgesChange={onEdgesChange}
        onEdgesDelete={handleEdgesDelete}
        onConnect={handleConnection}
        nodeTypes={nodeTypes}
        edgeTypes={edgeTypes as any}
        minZoom={-Infinity}
        maxZoom={1}
        zoomOnScroll={true}
        zoomOnPinch={true}
        fitView
        fitViewOptions={fitViewOptions}
        edgesUpdatable={false}
        className="bg-gray-50">
        <Controls />
        <Background variant={BackgroundVariant.Dots} gap={12} size={1} />
      </ReactFlow>
    </div>
  );
};
 
export default CreateFlow;