-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create clean, app-specific mount functions
- Loading branch information
1 parent
fbcb74c
commit 5dab918
Showing
8 changed files
with
325 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { CoreStart } from 'kibana/public'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { | ||
useUiSetting$, | ||
KibanaContextProvider, | ||
} from '../../../../../src/plugins/kibana_react/public'; | ||
import { TriggersActionsProvider } from '../utils/triggers_actions_context'; | ||
import { ClientPluginsStart } from '../types'; | ||
import { TriggersAndActionsUIPublicPluginStart } from '../../../triggers_actions_ui/public'; | ||
import { ApolloClientContext } from '../utils/apollo_context'; | ||
import { EuiThemeProvider } from '../../../observability/public'; | ||
import { NavigationWarningPromptProvider } from '../utils/navigation_warning_prompt'; | ||
|
||
export const CommonInfraProviders: React.FC<{ | ||
apolloClient: ApolloClient<{}>; | ||
triggersActionsUI: TriggersAndActionsUIPublicPluginStart; | ||
}> = ({ apolloClient, children, triggersActionsUI }) => { | ||
const [darkMode] = useUiSetting$<boolean>('theme:darkMode'); | ||
|
||
return ( | ||
<TriggersActionsProvider triggersActionsUI={triggersActionsUI}> | ||
<ApolloClientContext.Provider value={apolloClient}> | ||
<EuiThemeProvider darkMode={darkMode}> | ||
<NavigationWarningPromptProvider>{children}</NavigationWarningPromptProvider> | ||
</EuiThemeProvider> | ||
</ApolloClientContext.Provider> | ||
</TriggersActionsProvider> | ||
); | ||
}; | ||
|
||
export const CoreProviders: React.FC<{ | ||
core: CoreStart; | ||
plugins: ClientPluginsStart; | ||
}> = ({ children, core, plugins }) => { | ||
return ( | ||
<KibanaContextProvider services={{ ...core, ...plugins }}> | ||
<core.i18n.Context>{children}</core.i18n.Context> | ||
</KibanaContextProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ApolloClient } from 'apollo-client'; | ||
import { History } from 'history'; | ||
import { CoreStart } from 'kibana/public'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Route, Router, Switch } from 'react-router-dom'; | ||
import { AppMountParameters } from '../../../../../src/core/public'; | ||
import { NotFoundPage } from '../pages/404'; | ||
import { LinkToLogsPage } from '../pages/link_to/link_to_logs'; | ||
import { LogsPage } from '../pages/logs'; | ||
import { ClientPluginsStart } from '../types'; | ||
import { createApolloClient } from '../utils/apollo_client'; | ||
import { RedirectWithQueryParams } from '../utils/redirect_with_query_params'; | ||
import { CommonInfraProviders, CoreProviders } from './common_providers'; | ||
|
||
export const renderApp = ( | ||
core: CoreStart, | ||
plugins: ClientPluginsStart, | ||
{ element, history }: AppMountParameters | ||
) => { | ||
const apolloClient = createApolloClient(core.http.fetch); | ||
|
||
ReactDOM.render( | ||
<LogsApp apolloClient={apolloClient} core={core} history={history} plugins={plugins} />, | ||
element | ||
); | ||
|
||
return () => { | ||
ReactDOM.unmountComponentAtNode(element); | ||
}; | ||
}; | ||
|
||
const LogsApp: React.FC<{ | ||
apolloClient: ApolloClient<{}>; | ||
core: CoreStart; | ||
history: History<unknown>; | ||
plugins: ClientPluginsStart; | ||
}> = ({ apolloClient, core, history, plugins }) => { | ||
const uiCapabilities = core.application.capabilities; | ||
|
||
return ( | ||
<CoreProviders core={core} plugins={plugins}> | ||
<CommonInfraProviders | ||
apolloClient={apolloClient} | ||
triggersActionsUI={plugins.triggers_actions_ui} | ||
> | ||
<Router history={history}> | ||
<Switch> | ||
<Route path="/link-to" component={LinkToLogsPage} /> | ||
{uiCapabilities?.logs?.show && ( | ||
<RedirectWithQueryParams from="/" exact={true} to="/stream" /> | ||
)} | ||
{uiCapabilities?.logs?.show && <Route path="/" component={LogsPage} />} | ||
<Route component={NotFoundPage} /> | ||
</Switch> | ||
</Router> | ||
</CommonInfraProviders> | ||
</CoreProviders> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ApolloClient } from 'apollo-client'; | ||
import { History } from 'history'; | ||
import { CoreStart } from 'kibana/public'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Route, Router, Switch } from 'react-router-dom'; | ||
import { AppMountParameters } from '../../../../../src/core/public'; | ||
import { NotFoundPage } from '../pages/404'; | ||
import { LinkToMetricsPage } from '../pages/link_to/link_to_metrics'; | ||
import { InfrastructurePage } from '../pages/metrics'; | ||
import { MetricDetail } from '../pages/metrics/metric_detail'; | ||
import { ClientPluginsStart } from '../types'; | ||
import { createApolloClient } from '../utils/apollo_client'; | ||
import { RedirectWithQueryParams } from '../utils/redirect_with_query_params'; | ||
import { CommonInfraProviders, CoreProviders } from './common_providers'; | ||
|
||
export const renderApp = ( | ||
core: CoreStart, | ||
plugins: ClientPluginsStart, | ||
{ element, history }: AppMountParameters | ||
) => { | ||
const apolloClient = createApolloClient(core.http.fetch); | ||
|
||
ReactDOM.render( | ||
<MetricsApp apolloClient={apolloClient} core={core} history={history} plugins={plugins} />, | ||
element | ||
); | ||
|
||
return () => { | ||
ReactDOM.unmountComponentAtNode(element); | ||
}; | ||
}; | ||
|
||
const MetricsApp: React.FC<{ | ||
apolloClient: ApolloClient<{}>; | ||
core: CoreStart; | ||
history: History<unknown>; | ||
plugins: ClientPluginsStart; | ||
}> = ({ apolloClient, core, history, plugins }) => { | ||
const uiCapabilities = core.application.capabilities; | ||
|
||
return ( | ||
<CoreProviders core={core} plugins={plugins}> | ||
<CommonInfraProviders | ||
apolloClient={apolloClient} | ||
triggersActionsUI={plugins.triggers_actions_ui} | ||
> | ||
<Router history={history}> | ||
<Switch> | ||
<Route path="/link-to" component={LinkToMetricsPage} /> | ||
{uiCapabilities?.infrastructure?.show && ( | ||
<RedirectWithQueryParams from="/" exact={true} to="/inventory" /> | ||
)} | ||
{uiCapabilities?.infrastructure?.show && ( | ||
<RedirectWithQueryParams from="/snapshot" exact={true} to="/inventory" /> | ||
)} | ||
{uiCapabilities?.infrastructure?.show && ( | ||
<RedirectWithQueryParams from="/metrics-explorer" exact={true} to="/explorer" /> | ||
)} | ||
{uiCapabilities?.infrastructure?.show && ( | ||
<Route path="/detail/:type/:node" component={MetricDetail} /> | ||
)} | ||
{uiCapabilities?.infrastructure?.show && ( | ||
<Route path="/" component={InfrastructurePage} /> | ||
)} | ||
<Route component={NotFoundPage} /> | ||
</Switch> | ||
</Router> | ||
</CommonInfraProviders> | ||
</CoreProviders> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { DataPublicPluginStart } from '../../../../src/plugins/data/public'; | ||
import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; | ||
import { TriggersAndActionsUIPublicPluginSetup } from '../../../plugins/triggers_actions_ui/public'; | ||
import { DataEnhancedStart } from '../../data_enhanced/public'; | ||
|
||
export interface ClientPluginsSetup { | ||
home: HomePublicPluginSetup; | ||
triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup; | ||
} | ||
|
||
export interface ClientPluginsStart { | ||
data: DataPublicPluginStart; | ||
dataEnhanced: DataEnhancedStart; | ||
triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup; | ||
} |
Oops, something went wrong.