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.
* chore: create react flow redux state to house nodes and edges hydrated by fetch of clusters. * chore:move react flow helper funcs to utils * chore: hydrate nodes and edges for react flow redux state. use implicit type attached to return of getClusters instead of explicit. * chore: use react flow state for nodes and edges in GraphView comp
- Loading branch information
Showing
8 changed files
with
181 additions
and
210 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,155 +1,12 @@ | ||
import React, { useCallback, type FunctionComponent, MouseEvent } from 'react'; | ||
import ReactFlow, { | ||
addEdge, | ||
Edge, | ||
Connection, | ||
NodeTypes, | ||
useNodesState, | ||
useEdgesState, | ||
Node, | ||
ReactFlowProvider, | ||
useReactFlow, | ||
} from 'reactflow'; | ||
import ReactFlow, { NodeTypes, Node, ReactFlowProvider, useReactFlow } 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 { GraphNode } from '../graphNode'; | ||
import { ClusterInfo } from '../../components/clusterTable/clusterTable'; | ||
import { useAppDispatch, useAppSelector } from '../../redux/store'; | ||
import { onConnect, onEdgesChange, onNodesChange } from '../../redux/slices/reactFlow.slice'; | ||
|
||
const clusters: ClusterInfo[] = [ | ||
{ | ||
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, | ||
}, | ||
{ | ||
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', | ||
}, | ||
{ | ||
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', | ||
}, | ||
{ | ||
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', | ||
}, | ||
{ | ||
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); | ||
import 'reactflow/dist/style.css'; | ||
|
||
const nodeTypes: NodeTypes = { | ||
custom: GraphNode, | ||
|
@@ -160,15 +17,11 @@ interface GraphViewProps { | |
} | ||
|
||
const GraphView: FunctionComponent<GraphViewProps> = ({ onNodeClick }) => { | ||
const [nodes, , onNodesChange] = useNodesState(initialNodes); | ||
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); | ||
const { nodes, edges } = useAppSelector(({ reactFlow }) => reactFlow); | ||
|
||
const { setCenter } = useReactFlow(); | ||
const dispatch = useAppDispatch(); | ||
|
||
const onConnect = useCallback( | ||
(params: Connection | Edge) => setEdges((eds) => addEdge(params, eds)), | ||
[setEdges], | ||
); | ||
const { setCenter } = useReactFlow(); | ||
|
||
const handleNodeClick = useCallback( | ||
(e: MouseEvent, node: Node) => { | ||
|
@@ -187,12 +40,12 @@ const GraphView: FunctionComponent<GraphViewProps> = ({ onNodeClick }) => { | |
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
onNodesChange={(changes) => dispatch(onNodesChange(changes))} | ||
onEdgesChange={(changes) => dispatch(onEdgesChange(changes))} | ||
onConnect={(connection) => dispatch(onConnect(connection))} | ||
onNodeClick={handleNodeClick} | ||
onConnect={onConnect} | ||
fitView | ||
nodeTypes={nodeTypes} | ||
fitView | ||
/> | ||
); | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { | ||
addEdge, | ||
applyEdgeChanges, | ||
applyNodeChanges, | ||
Connection, | ||
Edge, | ||
EdgeChange, | ||
Node, | ||
NodeChange, | ||
} from 'reactflow'; | ||
|
||
export interface ReactFlowState { | ||
nodes: Node[]; | ||
edges: Edge[]; | ||
} | ||
|
||
export const initialState: ReactFlowState = { | ||
nodes: [], | ||
edges: [], | ||
}; | ||
|
||
const reactFlowSlice = createSlice({ | ||
name: 'react-flow', | ||
initialState, | ||
reducers: { | ||
setNodes: (state, { payload }: PayloadAction<Node[]>) => { | ||
state.nodes = payload; | ||
}, | ||
setEdges: (state, { payload }: PayloadAction<Edge[]>) => { | ||
state.edges = payload; | ||
}, | ||
onNodesChange: (state, { payload }: PayloadAction<NodeChange[]>) => { | ||
state.nodes = applyNodeChanges(payload, state.nodes); | ||
}, | ||
onEdgesChange: (state, { payload }: PayloadAction<EdgeChange[]>) => { | ||
state.edges = applyEdgeChanges(payload, state.edges); | ||
}, | ||
onConnect: (state, { payload }: PayloadAction<Connection>) => { | ||
state.edges = addEdge(payload, state.edges); | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setNodes, setEdges, onNodesChange, onEdgesChange, onConnect } = | ||
reactFlowSlice.actions; | ||
|
||
export const reactFlowReducer = reactFlowSlice.reducer; |
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
Oops, something went wrong.