forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded_map.tsx
228 lines (201 loc) · 7.24 KB
/
embedded_map.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* 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 { EuiLink, EuiText } from '@elastic/eui';
import React, { useEffect, useState } from 'react';
import { createPortalNode, InPortal } from 'react-reverse-portal';
import styled, { css } from 'styled-components';
import { npStart } from 'ui/new_platform';
import {
EmbeddablePanel,
ErrorEmbeddable,
} from '../../../../../../../src/plugins/embeddable/public';
import { DEFAULT_INDEX_KEY } from '../../../../../../plugins/siem/common/constants';
import { getIndexPatternTitleIdMapping } from '../../hooks/api/helpers';
import { useIndexPatterns } from '../../hooks/use_index_patterns';
import { Loader } from '../loader';
import { displayErrorToast, useStateToaster } from '../toasters';
import { Embeddable } from './embeddable';
import { EmbeddableHeader } from './embeddable_header';
import { createEmbeddable, findMatchingIndexPatterns } from './embedded_map_helpers';
import { IndexPatternsMissingPrompt } from './index_patterns_missing_prompt';
import { MapToolTip } from './map_tool_tip/map_tool_tip';
import * as i18n from './translations';
import { SetQuery } from './types';
import { MapEmbeddable } from '../../../../../plugins/maps/public';
import { Query, Filter } from '../../../../../../../src/plugins/data/public';
import { useKibana, useUiSetting$ } from '../../lib/kibana';
import { getSavedObjectFinder } from '../../../../../../../src/plugins/saved_objects/public';
interface EmbeddableMapProps {
maintainRatio?: boolean;
}
const EmbeddableMap = styled.div.attrs(() => ({
className: 'siemEmbeddable__map',
}))<EmbeddableMapProps>`
.embPanel {
border: none;
box-shadow: none;
}
.mapToolbarOverlay__button {
display: none;
}
${({ maintainRatio }) =>
maintainRatio &&
css`
padding-top: calc(3 / 4 * 100%); /* 4:3 (standard) ratio */
position: relative;
@media only screen and (min-width: ${({ theme }) => theme.eui.euiBreakpoints.m}) {
padding-top: calc(9 / 32 * 100%); /* 32:9 (ultra widescreen) ratio */
}
@media only screen and (min-width: 1441px) and (min-height: 901px) {
padding-top: calc(9 / 21 * 100%); /* 21:9 (ultrawide) ratio */
}
.embPanel {
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
}
`}
`;
EmbeddableMap.displayName = 'EmbeddableMap';
export interface EmbeddedMapProps {
query: Query;
filters: Filter[];
startDate: number;
endDate: number;
setQuery: SetQuery;
}
export const EmbeddedMapComponent = ({
endDate,
filters,
query,
setQuery,
startDate,
}: EmbeddedMapProps) => {
const [embeddable, setEmbeddable] = React.useState<MapEmbeddable | undefined | ErrorEmbeddable>(
undefined
);
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [isIndexError, setIsIndexError] = useState(false);
const [, dispatchToaster] = useStateToaster();
const [loadingKibanaIndexPatterns, kibanaIndexPatterns] = useIndexPatterns();
const [siemDefaultIndices] = useUiSetting$<string[]>(DEFAULT_INDEX_KEY);
// This portalNode provided by react-reverse-portal allows us re-parent the MapToolTip within our
// own component tree instead of the embeddables (default). This is necessary to have access to
// the Redux store, theme provider, etc, which is required to register and un-register the draggable
// Search InPortal/OutPortal for implementation touch points
const portalNode = React.useMemo(() => createPortalNode(), []);
const { services } = useKibana();
// Initial Load useEffect
useEffect(() => {
let isSubscribed = true;
async function setupEmbeddable() {
// Ensure at least one `siem:defaultIndex` kibana index pattern exists before creating embeddable
const matchingIndexPatterns = findMatchingIndexPatterns({
kibanaIndexPatterns,
siemDefaultIndices,
});
if (matchingIndexPatterns.length === 0 && isSubscribed) {
setIsLoading(false);
setIsIndexError(true);
return;
}
// Create & set Embeddable
try {
const embeddableObject = await createEmbeddable(
filters,
getIndexPatternTitleIdMapping(matchingIndexPatterns),
query,
startDate,
endDate,
setQuery,
portalNode,
services.embeddable
);
if (isSubscribed) {
setEmbeddable(embeddableObject);
}
} catch (e) {
if (isSubscribed) {
displayErrorToast(i18n.ERROR_CREATING_EMBEDDABLE, [e.message], dispatchToaster);
setIsError(true);
}
}
if (isSubscribed) {
setIsLoading(false);
}
}
if (!loadingKibanaIndexPatterns) {
setupEmbeddable();
}
return () => {
isSubscribed = false;
};
}, [loadingKibanaIndexPatterns, kibanaIndexPatterns]);
// queryExpression updated useEffect
useEffect(() => {
if (embeddable != null) {
embeddable.updateInput({ query });
}
}, [query]);
useEffect(() => {
if (embeddable != null) {
embeddable.updateInput({ filters });
}
}, [filters]);
// DateRange updated useEffect
useEffect(() => {
if (embeddable != null && startDate != null && endDate != null) {
const timeRange = {
from: new Date(startDate).toISOString(),
to: new Date(endDate).toISOString(),
};
embeddable.updateInput({ timeRange });
}
}, [startDate, endDate]);
return isError ? null : (
<Embeddable>
<EmbeddableHeader title={i18n.EMBEDDABLE_HEADER_TITLE}>
<EuiText size="xs">
<EuiLink
href={`${services.docLinks.ELASTIC_WEBSITE_URL}guide/en/siem/guide/${services.docLinks.DOC_LINK_VERSION}/conf-map-ui.html`}
target="_blank"
>
{i18n.EMBEDDABLE_HEADER_HELP}
</EuiLink>
</EuiText>
</EmbeddableHeader>
<InPortal node={portalNode}>
<MapToolTip />
</InPortal>
<EmbeddableMap maintainRatio={!isIndexError}>
{embeddable != null ? (
<EmbeddablePanel
data-test-subj="embeddable-panel"
embeddable={embeddable}
getActions={services.uiActions.getTriggerCompatibleActions}
getEmbeddableFactory={npStart.plugins.embeddable.getEmbeddableFactory}
getAllEmbeddableFactories={npStart.plugins.embeddable.getEmbeddableFactories}
notifications={services.notifications}
overlays={services.overlays}
inspector={services.inspector}
application={services.application}
SavedObjectFinder={getSavedObjectFinder(services.savedObjects, services.uiSettings)}
/>
) : !isLoading && isIndexError ? (
<IndexPatternsMissingPrompt data-test-subj="missing-prompt" />
) : (
<Loader data-test-subj="loading-panel" overlay size="xl" />
)}
</EmbeddableMap>
</Embeddable>
);
};
EmbeddedMapComponent.displayName = 'EmbeddedMapComponent';
export const EmbeddedMap = React.memo(EmbeddedMapComponent);
EmbeddedMap.displayName = 'EmbeddedMap';