All files / src/pages/flowsheet-page/flowsheet/Canvas/streamTracking trackedEdges.ts

75% Statements 6/8
91.66% Branches 11/12
100% Functions 0/0
71.42% Lines 5/7

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                                                                              5079x 5079x                                     5079x   5079x                                             5079x                                      
import type { Edge } from "@xyflow/react";
import type { CSSProperties } from "react";
import {
  trackedEdgeClassName,
  trackedEdgeCssVariables,
  trackedEdgeStrokeWidth,
  trackedHaloEdgeClassName,
  trackedHaloEdgeStyle,
} from "./edgeStyle";
import { trackedEdgeOpacity } from "./renderTrackingStyles";
import type { TrackedStreamEdgeFlow } from "./types";
 
type StreamPropertyEdgeStyle = {
  stroke?: string;
  strokeDasharray?: string;
  strokeLinecap?: CSSProperties["strokeLinecap"];
};
 
type StreamConnectionEdgeData = Record<string, unknown> & {
  isEnergyStream: boolean;
};
 
type BuildTrackedStreamConnectionEdgesArgs = {
  edgeId: string;
  source: string;
  target: string;
  sourceHandle: string;
  targetHandle: string;
  isRecycle: boolean;
  streamId: number;
  propertyStyle?: StreamPropertyEdgeStyle;
  trackedFlows: TrackedStreamEdgeFlow[];
  trackedStreamIds?: Set<number>;
  data: StreamConnectionEdgeData;
  reconnectable?: Edge["reconnectable"];
  reconnectableClassName?: string;
};
 
function joinClassNames(...classNames: Array<string | undefined>) {
  const className = classNames.filter(Boolean).join(" ");
  return className.length ? className : undefined;
}
 
export function buildTrackedStreamConnectionEdges({
  edgeId,
  source,
  target,
  sourceHandle,
  targetHandle,
  isRecycle,
  streamId,
  propertyStyle,
  trackedFlows,
  trackedStreamIds,
  data,
  reconnectable = false,
  reconnectableClassName,
}: BuildTrackedStreamConnectionEdgesArgs): Edge[] {
  const strokeDasharray =
    propertyStyle?.strokeDasharray ?? (isRecycle ? "5,5" : undefined);
 
  const streamEdge: Edge = {
    id: edgeId,
    source,
    target,
    sourceHandle,
    targetHandle,
    type: "step",
    className: joinClassNames(
      trackedEdgeClassName(trackedFlows),
      reconnectable ? reconnectableClassName : undefined,
    ),
    reconnectable,
    style: {
      ...trackedEdgeCssVariables(trackedFlows),
      stroke: propertyStyle?.stroke ?? "#000000",
      strokeWidth: trackedEdgeStrokeWidth(trackedFlows, isRecycle),
      strokeDasharray,
      strokeLinecap: propertyStyle?.strokeLinecap,
      opacity: trackedEdgeOpacity(streamId, trackedStreamIds),
    },
    data,
  };
 
  if (!trackedFlows.length) return [streamEdge];
 
  const haloEdge: Edge = {
    id: `${edgeId}-tracking-halo`,
    source,
    target,
    sourceHandle,
    targetHandle,
    type: "step",
    className: trackedHaloEdgeClassName(trackedFlows),
    selectable: false,
    focusable: false,
    deletable: false,
    style: trackedHaloEdgeStyle(trackedFlows),
    data,
  };
 
  return [haloEdge, streamEdge];
}