-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
plugin.tsx
96 lines (85 loc) · 3.39 KB
/
plugin.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import * as Rx from 'rxjs';
import { catchError, takeUntil } from 'rxjs/operators';
import ReactDOM from 'react-dom';
import React from 'react';
import moment from 'moment';
import { I18nProvider } from '@kbn/i18n/react';
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public';
import { NewsfeedPluginBrowserConfig, NewsfeedPluginStartDependencies } from './types';
import { NewsfeedNavButton } from './components/newsfeed_header_nav_button';
import { getApi, NewsfeedApi, NewsfeedApiEndpoint } from './lib/api';
export type NewsfeedPublicPluginSetup = ReturnType<NewsfeedPublicPlugin['setup']>;
export type NewsfeedPublicPluginStart = ReturnType<NewsfeedPublicPlugin['start']>;
export class NewsfeedPublicPlugin
implements Plugin<NewsfeedPublicPluginSetup, NewsfeedPublicPluginStart>
{
private readonly kibanaVersion: string;
private readonly config: NewsfeedPluginBrowserConfig;
private readonly stop$ = new Rx.ReplaySubject(1);
constructor(initializerContext: PluginInitializerContext<NewsfeedPluginBrowserConfig>) {
this.kibanaVersion = initializerContext.env.packageInfo.version;
const config = initializerContext.config.get();
this.config = Object.freeze({
...config,
// We need wrap them in moment.duration because exposeToBrowser stringifies it.
mainInterval: moment.duration(config.mainInterval),
fetchInterval: moment.duration(config.fetchInterval),
});
}
public setup(core: CoreSetup) {
return {};
}
public start(core: CoreStart, { screenshotMode }: NewsfeedPluginStartDependencies) {
const isScreenshotMode = screenshotMode.isScreenshotMode();
const api = this.createNewsfeedApi(this.config, NewsfeedApiEndpoint.KIBANA, isScreenshotMode);
core.chrome.navControls.registerRight({
order: 1000,
mount: (target) => this.mount(api, target),
});
return {
createNewsFeed$: (endpoint: NewsfeedApiEndpoint) => {
const config = Object.assign({}, this.config, {
service: {
...this.config.service,
pathTemplate: `/${endpoint}/v{VERSION}.json`,
},
});
const { fetchResults$ } = this.createNewsfeedApi(config, endpoint, isScreenshotMode);
return fetchResults$;
},
};
}
public stop() {
this.stop$.next();
}
private createNewsfeedApi(
config: NewsfeedPluginBrowserConfig,
newsfeedId: NewsfeedApiEndpoint,
isScreenshotMode: boolean
): NewsfeedApi {
const api = getApi(config, this.kibanaVersion, newsfeedId, isScreenshotMode);
return {
markAsRead: api.markAsRead,
fetchResults$: api.fetchResults$.pipe(
takeUntil(this.stop$), // stop the interval when stop method is called
catchError(() => Rx.of(null)) // do not throw error
),
};
}
private mount(api: NewsfeedApi, targetDomElement: HTMLElement) {
ReactDOM.render(
<I18nProvider>
<NewsfeedNavButton newsfeedApi={api} />
</I18nProvider>,
targetDomElement
);
return () => ReactDOM.unmountComponentAtNode(targetDomElement);
}
}