/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useCallback, useMemo } from 'react'; import { EuiPanel, EuiSpacer, EuiTitle, EuiFlexItem, EuiFlexGroup } 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'; const mapLayerForOption = ({ layerId, name }: VectorLayer) => ({ text: name, value: layerId, }); const mapFieldForOption = ({ description, name }: FileLayerField) => ({ text: description, value: name, }); export type DefaultMapOptionsProps = { getServiceSettings: () => Promise; } & VisOptionsProps; function DefaultMapOptions(props: DefaultMapOptionsProps) { const { getServiceSettings, stateParams, vis, setValue } = props; const vectorLayers = vis.type.editorConfig.collections.vectorLayers; const vectorLayerOptions = useMemo(() => vectorLayers.map(mapLayerForOption), [vectorLayers]); const fieldOptions = useMemo( () => ((stateParams.selectedLayer && stateParams.selectedLayer.fields) || []).map( mapFieldForOption ), [stateParams.selectedLayer] ); 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 setField = useCallback( (paramName: 'selectedJoinField', value: FileLayerField['name']) => { if (stateParams.selectedLayer) { setValue( paramName, stateParams.selectedLayer.fields.find((f) => f.name === value) ); } }, [setValue, stateParams.selectedLayer] ); return (

); } export { DefaultMapOptions };