import { css } from '@emotion/css'; import React, { FC, useMemo, useState, useEffect } from 'react'; import { StandardEditorProps, SelectableValue, GrafanaTheme2 } from '@grafana/data'; import { Alert, Select, stylesFactory, useTheme2 } from '@grafana/ui'; import { COUNTRIES_GAZETTEER_PATH, Gazetteer, getGazetteer } from '../gazetteer/gazetteer'; const defaultPaths: Array> = [ { label: 'Countries', description: 'Lookup countries by name, two letter code, or three letter code', value: COUNTRIES_GAZETTEER_PATH, }, { label: 'USA States', description: 'Lookup states by name or 2 ', value: 'public/gazetteer/usa-states.json', }, { label: 'Airports', description: 'Lookup airports by id or code', value: 'public/gazetteer/airports.geojson', }, ]; export interface GazetteerPathEditorConfigSettings { options?: Array>; } export const GazetteerPathEditor: FC> = ({ value, onChange, context, item, }) => { const styles = getStyles(useTheme2()); const [gaz, setGaz] = useState(); const settings = item.settings as any; useEffect(() => { async function fetchData() { const p = await getGazetteer(value); setGaz(p); } fetchData(); }, [value, setGaz]); const { current, options } = useMemo(() => { let options = settings?.options ? [...settings.options] : [...defaultPaths]; let current = options?.find((f) => f.value === gaz?.path); if (!current && gaz) { current = { label: gaz.path, value: gaz.path, }; options.push(current); } return { options, current }; }, [gaz, settings?.options]); return ( <>