All files / src/hooks/playbooks useGraphDimensions.ts

0% Statements 0/20
0% Branches 0/3
0% Functions 0/2
0% Lines 0/20

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                                                                                                             
import { useEffect, useState } from "react";
import fetchGraphData from "../../utils/graph/fetchGraphData.ts";
import { useSelector } from "react-redux";
import { currentPlaybookSelector } from "../../store/features/playbook/playbookSlice.ts";
import { calculateData } from "../../utils/graph/calculateData.ts";
import { ReactFlowInstance } from "reactflow";
import usePermanentDrawerState from "../common/usePermanentDrawerState.ts";
 
const fitViewOptions = {
  maxZoom: 0.75,
  duration: 500,
};
 
type GraphDimensions = {
  graphData: any;
  dagreData: any;
};
 
function useGraphDimensions(
  width: number | undefined,
  height: number | undefined,
  instance: ReactFlowInstance<any, any>,
): GraphDimensions {
  const playbook = useSelector(currentPlaybookSelector);
  const { permanentView, isOpen } = usePermanentDrawerState();
  const graphData = fetchGraphData();
  const [data, setData] = useState<any>({});
 
  useEffect(() => {
    Iif (width && height) {
      const graphData = fetchGraphData();
      const dagreData = calculateData(graphData, width, height);
      setData(dagreData);
    }
    instance.fitView(fitViewOptions);
  }, [
    width,
    height,
    playbook?.steps,
    playbook?.step_relations,
    permanentView,
    instance,
    isOpen,
  ]);
 
  instance.fitView(fitViewOptions);
 
  return {
    graphData,
    dagreData: data,
  };
}
 
export default useGraphDimensions;