Skip to content

Commit

Permalink
re building services on page switches
Browse files Browse the repository at this point in the history
  • Loading branch information
RamakrishnaChilaka committed Mar 20, 2024
1 parent 4937cdd commit 66bc3d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 61 deletions.
50 changes: 8 additions & 42 deletions public/index_management_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import { CoreStart, AppMountParameters } from "opensearch-dashboards/public";
import React from "react";
import ReactDOM from "react-dom";
import { HashRouter as Router, Route } from "react-router-dom";
import {
IndexService,
ManagedIndexService,
PolicyService,
RollupService,
TransformService,
NotificationService,
ServicesContext,
SnapshotManagementService,
CommonService,
} from "./services";
import { DarkModeContext } from "./components/DarkMode";
import Main from "./pages/Main";
import { CoreServicesContext } from "./components/core_services";
Expand All @@ -30,44 +19,21 @@ export function renderApp(
params: AppMountParameters,
landingPage: string
) {
const http = coreStart.http;

const indexService = new IndexService(http);
const managedIndexService = new ManagedIndexService(http);
const policyService = new PolicyService(http);
const rollupService = new RollupService(http);
const transformService = new TransformService(http);
const notificationService = new NotificationService(http);
const snapshotManagementService = new SnapshotManagementService(http);
const commonService = new CommonService(http);
const services = {
indexService,
managedIndexService,
policyService,
rollupService,
transformService,
notificationService,
snapshotManagementService,
commonService,
};

const isDarkMode = coreStart.uiSettings.get("theme:darkMode") || false;

ReactDOM.render(
<Router>
<Route
render={(props) => (
<DarkModeContext.Provider value={isDarkMode}>
<ServicesContext.Provider value={services}>
<CoreServicesContext.Provider value={coreStart}>
<Main
{...props}
landingPage={landingPage}
setActionMenu={params.setHeaderActionMenu}
multiDataSourceEnabled={!!pluginStartDependencies.dataSource}
/>
</CoreServicesContext.Provider>
</ServicesContext.Provider>
<CoreServicesContext.Provider value={coreStart}>
<Main
{...props}
landingPage={landingPage}
setActionMenu={params.setHeaderActionMenu}
multiDataSourceEnabled={!!pluginStartDependencies.dataSource}
/>
</CoreServicesContext.Provider>
</DarkModeContext.Provider>
)}
/>
Expand Down
91 changes: 72 additions & 19 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, { Component, createContext } from "react";
import { Switch, Route, Redirect, RouteComponentProps } from "react-router-dom";
// @ts-ignore
import { EuiSideNav, EuiPage, EuiPageBody, EuiPageSideBar, Query, Direction } from "@elastic/eui";
import { CoreStart, MountPoint } from "opensearch-dashboards/public";
import { CoreStart, HttpSetup, MountPoint } from "opensearch-dashboards/public";
import queryString from "query-string";
import Policies from "../Policies";
import ManagedIndices from "../ManagedIndices";
Expand All @@ -18,7 +18,16 @@ import ChangePolicy from "../ChangePolicy";
import PolicyDetails from "../PolicyDetails/containers/PolicyDetails";
import Rollups from "../Rollups";
import { ModalProvider, ModalRoot } from "../../components/Modal";
import { ServicesConsumer, ServicesContext } from "../../services";
import {
ManagedIndexService,
NotificationService,
PolicyService,
RollupService,
ServicesConsumer,
ServicesContext,
SnapshotManagementService,
TransformService,
} from "../../services";
import { BrowserServices } from "../../models/interfaces";
import { ROUTES } from "../../utils/constants";
import { CoreServicesConsumer } from "../../components/core_services";
Expand Down Expand Up @@ -125,6 +134,25 @@ interface MainState {
dataSourceLabel: string;
}

const dataSourceEnabledPaths: string[] = [
ROUTES.CREATE_DATA_STREAM,
ROUTES.CREATE_TEMPLATE,
ROUTES.CREATE_COMPOSABLE_TEMPLATE,
ROUTES.FORCE_MERGE,
ROUTES.SPLIT_INDEX,
ROUTES.ROLLOVER,
ROUTES.INDEX_DETAIL,
ROUTES.INDICES,
ROUTES.CREATE_INDEX,
ROUTES.ALIASES,
ROUTES.DATA_STREAMS,
ROUTES.TEMPLATES,
ROUTES.CREATE_DATA_STREAM,
ROUTES.CREATE_TEMPLATE,
ROUTES.COMPOSABLE_TEMPLATES,
ROUTES.CREATE_COMPOSABLE_TEMPLATE,
];

export default class Main extends Component<MainProps, MainState> {
constructor(props: MainProps) {
super(props);
Expand All @@ -145,6 +173,40 @@ export default class Main extends Component<MainProps, MainState> {
}
}

isDataSourceEnabledForPath(path: string): boolean {
return dataSourceEnabledPaths.some((dataSourceEnabledPath: string) => path.startsWith(dataSourceEnabledPath));
}

getServices(http: HttpSetup) {
const {
location: { pathname },
} = this.props;
const indexService = new IndexService(http);
const managedIndexService = new ManagedIndexService(http);
const policyService = new PolicyService(http);
const rollupService = new RollupService(http);
const transformService = new TransformService(http);
const notificationService = new NotificationService(http);
const snapshotManagementService = new SnapshotManagementService(http);
const commonService = new CommonService(http);
const services = {
indexService,
managedIndexService,
policyService,
rollupService,
transformService,
notificationService,
snapshotManagementService,
commonService,
};

if (this.props.multiDataSourceEnabled && this.isDataSourceEnabledForPath(pathname)) {
services.indexService = new IndexService(http, this.state.dataSourceId);
services.commonService = new CommonService(http, this.state.dataSourceId);
}
return services;
}

render() {
const {
location: { pathname },
Expand Down Expand Up @@ -254,19 +316,10 @@ export default class Main extends Component<MainProps, MainState> {
<CoreServicesConsumer>
{(core: CoreStart | null) =>
core && (
<ServicesConsumer>
{(services: BrowserServices | null) =>
services && (
<ServicesContext.Provider
value={(() => {
if (!this.props.multiDataSourceEnabled) {
return services;
}
services.indexService = new IndexService(core.http, this.state.dataSourceId);
services.commonService = new CommonService(core.http, this.state.dataSourceId);
return services;
})()}
>
<ServicesContext.Provider value={this.getServices(core.http)}>
<ServicesConsumer>
{(services: BrowserServices | null) =>
services && (
<ModalProvider>
<DataSourceMenuContext.Provider
value={{
Expand Down Expand Up @@ -765,10 +818,10 @@ export default class Main extends Component<MainProps, MainState> {
</EuiPage>
</DataSourceMenuContext.Provider>
</ModalProvider>
</ServicesContext.Provider>
)
}
</ServicesConsumer>
)
}
</ServicesConsumer>
</ServicesContext.Provider>
)
}
</CoreServicesConsumer>
Expand Down

0 comments on commit 66bc3d3

Please sign in to comment.