All files / src/UI/Canvas NodesArc.tsx

76.67% Statements 46/60
50% Branches 20/40
75% Functions 6/8
74.51% Lines 38/51

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                              7x 7x               7x 7x     18x 9x             18x 9x                                                       7x 51x 51x 51x 51x   51x 51x 51x           51x       51x   11x 11x 11x 9x 9x       9x   9x                                 9x 9x 9x 9x 9x 9x           9x           2x           51x   11x 11x 11x           51x                                  
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import { Arc, IArcProps } from './Arc';
import React, {
  FunctionComponent,
  useCallback,
  useEffect,
  useState,
} from 'react';
 
import { Coords } from './models';
import { useCanvas } from './CanvasContext';
import { useNodeRect } from './NodeRefProvider';
 
function leftCoords(rect: DOMRect) {
  const { left, y } = rect;
  return {
    x: left,
    y: y,
  };
}
 
function rightCoords(rect: DOMRect) {
  const { right, y } = rect;
  return {
    x: right,
    y: y,
  };
}
 
function topCoords(rect: DOMRect) {
  const { x, top } = rect;
  return {
    x: x,
    y: top,
  };
}
 
function bottomCoords(rect: DOMRect) {
  const { x, bottom } = rect;
  return {
    x: x,
    y: bottom,
  };
}
 
export interface INodesArcProps
  extends Omit<Omit<Omit<IArcProps, 'start'>, 'end'>, 'type'> {
  start: string;
  end: string;
}
 
export const NodesArc: FunctionComponent<INodesArcProps> = ({
  start: startId,
  end: endId,
  color,
  ...props
}) => {
  const { addRedrawListener, removeRedrawListener } = useCanvas();
  const getRect = useNodeRect();
  const [coords, setCoords] = useState<{
    start: Coords;
    end: Coords;
    startSideSize: number;
    endSideSize: number;
  } | null>(null);
  const [linkType, setLinkType] = useState<'horizontal' | 'vertical'>(
    'horizontal',
  );
 
  const calculateCoords = useCallback(
    function calculateCoordsCb() {
      const startRect = getRect(startId);
      const endRect = getRect(endId);
      if (startRect && endRect) {
        const [smallRect, bigRect] =
          startRect.width < endRect.width
            ? [startRect, endRect]
            : [endRect, startRect];
        const sameVerticalSpace =
          (smallRect.left > bigRect.left && smallRect.left < bigRect.right) ||
          (smallRect.right < bigRect.right && smallRect.right > bigRect.left);
        Iif (sameVerticalSpace) {
          const [aboveId, bottomId] =
            startRect.top > endRect.top ? [startId, endId] : [endId, startId];
          const start = getRect(aboveId);
          const end = getRect(bottomId);
          if (start && end) {
            setCoords({
              start: topCoords(start),
              end: bottomCoords(end),
              startSideSize: start.clipped ? 0 : start.width,
              endSideSize: end.clipped ? 0 : end.width,
            });
            setLinkType('vertical');
          } else {
            setCoords(null);
          }
        } else {
          const [leftId, rightId] =
            startRect.left > endRect.left ? [startId, endId] : [endId, startId];
          const start = getRect(leftId);
          const end = getRect(rightId);
          Eif (start && end) {
            setCoords({
              start: leftCoords(start),
              end: rightCoords(end),
              startSideSize: start.clipped ? 0 : start.height,
              endSideSize: end.clipped ? 0 : end.height,
            });
            setLinkType('horizontal');
          } else {
            setCoords(null);
          }
        }
      } else {
        setCoords(null);
      }
    },
    [endId, startId, getRect],
  );
 
  useEffect(
    function onNodeArcRedrawCb() {
      addRedrawListener(calculateCoords);
      return () => {
        removeRedrawListener(calculateCoords);
      };
    },
    [addRedrawListener, removeRedrawListener, calculateCoords],
  );
 
  return coords ? (
    <Arc
      start={coords.start}
      end={coords.end}
      type={linkType}
      startSideSize={coords.startSideSize}
      endSideSize={coords.endSideSize}
      color={
        color ||
        ((coords.startSideSize && coords.endSideSize) === 0
          ? 'var(--pf-global--Color--light-300)'
          : 'var(--pf-global--Color--dark-200)')
      }
      {...props}
    />
  ) : null;
};