/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import './map_choice_options.scss'; import React, { useCallback, useMemo } from 'react'; import { EuiPanel, EuiSpacer, EuiText, EuiTitle, EuiCheckableCard, EuiFlexItem, EuiFlexGroup, EuiTextColor, } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; import { VisOptionsProps } from 'src/plugins/vis_default_editor/public'; import { FileLayerField, VectorLayer, IServiceSettings } from '../../../maps_legacy/public'; import { SelectOption, SwitchOption } from '../../../charts/public'; import { RegionMapVisParams } from '../../../maps_legacy/public'; import { DEFAULT_MAP_CHOICE, CUSTOM_MAP_CHOICE } from '../../common'; const mapLayerForOption = ({ layerId, name }: VectorLayer) => ({ text: name, value: layerId, }); const mapFieldForOption = ({ description, name }: FileLayerField) => ({ text: description, value: name, }); const mapCustomJoinFieldForOption = ({ description, name }: FileLayerField) => ({ label: name, text: description, value: name, }); export type MapChoiceOptionsProps = { getServiceSettings: () => Promise; } & VisOptionsProps; function MapChoiceOptions(props: MapChoiceOptionsProps) { const { getServiceSettings, stateParams, vis, setValue } = props; const vectorLayers = vis.type.editorConfig.collections.vectorLayers; const customVectorLayers = vis.type.editorConfig.collections.customVectorLayers; const vectorLayerOptions = useMemo(() => vectorLayers.map(mapLayerForOption), [vectorLayers]); const customVectorLayerOptions = useMemo(() => customVectorLayers.map(mapLayerForOption), [ customVectorLayers, ]); const fieldOptions = useMemo( () => ((stateParams.selectedLayer && stateParams.selectedLayer.fields) || []).map( mapFieldForOption ), [stateParams.selectedLayer] ); const customFieldOptions = useMemo( () => ((stateParams.selectedCustomLayer && stateParams.selectedCustomLayer.fields) || []).map( mapCustomJoinFieldForOption ), [stateParams.selectedCustomLayer] ); const selectDefaultVectorMap = () => { setValue('layerChosenByUser', DEFAULT_MAP_CHOICE); }; const selectCustomVectorMap = () => { setValue('layerChosenByUser', CUSTOM_MAP_CHOICE); }; const setEmsHotLink = useCallback( async (layer: VectorLayer) => { const serviceSettings = await getServiceSettings(); const emsHotLink = await serviceSettings.getEMSHotLink(layer); setValue('emsHotLink', emsHotLink); }, [setValue, getServiceSettings] ); const setLayer = useCallback( async (paramName: 'selectedLayer', value: VectorLayer['layerId']) => { const newLayer = vectorLayers.find(({ layerId }: VectorLayer) => layerId === value); if (newLayer) { setValue(paramName, newLayer); setValue('selectedJoinField', newLayer.fields[0]); setEmsHotLink(newLayer); } }, [vectorLayers, setEmsHotLink, setValue] ); const setCustomLayer = useCallback( async (paramName: 'selectedCustomLayer', value: VectorLayer['layerId']) => { const newLayer = customVectorLayers.find(({ layerId }: VectorLayer) => layerId === value); if (newLayer) { setValue(paramName, newLayer); setValue('selectedJoinField', newLayer.fields[0]); } }, [customVectorLayers, setValue] ); const setCustomJoinField = useCallback( async (paramName: 'selectedCustomJoinField', value) => { if (stateParams.selectedCustomLayer) { setValue( paramName, stateParams.selectedCustomLayer.fields.find((f) => f.name === value) ); } }, [setValue, stateParams.selectedCustomLayer] ); const setField = useCallback( (paramName: 'selectedJoinField', value: FileLayerField['name']) => { if (stateParams.selectedLayer) { setValue( paramName, stateParams.selectedLayer.fields.find((f) => f.name === value) ); } }, [setValue, stateParams.selectedLayer] ); return (

Choose a vector map layer {DEFAULT_MAP_CHOICE === stateParams.layerChosenByUser ? ( ) : ( )}
); } export { MapChoiceOptions };