Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create separate provider for the context #1050

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 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>
);
};

// 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
Comment on lines -168 to -169
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this comment back...

// 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 (
<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
107 changes: 85 additions & 22 deletions packages/odf/components/topology/Topology.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>([]);
const [activeItem, setActiveItem] = React.useState<string>('');
const contextValue = React.useMemo(
() => ({
activeItemsUID,
setActiveItemsUID,
activeItem,
setActiveItem,
}),
[activeItemsUID, setActiveItemsUID, activeItem, setActiveItem]
);

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

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<GraphElement>(null);
const contextValue = React.useMemo(
() => ({
nodes,
storageCluster,
zones,
deployments,
visualizationLevel,
activeNode,
nodeDeploymentMap,
selectedElement,
setSelectedElement,
}),
[
nodes,
storageCluster,
zones,
deployments,
visualizationLevel,
activeNode,
nodeDeploymentMap,
selectedElement,
setSelectedElement,
]
);

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

const Topology: React.FC = () => {
const [controller, setController] = React.useState<Visualization>(null);
const [visualizationLevel, setVisualizationLevel] =
React.useState<TopologyViewLevel>(TopologyViewLevel.NODES);
const [activeItemsUID, setActiveItemsUID] = React.useState<string[]>([]);
const [activeItem, setActiveItem] = React.useState<string>('');
const [activeNode, setActiveNode] = React.useState('');
const [selectedElement, setSelectedElement] =
React.useState<GraphElement>(null);

const [nodes, nodesLoaded, nodesError] =
useK8sWatchResource<NodeKind[]>(nodeResource);
Expand Down Expand Up @@ -532,23 +602,16 @@ const Topology: React.FC = () => {
const zones = memoizedNodes.map(getTopologyDomain);

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

Expand Down