|
|
@@ -1,7 +1,7 @@
|
|
|
import React, { Component } from 'react';
|
|
|
import styled from 'styled-components';
|
|
|
|
|
|
-import { ResourceType, NodeType, EdgeType } from '../../../../../shared/types';
|
|
|
+import { ResourceType, NodeType, EdgeType, ChartType } from '../../../../../shared/types';
|
|
|
|
|
|
import Node from './Node';
|
|
|
import Edge from './Edge';
|
|
|
@@ -15,7 +15,10 @@ type PropsType = {
|
|
|
components: ResourceType[],
|
|
|
isExpanded: boolean,
|
|
|
setSidebar: (x: boolean) => void,
|
|
|
- currentChartName: string
|
|
|
+ currentChart: ChartType,
|
|
|
+
|
|
|
+ // Handle revisions expansion for YAML wrapper
|
|
|
+ showRevisions: boolean
|
|
|
};
|
|
|
|
|
|
type StateType = {
|
|
|
@@ -45,6 +48,7 @@ type StateType = {
|
|
|
openedNode: NodeType | null,
|
|
|
suppressCloseNode: boolean, // Still click should close opened unless on a node
|
|
|
suppressDisplay: boolean, // Ignore clicks + pan/zoom on InfoPanel or ButtonSection
|
|
|
+ version?: number // Track in localstorage for handling updates when unmounted
|
|
|
};
|
|
|
|
|
|
// TODO: region-based unselect, shift-click, multi-region
|
|
|
@@ -87,7 +91,7 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
|
- let { components } = this.props;
|
|
|
+ let { components, currentChart } = this.props;
|
|
|
|
|
|
// Initialize origin
|
|
|
let height = this.spaceRef.offsetHeight;
|
|
|
@@ -101,21 +105,39 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
this.spaceRef.addEventListener("touchmove", (e: any) => e.preventDefault());
|
|
|
this.spaceRef.addEventListener("mousewheel", (e: any) => e.preventDefault());
|
|
|
|
|
|
- let graph = localStorage.getItem(`charts.${this.props.currentChartName}`)
|
|
|
- let nodes = [] as NodeType[]
|
|
|
- let edges = [] as EdgeType[]
|
|
|
+ document.addEventListener("keydown", this.handleKeyDown);
|
|
|
+ document.addEventListener("keyup", this.handleKeyUp);
|
|
|
|
|
|
+ // Handle graph from localstorage
|
|
|
+ let graph = localStorage.getItem(`charts.${currentChart.name}-${currentChart.version}`);
|
|
|
+ let nodes = [] as NodeType[];
|
|
|
+ let edges = [] as EdgeType[];
|
|
|
if (!graph) {
|
|
|
- nodes = this.createNodes(components)
|
|
|
- edges = this.createEdges(components)
|
|
|
+ nodes = this.createNodes(components);
|
|
|
+ edges = this.createEdges(components);
|
|
|
this.setState({ nodes, edges });
|
|
|
} else {
|
|
|
- let storedState = JSON.parse(localStorage.getItem(`charts.${this.props.currentChartName}`))
|
|
|
+ let storedState = JSON.parse(localStorage.getItem(
|
|
|
+ `charts.${currentChart.name}-${currentChart.version}`
|
|
|
+ ));
|
|
|
this.setState(storedState);
|
|
|
}
|
|
|
|
|
|
- document.addEventListener("keydown", this.handleKeyDown);
|
|
|
- document.addEventListener("keyup", this.handleKeyUp);
|
|
|
+ window.onbeforeunload = () => {
|
|
|
+ this.storeChart();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Live update on rollback/upgrade
|
|
|
+ componentDidUpdate(prevProps: PropsType) {
|
|
|
+ if (prevProps.components !== this.props.components) {
|
|
|
+ let nodes = [] as NodeType[];
|
|
|
+ let edges = [] as EdgeType[];
|
|
|
+
|
|
|
+ nodes = this.createNodes(this.props.components);
|
|
|
+ edges = this.createEdges(this.props.components);
|
|
|
+ this.setState({ nodes, edges, openedNode: null });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
createNodes = (components: ResourceType[]) => {
|
|
|
@@ -142,7 +164,7 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
}
|
|
|
|
|
|
createEdges = (components: ResourceType[]) => {
|
|
|
- let edges = [] as EdgeType[]
|
|
|
+ let edges = [] as EdgeType[];
|
|
|
components.map((c: ResourceType) => {
|
|
|
c.Relations?.ControlRels?.map((rel: any) => {
|
|
|
if (rel.Source == c.ID) {
|
|
|
@@ -160,13 +182,14 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
}
|
|
|
})
|
|
|
});
|
|
|
- return edges
|
|
|
+ return edges;
|
|
|
}
|
|
|
|
|
|
- componentWillUnmount() {
|
|
|
- let graph = this.state;
|
|
|
+ storeChart = () => {
|
|
|
+ let { currentChart } = this.props;
|
|
|
+ let graph = JSON.parse(JSON.stringify(this.state));
|
|
|
|
|
|
- // flush non-persistent data
|
|
|
+ // Flush non-persistent data
|
|
|
graph.activeIds = [];
|
|
|
graph.currentNode = null;
|
|
|
graph.currentEdge = null;
|
|
|
@@ -175,7 +198,15 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
graph.suppressDisplay = false;
|
|
|
graph.suppressCloseNode = false;
|
|
|
|
|
|
- localStorage.setItem(`charts.${this.props.currentChartName}`, JSON.stringify(graph))
|
|
|
+ localStorage.setItem(
|
|
|
+ `charts.${currentChart.name}-${currentChart.version}`,
|
|
|
+ JSON.stringify(graph)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ componentWillUnmount() {
|
|
|
+ this.storeChart();
|
|
|
+
|
|
|
this.spaceRef.removeEventListener("touchmove", (e: any) => e.preventDefault());
|
|
|
this.spaceRef.removeEventListener("mousewheel", (e: any) => e.preventDefault());
|
|
|
document.removeEventListener("keydown", this.handleKeyDown);
|
|
|
@@ -475,6 +506,7 @@ export default class GraphDisplay extends Component<PropsType, StateType> {
|
|
|
|
|
|
// For YAML wrapper to trigger resize
|
|
|
isExpanded={this.state.isExpanded}
|
|
|
+ showRevisions={this.props.showRevisions}
|
|
|
/>
|
|
|
</StyledGraphDisplay>
|
|
|
);
|