Skip to content

Commit 8616036

Browse files
committed
use path navigartion in place of state
1 parent ac843b8 commit 8616036

File tree

8 files changed

+69
-120
lines changed

8 files changed

+69
-120
lines changed

examples/alerting_example/public/application.tsx

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,8 @@
2020
import React from 'react';
2121
import ReactDOM from 'react-dom';
2222
import { BrowserRouter as Router, Route, withRouter, RouteComponentProps } from 'react-router-dom';
23-
24-
import {
25-
EuiPage,
26-
EuiPageSideBar,
27-
// @ts-ignore
28-
EuiSideNav,
29-
} from '@elastic/eui';
30-
31-
import { JsonObject } from '../../../src/plugins/kibana_utils/common';
32-
import { AppMountParameters, CoreStart, ScopedHistory } from '../../../src/core/public';
23+
import { EuiPage, EuiPageSideBar, EuiSideNav } from '@elastic/eui';
24+
import { AppMountParameters, CoreStart } from '../../../src/core/public';
3325
import { Page } from './page';
3426
import { DocumentationPage } from './documentation';
3527
import { CreateAlertPage } from './create_alert';
@@ -66,22 +58,13 @@ const Nav = withRouter(({ history, pages }: NavProps) => {
6658
);
6759
});
6860

69-
export interface NavState {
70-
alert: JsonObject;
71-
alertType: JsonObject;
72-
}
7361
export interface AlertingExampleComponentParams {
7462
application: CoreStart['application'];
7563
http: CoreStart['http'];
76-
history: ScopedHistory<NavState>;
7764
basename: string;
7865
}
7966

80-
const Home = withRouter(({ history }) => {
81-
return history.location.state ? <ViewAlertPage /> : <DocumentationPage />;
82-
});
83-
84-
const AlertingExampleApp = ({ basename, http, history }: AlertingExampleComponentParams) => {
67+
const AlertingExampleApp = ({ basename, http }: AlertingExampleComponentParams) => {
8568
const pages: PageDef[] = [
8669
{
8770
id: 'home',
@@ -93,20 +76,13 @@ const AlertingExampleApp = ({ basename, http, history }: AlertingExampleComponen
9376
title: 'Create',
9477
component: <CreateAlertPage http={http} />,
9578
},
96-
{
97-
id: 'view',
98-
title: 'View Alert',
99-
component: <ViewAlertPage />,
100-
},
10179
];
10280

10381
const routes = pages.map((page, i) => (
10482
<Route
10583
key={i}
10684
path={`/${page.id}`}
107-
render={props => {
108-
return <Page title={page.title}>{page.component}</Page>;
109-
}}
85+
render={() => <Page title={page.title}>{page.component}</Page>}
11086
/>
11187
));
11288

@@ -116,7 +92,17 @@ const AlertingExampleApp = ({ basename, http, history }: AlertingExampleComponen
11692
<EuiPageSideBar>
11793
<Nav pages={pages} />
11894
</EuiPageSideBar>
119-
<Route path="/" exact component={Home} />
95+
<Route path="/" exact render={DocumentationPage} />
96+
<Route
97+
path={`/alert/:id`}
98+
render={(props: RouteComponentProps<{ id: string }>) => {
99+
return (
100+
<Page title={`View Alert ${props.match.params.id}`}>
101+
<ViewAlertPage http={http} id={props.match.params.id} />
102+
</Page>
103+
);
104+
}}
105+
/>
120106
{routes}
121107
</EuiPage>
122108
</Router>
@@ -126,12 +112,11 @@ const AlertingExampleApp = ({ basename, http, history }: AlertingExampleComponen
126112
export const renderApp = (
127113
coreStart: CoreStart,
128114
deps: any,
129-
{ appBasePath, element, history }: AppMountParameters<NavState>
115+
{ appBasePath, element }: AppMountParameters
130116
) => {
131117
ReactDOM.render(
132118
<AlertingExampleApp
133119
basename={appBasePath}
134-
history={history}
135120
application={coreStart.application}
136121
http={coreStart.http}
137122
/>,

examples/alerting_example/public/documentation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from '@elastic/eui';
3232

3333
export const DocumentationPage = () => (
34-
<EuiPageBody data-test-subj="dataPluginExplorerHome">
34+
<EuiPageBody>
3535
<EuiPageHeader>
3636
<EuiPageHeaderSection>
3737
<EuiTitle size="l">

examples/alerting_example/public/plugin.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
import { Plugin, CoreSetup, AppMountParameters } from 'kibana/public';
21-
import { NavState } from './application';
2221
import { PluginSetupContract as AlertingSetup } from '../../../x-pack/plugins/alerting/public';
2322
import { ALERTING_EXAMPLE_APP_ID } from '../common/constants';
2423
import { AlertType, SanitizedAlert } from '../../../x-pack/plugins/alerting/common';
@@ -35,7 +34,7 @@ export class AlertingExamplePlugin implements Plugin<Setup, Start, AlertingExamp
3534
core.application.register({
3635
id: 'AlertingExample',
3736
title: 'Alerting Example',
38-
async mount(params: AppMountParameters<NavState>) {
37+
async mount(params: AppMountParameters) {
3938
const [coreStart, depsStart] = await core.getStartServices();
4039
const { renderApp } = await import('./application');
4140
return renderApp(coreStart, depsStart, params);
@@ -45,13 +44,7 @@ export class AlertingExamplePlugin implements Plugin<Setup, Start, AlertingExamp
4544
alerting.registerNavigation(
4645
ALERTING_EXAMPLE_APP_ID,
4746
'.alerting-example',
48-
(alert: SanitizedAlert, alertType: AlertType) => ({
49-
state: {
50-
// LOLs
51-
alert: JSON.parse(JSON.stringify(alert)),
52-
alertType: JSON.parse(JSON.stringify(alertType)),
53-
},
54-
})
47+
(alert: SanitizedAlert, alertType: AlertType) => `/alert/${alert.id}`
5548
);
5649
}
5750

examples/alerting_example/public/view_alert.tsx

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,37 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import React from 'react';
19+
import React, { useState, useEffect } from 'react';
2020

21-
import {
22-
EuiText,
23-
EuiPageBody,
24-
EuiPageContent,
25-
EuiPageContentBody,
26-
EuiPageContentHeader,
27-
EuiPageContentHeaderSection,
28-
EuiPageHeader,
29-
EuiPageHeaderSection,
30-
EuiTitle,
31-
} from '@elastic/eui';
32-
import { withRouter } from 'react-router-dom';
21+
import { EuiText, EuiLoadingKibana } from '@elastic/eui';
22+
import { withRouter, RouteComponentProps } from 'react-router-dom';
23+
import { CoreStart } from 'kibana/public';
24+
import { Alert, AlertTaskState } from '../../../x-pack/plugins/alerting/common';
3325

34-
export const ViewAlertPage = withRouter(({ history }) => (
35-
<EuiPageBody data-test-subj="dataPluginExplorerHome">
36-
<EuiPageHeader>
37-
<EuiPageHeaderSection>
38-
<EuiTitle size="l">
39-
<h1>Welcome to the Alerting plugin example</h1>
40-
</EuiTitle>
41-
</EuiPageHeaderSection>
42-
</EuiPageHeader>
43-
<EuiPageContent>
44-
<EuiPageContentHeader>
45-
<EuiPageContentHeaderSection>
46-
<EuiTitle>
47-
<h2>Documentation links</h2>
48-
</EuiTitle>
49-
</EuiPageContentHeaderSection>
50-
</EuiPageContentHeader>
51-
<EuiPageContentBody>
52-
<EuiText>
53-
<h2>Plugin Structure</h2>
54-
<p>
55-
This example solution has both `server` and a `public` plugins. The `server` handles
56-
registration of the AlertType, while the `public` handles registration of the
57-
navigation.
58-
</p>
59-
</EuiText>
60-
</EuiPageContentBody>
61-
</EuiPageContent>
62-
</EuiPageBody>
63-
));
26+
type Props = RouteComponentProps & {
27+
http: CoreStart['http'];
28+
id: string;
29+
};
30+
export const ViewAlertPage = withRouter(({ http, id }: Props) => {
31+
const [alert, setAlert] = useState<Alert | null>(null);
32+
const [alertState, setAlertState] = useState<AlertTaskState | null>(null);
33+
34+
useEffect(() => {
35+
if (!alert) {
36+
http.get(`/api/alert/${id}`).then(setAlert);
37+
}
38+
if (!alertState) {
39+
http.get(`/api/alert/${id}/state`).then(setAlertState);
40+
}
41+
}, [alert, alertState, http, id]);
42+
43+
return alert && alertState ? (
44+
<EuiText>
45+
<h2>Alert JSON</h2>
46+
<p>{JSON.stringify(alert)}</p>
47+
<p>{JSON.stringify(alertState)}</p>
48+
</EuiText>
49+
) : (
50+
<EuiLoadingKibana size="xl" />
51+
);
52+
});

x-pack/plugins/alerting/common/alert_navigation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { JsonObject } from '../../infra/common/typed_json';
88
export interface AlertUrlNavigation {
9-
url: string;
9+
path: string;
1010
}
1111
export interface AlertStateNavigation {
1212
state: JsonObject;

x-pack/plugins/alerting/public/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class AlertingPublicPlugin implements Plugin<PluginSetupContract, PluginS
4848
if (this.alertNavigationRegistry!.has(alert.consumer, alertType)) {
4949
const navigationHandler = this.alertNavigationRegistry!.get(alert.consumer, alertType);
5050
const state = navigationHandler(alert, alertType);
51-
return typeof state === 'string' ? { url: state } : { state };
51+
return typeof state === 'string' ? { path: state } : { state };
5252
}
5353
},
5454
};

x-pack/plugins/triggers_actions_ui/public/application/sections/alert_details/components/view_in_app.tsx

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ interface ViewInAppProps {
2121
alert: Alert;
2222
}
2323

24-
const NO_NAVIGATION = 'NO_NAVIGATION';
24+
const NO_NAVIGATION = false;
2525

26-
type AlertNavigationLoadingState = AlertNavigation | 'NO_NAVIGATION' | null;
26+
type AlertNavigationLoadingState = AlertNavigation | false | null;
2727

2828
export const ViewInApp: React.FunctionComponent<ViewInAppProps> = ({ alert }) => {
2929
const { navigateToApp, alerting } = useAppDependencies();
@@ -53,36 +53,24 @@ export const ViewInApp: React.FunctionComponent<ViewInAppProps> = ({ alert }) =>
5353
);
5454
};
5555

56-
function hasNavigation(alertNavigation: AlertNavigationLoadingState) {
57-
return hasNavigationState(alertNavigation) || hasNavigationUrl(alertNavigation);
58-
}
59-
60-
function hasNavigationState(
61-
alertNavigation: AlertNavigationLoadingState
62-
): alertNavigation is AlertStateNavigation {
63-
return alertNavigation ? alertNavigation.hasOwnProperty('state') : false;
64-
}
65-
66-
function hasNavigationUrl(
56+
function hasNavigation(
6757
alertNavigation: AlertNavigationLoadingState
68-
): alertNavigation is AlertUrlNavigation {
69-
return alertNavigation ? alertNavigation.hasOwnProperty('url') : false;
58+
): alertNavigation is AlertStateNavigation | AlertUrlNavigation {
59+
return alertNavigation
60+
? alertNavigation.hasOwnProperty('state') || alertNavigation.hasOwnProperty('path')
61+
: NO_NAVIGATION;
7062
}
7163

7264
function getNavigationHandler(
7365
alertNavigation: AlertNavigationLoadingState,
7466
alert: Alert,
7567
navigateToApp: CoreStart['application']['navigateToApp']
7668
): object {
77-
if (hasNavigationState(alertNavigation)) {
78-
return {
79-
onClick: () => {
80-
navigateToApp(alert.consumer, { state: alertNavigation.state });
81-
},
82-
};
83-
}
84-
if (hasNavigationUrl(alertNavigation)) {
85-
return { href: alertNavigation.url };
86-
}
87-
return {};
69+
return hasNavigation(alertNavigation)
70+
? {
71+
onClick: () => {
72+
navigateToApp(alert.consumer, alertNavigation);
73+
},
74+
}
75+
: {};
8876
}

x-pack/test/functional_with_es_ssl/fixtures/plugins/alerts/public/plugin.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,9 @@ export interface AlertingExamplePublicSetupDeps {
1818
export class AlertingFixturePlugin implements Plugin<Setup, Start, AlertingExamplePublicSetupDeps> {
1919
public setup(core: CoreSetup, { alerting }: AlertingExamplePublicSetupDeps) {
2020
alerting.registerNavigation(
21-
'test.noop',
2221
'consumer.noop',
23-
(alert: SanitizedAlert, alertType: AlertType) => ({
24-
state: {
25-
// LOLs
26-
alert: JSON.parse(JSON.stringify(alert)),
27-
alertType: JSON.parse(JSON.stringify(alertType)),
28-
},
29-
})
22+
'test.noop',
23+
(alert: SanitizedAlert, alertType: AlertType) => `/alert/${alert.id}`
3024
);
3125
}
3226

0 commit comments

Comments
 (0)