From 6a1e820e5d081f3e90bcd08011b5a422d0c5e2ce Mon Sep 17 00:00:00 2001 From: Timothy Asir Jeyasingh Date: Wed, 20 Sep 2023 14:25:40 +0530 Subject: [PATCH] Create separate provider for the context and pass its descendants as a childern Signed-off-by: Timothy Asir Jeyasingh --- .../data-policy/dr-dashboard.tsx | 47 ++++++-- packages/odf/components/topology/Topology.tsx | 107 ++++++++++++++---- 2 files changed, 120 insertions(+), 34 deletions(-) diff --git a/packages/mco/components/mco-dashboard/data-policy/dr-dashboard.tsx b/packages/mco/components/mco-dashboard/data-policy/dr-dashboard.tsx index a3b010d5a..02469df69 100644 --- a/packages/mco/components/mco-dashboard/data-policy/dr-dashboard.tsx +++ b/packages/mco/components/mco-dashboard/data-policy/dr-dashboard.tsx @@ -63,14 +63,30 @@ const UpperSection: React.FC = () => ( ); -export const DRDashboard: React.FC = () => { +const CSVStatusesContextProvider: React.FC<{ children: React.ReactNode }> = ({ + children, +}) => { const [csvData, csvError, csvLoading] = useCustomPrometheusPoll({ endpoint: 'api/v1/query' as any, query: STATUS_QUERIES[StorageDashboard.CSV_STATUS_ALL_WHITELISTED], basePath: ACM_ENDPOINT, cluster: HUB_CLUSTER_NAME, }); + const contextValue = React.useMemo( + () => ({ csvData, csvError, csvLoading }), + [csvData, csvError, csvLoading] + ); + + return ( + + {children} + + ); +}; +const DRResourcesContextProvider: React.FC<{ children: React.ReactNode }> = ({ + children, +}) => { const [drResources, drLoaded, drLoadError] = useDisasterRecoveryResourceWatch(); const [argoApplicationSetResources, loaded, loadError] = @@ -159,21 +175,28 @@ export const DRDashboard: React.FC = () => { loadError, ]); - const dRResourcesContext = { - drClusterAppsMap, - loaded, - loadError, - }; + const contextValue = React.useMemo( + () => ({ drClusterAppsMap, loaded, loadError }), + [drClusterAppsMap, loaded, loadError] + ); + + return ( + + {children} + + ); +}; - // ToDo(Sanjal): combime multiple Context together to make it scalable - // refer: https://javascript.plainenglish.io/how-to-combine-context-providers-for-cleaner-react-code-9ed24f20225e +// ToDo(Sanjal): combime multiple Context together to make it scalable +// refer: https://javascript.plainenglish.io/how-to-combine-context-providers-for-cleaner-react-code-9ed24f20225e +const DRDashboard: React.FC = () => { return (
- - + + - - + +
); }; diff --git a/packages/odf/components/topology/Topology.tsx b/packages/odf/components/topology/Topology.tsx index 928072583..cd90cd197 100644 --- a/packages/odf/components/topology/Topology.tsx +++ b/packages/odf/components/topology/Topology.tsx @@ -432,15 +432,85 @@ const TopologyViewComponent: React.FC = () => { const Error = ({ error }) => <>{error}; +const TopologySearchContextProvider: React.FC<{ children: React.ReactNode }> = + ({ children }) => { + const [activeItemsUID, setActiveItemsUID] = React.useState([]); + const [activeItem, setActiveItem] = React.useState(''); + const contextValue = React.useMemo( + () => ({ + activeItemsUID, + setActiveItemsUID, + activeItem, + setActiveItem, + }), + [activeItemsUID, setActiveItemsUID, activeItem, setActiveItem] + ); + + return ( + + {children} + + ); + }; + +const TopologyDataContextProvider: React.FC<{ + children: React.ReactNode; + nodes: NodeKind[]; + storageCluster: StorageClusterKind; + zones: string[]; + deployments: DeploymentKind[]; + visualizationLevel: TopologyViewLevel; + activeNode: string; + nodeDeploymentMap: any; +}> = ({ + children, + nodes, + storageCluster, + zones, + deployments, + visualizationLevel, + activeNode, + nodeDeploymentMap, +}) => { + const [selectedElement, setSelectedElement] = + React.useState(null); + const contextValue = React.useMemo( + () => ({ + nodes, + storageCluster, + zones, + deployments, + visualizationLevel, + activeNode, + nodeDeploymentMap, + selectedElement, + setSelectedElement, + }), + [ + nodes, + storageCluster, + zones, + deployments, + visualizationLevel, + activeNode, + nodeDeploymentMap, + selectedElement, + setSelectedElement, + ] + ); + + return ( + + {children} + + ); +}; + const Topology: React.FC = () => { const [controller, setController] = React.useState(null); const [visualizationLevel, setVisualizationLevel] = React.useState(TopologyViewLevel.NODES); - const [activeItemsUID, setActiveItemsUID] = React.useState([]); - const [activeItem, setActiveItem] = React.useState(''); const [activeNode, setActiveNode] = React.useState(''); - const [selectedElement, setSelectedElement] = - React.useState(null); const [nodes, nodesLoaded, nodesError] = useK8sWatchResource(nodeResource); @@ -532,23 +602,16 @@ const Topology: React.FC = () => { const zones = memoizedNodes.map(getTopologyDomain); return ( - - +
@@ -569,8 +632,8 @@ const Topology: React.FC = () => {
-
-
+ + ); };