generated from kubefirst/react-app-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: a couple of fields should have been updated with reset * set selected node when graph node is clicked on graph view/ open cluster details panel. disregard node layout stab - wip
- Loading branch information
Showing
4 changed files
with
172 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,184 @@ | ||
import React, { useCallback, type FunctionComponent } from 'react'; | ||
import React, { useCallback, type FunctionComponent, MouseEvent } from 'react'; | ||
import ReactFlow, { | ||
addEdge, | ||
Edge, | ||
Connection, | ||
NodeTypes, | ||
useNodesState, | ||
useEdgesState, | ||
Node, | ||
} from 'reactflow'; | ||
|
||
import { GraphNode, type CustomGraphNode } from '../graphNode'; | ||
import 'reactflow/dist/style.css'; | ||
import { ClusterStatus, ClusterType } from '../../types/provision'; | ||
import { InstallationType } from '../../types/redux'; | ||
import { ClusterInfo } from '../../components/clusterTable/clusterTable'; | ||
|
||
const initialNodes: CustomGraphNode[] = [ | ||
const clusters: ClusterInfo[] = [ | ||
{ | ||
id: '1', | ||
type: 'custom', | ||
data: { | ||
clusterName: 'kuberfirst-mgmt2', | ||
type: ClusterType.MANAGEMENT, | ||
cloudProvider: InstallationType.AWS, | ||
cloudRegion: 'ap-southeast-1', | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
nodes: 2, | ||
}, | ||
position: { x: 5, y: 0 }, | ||
draggable: false, | ||
clusterName: 'kuberfirst-mgmt', | ||
type: ClusterType.MANAGEMENT, | ||
cloudProvider: InstallationType.AWS, | ||
cloudRegion: 'ap-southeast-1', | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
nodes: 2, | ||
}, | ||
{ | ||
id: '2', | ||
type: 'custom', | ||
data: { | ||
clusterName: 'kuberfirst-worker-1', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.AWS, | ||
cloudRegion: 'ap-southeast-1', | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.ERROR, | ||
nodes: 2, | ||
}, | ||
position: { x: 500, y: -100 }, | ||
draggable: false, | ||
clusterName: 'kuberfirst-worker-1', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.CIVO, | ||
cloudRegion: 'ap-southeast-1', | ||
nodes: 2, | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.ERROR, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
}, | ||
{ | ||
id: '3', | ||
type: 'custom', | ||
data: { | ||
clusterName: 'kuberfirst-worker-2', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.AWS, | ||
cloudRegion: 'ap-southeast-1', | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
nodes: 2, | ||
}, | ||
position: { x: 500, y: 100 }, | ||
clusterName: 'kuberfirst-worker-2', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.DIGITAL_OCEAN, | ||
cloudRegion: 'ap-southeast-1', | ||
nodes: 2, | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.DELETING, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
}, | ||
]; | ||
|
||
const initialEdges: Edge[] = [ | ||
{ | ||
id: 'edge-1', | ||
source: '1', | ||
target: '2', | ||
animated: false, | ||
type: 'straight', | ||
style: { strokeWidth: 2, stroke: '#CBD5E1' }, | ||
clusterName: 'kuberfirst-worker-3', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.DIGITAL_OCEAN, | ||
cloudRegion: 'ap-southeast-1', | ||
nodes: 2, | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
}, | ||
{ | ||
id: 'edge-2', | ||
source: '1', | ||
target: '3', | ||
clusterName: 'kuberfirst-worker-4', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.VULTR, | ||
cloudRegion: 'ap-southeast-1', | ||
nodes: 2, | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
}, | ||
{ | ||
clusterName: 'kuberfirst-worker-5', | ||
type: ClusterType.WORKLOAD, | ||
cloudProvider: InstallationType.VULTR, | ||
cloudRegion: 'ap-southeast-1', | ||
nodes: 2, | ||
creationDate: '05 Apr 2023, 12:24:56', | ||
gitUser: 'Eleanor Carroll', | ||
status: ClusterStatus.PROVISIONED, | ||
adminEmail: '[email protected]', | ||
gitProvider: 'Github', | ||
domainName: 'yourdomain.com', | ||
}, | ||
]; | ||
|
||
function generateNode( | ||
cluster: ClusterInfo, | ||
id: string, | ||
position: { x: number; y: number }, | ||
): CustomGraphNode { | ||
return { | ||
id, | ||
type: 'custom', | ||
data: cluster, | ||
position, | ||
draggable: false, | ||
}; | ||
} | ||
|
||
function generateEdge(id: string, source: string, target: string): Edge { | ||
return { | ||
id, | ||
source, | ||
target, | ||
animated: false, | ||
type: 'straight', | ||
style: { strokeWidth: 2, stroke: '#CBD5E1' }, | ||
}, | ||
]; | ||
}; | ||
} | ||
|
||
function generateNodesConfig( | ||
managementCluster: ClusterInfo, | ||
workloadClusters: ClusterInfo[], | ||
): [CustomGraphNode[], Edge[]] { | ||
const managementNodeId = '1'; | ||
const nodes: CustomGraphNode[] = [ | ||
generateNode(managementCluster, managementNodeId, { | ||
x: 0, | ||
y: (workloadClusters.length * 200) / 2, | ||
}), | ||
]; | ||
|
||
const edges: Edge[] = []; | ||
|
||
for (let i = 0; i < workloadClusters.length; i += 1) { | ||
const generatedId = `${i + 2}`; | ||
|
||
nodes.push(generateNode(workloadClusters[i], generatedId, { x: 600, y: i * 200 })); | ||
edges.push(generateEdge(`edge-${i + 1}`, managementNodeId, generatedId)); | ||
} | ||
|
||
return [nodes, edges]; | ||
} | ||
|
||
const [managemenCluster, ...workloadClusters] = clusters; | ||
|
||
const [initialNodes, initialEdges] = generateNodesConfig(managemenCluster, workloadClusters); | ||
|
||
const nodeTypes: NodeTypes = { | ||
custom: GraphNode, | ||
}; | ||
|
||
export const Flow: FunctionComponent = () => { | ||
interface FlowProps { | ||
onNodeClick: (clusterInfo: ClusterInfo) => void; | ||
} | ||
|
||
export const Flow: FunctionComponent<FlowProps> = ({ onNodeClick }) => { | ||
const [nodes, , onNodesChange] = useNodesState(initialNodes); | ||
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); | ||
const onConnect = useCallback( | ||
(params: Connection | Edge) => setEdges((eds) => addEdge(params, eds)), | ||
[setEdges], | ||
); | ||
|
||
const handleNodeClick = useCallback( | ||
(e: MouseEvent, node: Node) => { | ||
onNodeClick(node.data); | ||
}, | ||
[onNodeClick], | ||
); | ||
|
||
return ( | ||
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
onNodeClick={handleNodeClick} | ||
onConnect={onConnect} | ||
fitView | ||
nodeTypes={nodeTypes} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters