|
| 1 | +import React from "react"; |
| 2 | +import { RegionsIcon as Icon4 } from "@patternfly/react-icons"; |
| 3 | +import { DataSourceIcon as Icon1 } from "@patternfly/react-icons"; |
| 4 | +import { DataSinkIcon as Icon2 } from "@patternfly/react-icons"; |
| 5 | +import { DataProcessorIcon as Icon3 } from "@patternfly/react-icons"; |
| 6 | +import { |
| 7 | + action, |
| 8 | + BreadthFirstLayout, |
| 9 | + ColaLayout, |
| 10 | + ConcentricLayout, |
| 11 | + createTopologyControlButtons, |
| 12 | + DagreLayout, |
| 13 | + defaultControlButtonsOptions, |
| 14 | + DefaultEdge, |
| 15 | + DefaultGroup, |
| 16 | + DefaultNode, |
| 17 | + Edge, |
| 18 | + EdgeTerminalType, |
| 19 | + ForceLayout, |
| 20 | + GraphComponent, |
| 21 | + GridLayout, |
| 22 | + ModelKind, |
| 23 | + SELECTION_EVENT, |
| 24 | + TopologyControlBar, |
| 25 | + TopologySideBar, |
| 26 | + TopologyView as PFTopologyView, |
| 27 | + Visualization, |
| 28 | + VisualizationProvider, |
| 29 | + VisualizationSurface, |
| 30 | + withPanZoom, |
| 31 | + withSelection, |
| 32 | + WithSelectionProps, |
| 33 | + ComponentFactory, |
| 34 | + Graph, |
| 35 | + Layout, |
| 36 | + LayoutFactory, |
| 37 | + Model, |
| 38 | + Node, |
| 39 | + EdgeModel, |
| 40 | + NodeModel, |
| 41 | +} from "@patternfly/react-topology"; |
| 42 | + |
| 43 | +import NodeInformation from "./NodeSideBarDetails"; |
| 44 | + |
| 45 | +export interface TopologyViewProps { |
| 46 | + layout: string; |
| 47 | + nodes: NodeModel[]; |
| 48 | + edges: EdgeModel[]; |
| 49 | + truncateLength?: number; |
| 50 | +} |
| 51 | + |
| 52 | +interface CustomNodeProps { |
| 53 | + element: Node; |
| 54 | +} |
| 55 | +interface DataEdgeProps { |
| 56 | + element: Edge; |
| 57 | +} |
| 58 | + |
| 59 | +const DataEdge: React.FC<DataEdgeProps> = ({ element, ...rest }) => ( |
| 60 | + <DefaultEdge |
| 61 | + element={element} |
| 62 | + startTerminalType={EdgeTerminalType.cross} |
| 63 | + endTerminalType={EdgeTerminalType.directionalAlt} |
| 64 | + {...rest} |
| 65 | + /> |
| 66 | +); |
| 67 | + |
| 68 | +const CustomNode: React.FC<CustomNodeProps & WithSelectionProps> = ({ |
| 69 | + element, |
| 70 | + onSelect, |
| 71 | + selected, |
| 72 | + ...rest |
| 73 | +}) => { |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 75 | + const data = element.getData(); |
| 76 | + let Icon; |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 78 | + if (data.type == "Source") { |
| 79 | + Icon = Icon1; |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 81 | + } else if (data.type == "Sink") { |
| 82 | + Icon = Icon2; |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 84 | + } else if (data.type == "Processor") { |
| 85 | + Icon = Icon3; |
| 86 | + } else { |
| 87 | + Icon = Icon4; |
| 88 | + } |
| 89 | + |
| 90 | + return ( |
| 91 | + <DefaultNode |
| 92 | + element={element} |
| 93 | + showStatusDecorator |
| 94 | + {...rest} |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access |
| 96 | + truncateLength={data.length} |
| 97 | + onSelect={onSelect} |
| 98 | + selected={selected} |
| 99 | + > |
| 100 | + <g transform={`translate(25, 25)`}> |
| 101 | + <Icon style={{ color: "#393F44" }} width={25} height={25} /> |
| 102 | + </g> |
| 103 | + </DefaultNode> |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +const customLayoutFactory: LayoutFactory = ( |
| 108 | + type: string, |
| 109 | + graph: Graph |
| 110 | +): Layout | undefined => { |
| 111 | + switch (type) { |
| 112 | + case "Cola": |
| 113 | + return new ColaLayout(graph, { |
| 114 | + layoutOnDrag: false, |
| 115 | + linkDistance: 10, |
| 116 | + collideDistance: 30, |
| 117 | + maxTicks: 1, |
| 118 | + groupDistance: 150, |
| 119 | + }); |
| 120 | + case "Dagre_network-simplex": |
| 121 | + return new DagreLayout(graph, { |
| 122 | + rankdir: "LR", |
| 123 | + nodesep: 10, |
| 124 | + linkDistance: 5, |
| 125 | + edgesep: 2, |
| 126 | + groupDistance: 150, |
| 127 | + ranker: "network-simplex", |
| 128 | + }); |
| 129 | + case "Dagre_tight-tree": |
| 130 | + return new DagreLayout(graph, { |
| 131 | + rankdir: "LR", |
| 132 | + nodesep: 10, |
| 133 | + linkDistance: 5, |
| 134 | + edgesep: 2, |
| 135 | + groupDistance: 150, |
| 136 | + ranker: "tight-tree", |
| 137 | + }); |
| 138 | + case "BreadthFirst": |
| 139 | + return new BreadthFirstLayout(graph, { |
| 140 | + nodeDistance: 80, |
| 141 | + }); |
| 142 | + case "Concentric": |
| 143 | + return new ConcentricLayout(graph, { |
| 144 | + groupDistance: 150, |
| 145 | + }); |
| 146 | + case "Grid": |
| 147 | + return new GridLayout(graph, { |
| 148 | + groupDistance: 150, |
| 149 | + nodeDistance: 75, |
| 150 | + }); |
| 151 | + default: |
| 152 | + return new ForceLayout(graph, { |
| 153 | + groupDistance: 250, |
| 154 | + }); |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +const customComponentFactory: ComponentFactory = ( |
| 159 | + kind: ModelKind, |
| 160 | + type: string |
| 161 | +): any => { |
| 162 | + switch (type) { |
| 163 | + case "group": |
| 164 | + return DefaultGroup; |
| 165 | + case "data-edge": |
| 166 | + return DataEdge; |
| 167 | + default: |
| 168 | + switch (kind) { |
| 169 | + case ModelKind.graph: |
| 170 | + return withPanZoom()(GraphComponent); |
| 171 | + case ModelKind.node: |
| 172 | + return withSelection()(CustomNode as React.FC); |
| 173 | + case ModelKind.edge: |
| 174 | + return DefaultEdge; |
| 175 | + default: |
| 176 | + return undefined; |
| 177 | + } |
| 178 | + } |
| 179 | +}; |
| 180 | + |
| 181 | +const TopologyView = (props: TopologyViewProps): JSX.Element => { |
| 182 | + const { layout, nodes, edges, truncateLength } = props; |
| 183 | + |
| 184 | + const [selectedIds, setSelectedIds] = React.useState<string[]>([]); |
| 185 | + |
| 186 | + const controller = React.useMemo(() => { |
| 187 | + nodes.map((node) => { |
| 188 | + node.data && Object.assign(node?.data, { length: truncateLength }); |
| 189 | + }); |
| 190 | + |
| 191 | + const model: Model = { |
| 192 | + nodes: nodes, |
| 193 | + edges: edges, |
| 194 | + graph: { |
| 195 | + id: "g1", |
| 196 | + type: "graph", |
| 197 | + layout, |
| 198 | + }, |
| 199 | + }; |
| 200 | + |
| 201 | + const newController = new Visualization(); |
| 202 | + newController.setFitToScreenOnLayout(true); |
| 203 | + newController.registerLayoutFactory(customLayoutFactory); |
| 204 | + newController.registerComponentFactory(customComponentFactory); |
| 205 | + |
| 206 | + newController.addEventListener(SELECTION_EVENT, setSelectedIds); |
| 207 | + |
| 208 | + newController.fromModel(model, false); |
| 209 | + |
| 210 | + return newController; |
| 211 | + }, [edges, layout, nodes, truncateLength]); |
| 212 | + |
| 213 | + const node = nodes.find((node) => node.id === selectedIds[0]); |
| 214 | + |
| 215 | + const topologySideBar = ( |
| 216 | + <TopologySideBar |
| 217 | + className="topology-example-sidebar" |
| 218 | + show={selectedIds.length > 0} |
| 219 | + onClose={(): void => setSelectedIds([])} |
| 220 | + > |
| 221 | + <NodeInformation node={node} /> |
| 222 | + </TopologySideBar> |
| 223 | + ); |
| 224 | + |
| 225 | + return ( |
| 226 | + <PFTopologyView |
| 227 | + sideBar={topologySideBar} |
| 228 | + controlBar={ |
| 229 | + <TopologyControlBar |
| 230 | + controlButtons={createTopologyControlButtons({ |
| 231 | + ...defaultControlButtonsOptions, |
| 232 | + zoomInCallback: action(() => { |
| 233 | + controller.getGraph().scaleBy(4 / 3); |
| 234 | + }), |
| 235 | + zoomOutCallback: action(() => { |
| 236 | + controller.getGraph().scaleBy(0.75); |
| 237 | + }), |
| 238 | + fitToScreenCallback: action(() => { |
| 239 | + controller.getGraph().fit(80); |
| 240 | + }), |
| 241 | + resetViewCallback: action(() => { |
| 242 | + controller.getGraph().reset(); |
| 243 | + controller.getGraph().layout(); |
| 244 | + }), |
| 245 | + legend: false, |
| 246 | + })} |
| 247 | + /> |
| 248 | + } |
| 249 | + > |
| 250 | + <VisualizationProvider controller={controller}> |
| 251 | + <VisualizationSurface state={{ selectedIds }} /> |
| 252 | + </VisualizationProvider> |
| 253 | + </PFTopologyView> |
| 254 | + ); |
| 255 | +}; |
| 256 | + |
| 257 | +export default TopologyView; |
0 commit comments