Skip to content

Commit

Permalink
Add seperate provider for context and pass as childern
Browse files Browse the repository at this point in the history
red-hat-storage#672
Signed-off-by: Timothy Asir Jeyasingh <[email protected]>
  • Loading branch information
TimothyAsirJeyasing committed May 30, 2024
1 parent 241c066 commit 12b2eec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,22 @@ const aggregateAppsMap = (
return acc;
}, {});

const MonitoringDashboard: React.FC = () => {
const CSVStatusesProvider: React.FC = ({ 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,
});

return (
<CSVStatusesContext.Provider value={{ csvData, csvError, csvLoading }}>
{children}
</CSVStatusesContext.Provider>
);
};

const DRResourcesProvider: React.FC = ({ children }) => {
const [managedClusters, managedClusterLoaded, managedClusterLoadError] =
useK8sWatchResource<ACMManagedClusterKind[]>(
getManagedClusterResourceObj()
Expand Down Expand Up @@ -122,17 +130,23 @@ const MonitoringDashboard: React.FC = () => {
loadError,
};

// 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 (
<CSVStatusesContext.Provider value={{ csvData, csvError, csvLoading }}>
<DRResourcesContext.Provider value={dRResourcesContext}>
<UpperSection />
</DRResourcesContext.Provider>
</CSVStatusesContext.Provider>
<DRResourcesContext.Provider value={dRResourcesContext}>
{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
const MonitoringDashboard: React.FC = () => (
<CSVStatusesProvider>
<DRResourcesProvider>
<UpperSection />
</DRResourcesProvider>
</CSVStatusesProvider>
);

const DRDashboard: React.FC = () => {
const isMonitoringEnabled = useFlag(ACM_OBSERVABILITY_FLAG);

Expand Down
30 changes: 25 additions & 5 deletions packages/odf/components/topology/Topology.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {
createNode,
defaultLayoutFactory,
stylesComponentFactory,
TopologyViewLevel,
} from '@odf/shared/topology';
import {
TopologyDataContext,
TopologySearchContext,
TopologyViewLevel,
} from '@odf/shared/topology';
import {
ClusterServiceVersionKind,
Expand Down Expand Up @@ -99,6 +101,24 @@ type SideBarProps = {
isExpanded: boolean;
};

const TopologyDataProvider: React.FC<{ value: any }> = ({
children,
value,
}) => (
<TopologyDataContext.Provider value={value}>
{children}
</TopologyDataContext.Provider>
);

const TopologySearchProvider: React.FC<{ value: any }> = ({
children,
value,
}) => (
<TopologySearchContext.Provider value={value}>
{children}
</TopologySearchContext.Provider>
);

const Sidebar: React.FC<SideBarProps> = ({ onClose, isExpanded }) => {
const { selectedElement: element } = React.useContext(TopologyDataContext);
const data = element?.getData();
Expand Down Expand Up @@ -572,7 +592,7 @@ const Topology: React.FC = () => {
const zones = memoizedNodes.map(getTopologyDomain);

return (
<TopologyDataContext.Provider
<TopologyDataProvider
value={{
nodes: memoizedNodes,
storageCluster: storageCluster,
Expand All @@ -586,7 +606,7 @@ const Topology: React.FC = () => {
setSelectedElement: setSelectedElement as any,
}}
>
<TopologySearchContext.Provider
<TopologySearchProvider
value={{ activeItemsUID, setActiveItemsUID, activeItem, setActiveItem }}
>
<VisualizationProvider controller={controller}>
Expand All @@ -610,8 +630,8 @@ const Topology: React.FC = () => {
</HandleErrorAndLoading>
</div>
</VisualizationProvider>
</TopologySearchContext.Provider>
</TopologyDataContext.Provider>
</TopologySearchProvider>
</TopologyDataProvider>
);
};

Expand Down

0 comments on commit 12b2eec

Please sign in to comment.