|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import React, { useEffect, useState, useRef } from 'react'; |
| 8 | +import uuid from 'uuid'; |
| 9 | +import styled from 'styled-components'; |
| 10 | + |
| 11 | +import { |
| 12 | + MapEmbeddable, |
| 13 | + MapEmbeddableInput, |
| 14 | + // eslint-disable-next-line @kbn/eslint/no-restricted-paths |
| 15 | +} from '../../../../../../maps/public/embeddable'; |
| 16 | +import { MAP_SAVED_OBJECT_TYPE } from '../../../../../../maps/common/constants'; |
| 17 | +import { useKibana } from '../../../../../../../../src/plugins/kibana_react/public'; |
| 18 | +import { |
| 19 | + ErrorEmbeddable, |
| 20 | + ViewMode, |
| 21 | + isErrorEmbeddable, |
| 22 | +} from '../../../../../../../../src/plugins/embeddable/public'; |
| 23 | +import { getLayerList } from './LayerList'; |
| 24 | +import { useUrlParams } from '../../../../hooks/useUrlParams'; |
| 25 | +import { RenderTooltipContentParams } from '../../../../../../maps/public'; |
| 26 | +import { MapToolTip } from './MapToolTip'; |
| 27 | +import { useMapFilters } from './useMapFilters'; |
| 28 | +import { EmbeddableStart } from '../../../../../../../../src/plugins/embeddable/public'; |
| 29 | + |
| 30 | +const EmbeddedPanel = styled.div` |
| 31 | + z-index: auto; |
| 32 | + flex: 1; |
| 33 | + display: flex; |
| 34 | + flex-direction: column; |
| 35 | + height: 100%; |
| 36 | + position: relative; |
| 37 | + .embPanel__content { |
| 38 | + display: flex; |
| 39 | + flex: 1 1 100%; |
| 40 | + z-index: 1; |
| 41 | + min-height: 0; // Absolute must for Firefox to scroll contents |
| 42 | + } |
| 43 | + &&& .mapboxgl-canvas { |
| 44 | + animation: none !important; |
| 45 | + } |
| 46 | +`; |
| 47 | + |
| 48 | +interface KibanaDeps { |
| 49 | + embeddable: EmbeddableStart; |
| 50 | +} |
| 51 | +export function EmbeddedMapComponent() { |
| 52 | + const { urlParams } = useUrlParams(); |
| 53 | + |
| 54 | + const { start, end, serviceName } = urlParams; |
| 55 | + |
| 56 | + const mapFilters = useMapFilters(); |
| 57 | + |
| 58 | + const [embeddable, setEmbeddable] = useState< |
| 59 | + MapEmbeddable | ErrorEmbeddable | undefined |
| 60 | + >(); |
| 61 | + |
| 62 | + const embeddableRoot: React.RefObject<HTMLDivElement> = useRef< |
| 63 | + HTMLDivElement |
| 64 | + >(null); |
| 65 | + |
| 66 | + const { |
| 67 | + services: { embeddable: embeddablePlugin }, |
| 68 | + } = useKibana<KibanaDeps>(); |
| 69 | + |
| 70 | + if (!embeddablePlugin) { |
| 71 | + throw new Error('Embeddable start plugin not found'); |
| 72 | + } |
| 73 | + const factory: any = embeddablePlugin.getEmbeddableFactory( |
| 74 | + MAP_SAVED_OBJECT_TYPE |
| 75 | + ); |
| 76 | + |
| 77 | + const input: MapEmbeddableInput = { |
| 78 | + id: uuid.v4(), |
| 79 | + filters: mapFilters, |
| 80 | + refreshConfig: { |
| 81 | + value: 0, |
| 82 | + pause: false, |
| 83 | + }, |
| 84 | + viewMode: ViewMode.VIEW, |
| 85 | + isLayerTOCOpen: false, |
| 86 | + query: { |
| 87 | + query: 'transaction.type : "page-load"', |
| 88 | + language: 'kuery', |
| 89 | + }, |
| 90 | + ...(start && { |
| 91 | + timeRange: { |
| 92 | + from: new Date(start!).toISOString(), |
| 93 | + to: new Date(end!).toISOString(), |
| 94 | + }, |
| 95 | + }), |
| 96 | + hideFilterActions: true, |
| 97 | + }; |
| 98 | + |
| 99 | + function renderTooltipContent({ |
| 100 | + addFilters, |
| 101 | + closeTooltip, |
| 102 | + features, |
| 103 | + isLocked, |
| 104 | + getLayerName, |
| 105 | + loadFeatureProperties, |
| 106 | + loadFeatureGeometry, |
| 107 | + }: RenderTooltipContentParams) { |
| 108 | + const props = { |
| 109 | + addFilters, |
| 110 | + closeTooltip, |
| 111 | + isLocked, |
| 112 | + getLayerName, |
| 113 | + loadFeatureProperties, |
| 114 | + loadFeatureGeometry, |
| 115 | + }; |
| 116 | + |
| 117 | + return <MapToolTip {...props} features={features} />; |
| 118 | + } |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + if (embeddable != null && serviceName) { |
| 122 | + embeddable.updateInput({ filters: mapFilters }); |
| 123 | + } |
| 124 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 125 | + }, [mapFilters]); |
| 126 | + |
| 127 | + // DateRange updated useEffect |
| 128 | + useEffect(() => { |
| 129 | + if (embeddable != null && start != null && end != null) { |
| 130 | + const timeRange = { |
| 131 | + from: new Date(start).toISOString(), |
| 132 | + to: new Date(end).toISOString(), |
| 133 | + }; |
| 134 | + embeddable.updateInput({ timeRange }); |
| 135 | + } |
| 136 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 137 | + }, [start, end]); |
| 138 | + |
| 139 | + useEffect(() => { |
| 140 | + async function setupEmbeddable() { |
| 141 | + if (!factory) { |
| 142 | + throw new Error('Map embeddable not found.'); |
| 143 | + } |
| 144 | + const embeddableObject: any = await factory.create({ |
| 145 | + ...input, |
| 146 | + title: 'Visitors by region', |
| 147 | + }); |
| 148 | + |
| 149 | + if (embeddableObject && !isErrorEmbeddable(embeddableObject)) { |
| 150 | + embeddableObject.setRenderTooltipContent(renderTooltipContent); |
| 151 | + await embeddableObject.setLayerList(getLayerList()); |
| 152 | + } |
| 153 | + |
| 154 | + setEmbeddable(embeddableObject); |
| 155 | + } |
| 156 | + |
| 157 | + setupEmbeddable(); |
| 158 | + |
| 159 | + // we want this effect to execute exactly once after the component mounts |
| 160 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 161 | + }, []); |
| 162 | + |
| 163 | + // We can only render after embeddable has already initialized |
| 164 | + useEffect(() => { |
| 165 | + if (embeddableRoot.current && embeddable) { |
| 166 | + embeddable.render(embeddableRoot.current); |
| 167 | + } |
| 168 | + }, [embeddable, embeddableRoot]); |
| 169 | + |
| 170 | + return ( |
| 171 | + <EmbeddedPanel> |
| 172 | + <div |
| 173 | + data-test-subj="xpack.apm.regionMap.embeddedPanel" |
| 174 | + className="embPanel__content" |
| 175 | + ref={embeddableRoot} |
| 176 | + /> |
| 177 | + </EmbeddedPanel> |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +EmbeddedMapComponent.displayName = 'EmbeddedMap'; |
| 182 | + |
| 183 | +export const EmbeddedMap = React.memo(EmbeddedMapComponent); |
0 commit comments