Skip to content

Commit

Permalink
Create separate provider for the context and pass its descendants as …
Browse files Browse the repository at this point in the history
…a childern

Signed-off-by: Timothy Asir Jeyasingh <[email protected]>
  • Loading branch information
TimothyAsirJeyasing committed Sep 20, 2023
1 parent 4cdc53a commit 831ebe1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 29 deletions.
43 changes: 33 additions & 10 deletions packages/mco/components/mco-dashboard/data-policy/dr-dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,30 @@ const UpperSection: React.FC = () => (
</Grid>
);

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 (
<CSVStatusesContext.Provider value={contextValue}>
{children}
</CSVStatusesContext.Provider>
);
};

const DRResourcesContextProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [drResources, drLoaded, drLoadError] =
useDisasterRecoveryResourceWatch();
const [argoApplicationSetResources, loaded, loadError] =
Expand Down Expand Up @@ -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 (
<DRResourcesContext.Provider value={contextValue}>
{children}
</DRResourcesContext.Provider>
);
};

const DRDashboard: React.FC = () => {
// 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
return (
<div className="odf-dashboard-body">
<CSVStatusesContext.Provider value={{ csvData, csvError, csvLoading }}>
<DRResourcesContext.Provider value={dRResourcesContext}>
<CSVStatusesContextProvider>
<DRResourcesContextProvider>
<UpperSection />
</DRResourcesContext.Provider>
</CSVStatusesContext.Provider>
</DRResourcesContextProvider>
</CSVStatusesContextProvider>
</div>
);
};
Expand Down
78 changes: 59 additions & 19 deletions packages/odf/components/topology/Topology.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,64 @@ const Topology: React.FC = () => {

const zones = memoizedNodes.map(getTopologyDomain);

const TopologyDataContextProvider: React.FC<{ children: React.ReactNode }> =
({ children }) => {
const contextValue = React.useMemo(
() => ({
nodes: memoizedNodes,
storageCluster: storageCluster[0],
zones,
deployments: memoizedDeployments,
visualizationLevel: visualizationLevel,
activeNode,
setActiveNode,
nodeDeploymentMap,
selectedElement,
setSelectedElement,
}),
[

Check warning on line 549 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (14)

React Hook React.useMemo has unnecessary dependencies: 'activeNode', 'memoizedDeployments', 'memoizedNodes', 'nodeDeploymentMap', 'selectedElement', 'setActiveNode', 'setSelectedElement', 'storageCluster', 'visualizationLevel', and 'zones'. Either exclude them or remove the dependency array. Outer scope values like 'memoizedNodes' aren't valid dependencies because mutating them doesn't re-render the component

Check warning on line 549 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (16)

React Hook React.useMemo has unnecessary dependencies: 'activeNode', 'memoizedDeployments', 'memoizedNodes', 'nodeDeploymentMap', 'selectedElement', 'setActiveNode', 'setSelectedElement', 'storageCluster', 'visualizationLevel', and 'zones'. Either exclude them or remove the dependency array. Outer scope values like 'memoizedNodes' aren't valid dependencies because mutating them doesn't re-render the component

Check warning on line 549 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (18)

React Hook React.useMemo has unnecessary dependencies: 'activeNode', 'memoizedDeployments', 'memoizedNodes', 'nodeDeploymentMap', 'selectedElement', 'setActiveNode', 'setSelectedElement', 'storageCluster', 'visualizationLevel', and 'zones'. Either exclude them or remove the dependency array. Outer scope values like 'memoizedNodes' aren't valid dependencies because mutating them doesn't re-render the component
memoizedNodes,
storageCluster,
zones,
memoizedDeployments,
visualizationLevel,
activeNode,
setActiveNode,
nodeDeploymentMap,
selectedElement,
setSelectedElement,
]
);

return (
<TopologyDataContext.Provider value={contextValue}>
{children}
</TopologyDataContext.Provider>
);
};

const TopologySearchContextProvider: React.FC<{ children: React.ReactNode }> =
({ children }) => {
const contextValue = React.useMemo(
() => ({
activeItemsUID,
setActiveItemsUID,
activeItem,
setActiveItem,
}),
[activeItemsUID, setActiveItemsUID, activeItem, setActiveItem]

Check warning on line 579 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (14)

React Hook React.useMemo has unnecessary dependencies: 'activeItem', 'activeItemsUID', 'setActiveItem', and 'setActiveItemsUID'. Either exclude them or remove the dependency array. Outer scope values like 'activeItemsUID' aren't valid dependencies because mutating them doesn't re-render the component

Check warning on line 579 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (16)

React Hook React.useMemo has unnecessary dependencies: 'activeItem', 'activeItemsUID', 'setActiveItem', and 'setActiveItemsUID'. Either exclude them or remove the dependency array. Outer scope values like 'activeItemsUID' aren't valid dependencies because mutating them doesn't re-render the component

Check warning on line 579 in packages/odf/components/topology/Topology.tsx

View workflow job for this annotation

GitHub Actions / resolve-dependencies (18)

React Hook React.useMemo has unnecessary dependencies: 'activeItem', 'activeItemsUID', 'setActiveItem', and 'setActiveItemsUID'. Either exclude them or remove the dependency array. Outer scope values like 'activeItemsUID' aren't valid dependencies because mutating them doesn't re-render the component
);

return (
<TopologySearchContext.Provider value={contextValue}>
{children}
</TopologySearchContext.Provider>
);
};

return (
<TopologyDataContext.Provider
value={{
nodes: memoizedNodes,
storageCluster: storageCluster[0],
zones,
deployments: memoizedDeployments,
visualizationLevel: visualizationLevel,
activeNode,
setActiveNode,
nodeDeploymentMap,
selectedElement,
setSelectedElement,
}}
>
<TopologySearchContext.Provider
value={{ activeItemsUID, setActiveItemsUID, activeItem, setActiveItem }}
>
<TopologyDataContextProvider>
<TopologySearchContextProvider>
<VisualizationProvider controller={controller}>
<div className="odf__topology-view" id="odf-topology">
<TopologyTopBar />
Expand All @@ -569,8 +609,8 @@ const Topology: React.FC = () => {
</HandleErrorAndLoading>
</div>
</VisualizationProvider>
</TopologySearchContext.Provider>
</TopologyDataContext.Provider>
</TopologySearchContextProvider>
</TopologyDataContextProvider>
);
};

Expand Down

0 comments on commit 831ebe1

Please sign in to comment.