-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
render_app.tsx
152 lines (141 loc) · 4.92 KB
/
render_app.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Redirect, RouteComponentProps } from 'react-router-dom';
import { Router, Routes, Route } from '@kbn/shared-ux-router';
import { i18n } from '@kbn/i18n';
import type { CoreStart, AppMountParameters } from '@kbn/core/public';
import { ExitFullScreenButtonKibanaProvider } from '@kbn/shared-ux-button-exit-full-screen';
import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
import { FormattedRelative } from '@kbn/i18n-react';
import type { SavedObjectTaggingPluginStart } from '@kbn/saved-objects-tagging-plugin/public';
import { TableListViewKibanaProvider } from '@kbn/content-management-table-list-view-table';
import {
getCoreChrome,
getAnalytics,
getCoreI18n,
getTheme,
getMapsCapabilities,
getEmbeddableService,
getDocLinks,
getCore,
} from './kibana_services';
import { ListPage, MapPage } from './routes';
import { APP_ID } from '../common/constants';
import { registerLayerWizards } from './classes/layers/wizards/load_layer_wizards';
import { MapSerializedState } from './react_embeddable/types';
function setAppChrome() {
if (!getMapsCapabilities().save) {
getCoreChrome().setBadge({
text: i18n.translate('xpack.maps.badge.readOnly.text', {
defaultMessage: 'Read only',
}),
tooltip: i18n.translate('xpack.maps.badge.readOnly.tooltip', {
defaultMessage: 'Unable to save maps',
}),
iconType: 'glasses',
});
}
const mapUrl = getDocLinks().links.maps.guide;
getCoreChrome().setHelpExtension({
appName: 'Maps',
links: [
{
linkType: 'documentation',
href: `${mapUrl}`,
},
{
linkType: 'github',
title: '[Maps]',
labels: ['Team:Geo'],
},
],
});
}
export async function renderApp(
{ element, history, onAppLeave, setHeaderActionMenu, theme$ }: AppMountParameters,
{
coreStart,
AppUsageTracker,
savedObjectsTagging,
}: {
coreStart: CoreStart;
savedObjectsTagging?: SavedObjectTaggingPluginStart;
AppUsageTracker: React.FC<{ children: React.ReactNode }>;
}
) {
const stateTransfer = getEmbeddableService().getStateTransfer();
registerLayerWizards();
setAppChrome();
function renderMapApp(routeProps: RouteComponentProps<{ savedMapId?: string }>) {
const { embeddableId, originatingApp, valueInput, originatingPath } =
stateTransfer.getIncomingEditorState(APP_ID) || {};
let mapSerializedState: MapSerializedState | undefined;
if (routeProps.match.params.savedMapId) {
mapSerializedState = {
savedObjectId: routeProps.match.params.savedMapId,
};
} else if (valueInput) {
mapSerializedState = valueInput as MapSerializedState;
}
return (
<ExitFullScreenButtonKibanaProvider coreStart={getCore()}>
<MapPage
mapSerializedState={mapSerializedState}
embeddableId={embeddableId}
onAppLeave={onAppLeave}
setHeaderActionMenu={setHeaderActionMenu}
stateTransfer={stateTransfer}
originatingApp={originatingApp}
originatingPath={originatingPath}
history={history}
key={routeProps.match.params.savedMapId ? routeProps.match.params.savedMapId : 'new'}
/>
</ExitFullScreenButtonKibanaProvider>
);
}
render(
<KibanaRenderContextProvider analytics={getAnalytics()} i18n={getCoreI18n()} theme={getTheme()}>
<AppUsageTracker>
<TableListViewKibanaProvider
{...{
core: coreStart,
savedObjectsTagging,
FormattedRelative,
}}
>
<Router history={history}>
<Routes>
<Route path={`/map/:savedMapId`} render={renderMapApp} />
<Route exact path={`/map`} render={renderMapApp} />
// Redirect other routes to list, or if hash-containing, their non-hash equivalents
<Route
path={``}
render={({ location: { pathname, hash } }) => {
if (hash) {
// Remove leading hash
const newPath = hash.substr(1);
return <Redirect to={newPath} />;
} else if (pathname === '/' || pathname === '') {
return <ListPage history={history} stateTransfer={stateTransfer} />;
} else {
return <Redirect to="/" />;
}
}}
/>
</Routes>
</Router>
</TableListViewKibanaProvider>
</AppUsageTracker>
</KibanaRenderContextProvider>,
element
);
return () => {
unmountComponentAtNode(element);
};
}