import React, { FC, useCallback } from 'react'; import { FieldNamePickerConfigSettings, StandardEditorProps, StandardEditorsRegistryItem } from '@grafana/data'; import { InlineField, InlineFieldRow, RadioButtonGroup } from '@grafana/ui'; import { FieldNamePicker } from '@grafana/ui/src/components/MatchersUI/FieldNamePicker'; import { getPublicOrAbsoluteUrl, ResourceFolderName } from '..'; import { MediaType, ResourceDimensionConfig, ResourceDimensionMode, ResourceDimensionOptions, ResourcePickerSize, } from '../types'; import { ResourcePicker } from './ResourcePicker'; const resourceOptions = [ { label: 'Fixed', value: ResourceDimensionMode.Fixed, description: 'Fixed value' }, { label: 'Field', value: ResourceDimensionMode.Field, description: 'Use a string field result' }, // { label: 'Mapping', value: ResourceDimensionMode.Mapping, description: 'Map the results of a value to an svg' }, ]; const dummyFieldSettings: StandardEditorsRegistryItem = { settings: {}, } as any; export const ResourceDimensionEditor: FC< StandardEditorProps > = (props) => { const { value, context, onChange, item } = props; const labelWidth = 9; const onModeChange = useCallback( (mode) => { onChange({ ...value, mode, }); }, [onChange, value] ); const onFieldChange = useCallback( (field) => { onChange({ ...value, field, }); }, [onChange, value] ); const onFixedChange = useCallback( (fixed?: string) => { onChange({ ...value, fixed: fixed ?? '', }); }, [onChange, value] ); const onClear = (event: React.MouseEvent) => { event.stopPropagation(); onChange({ mode: ResourceDimensionMode.Fixed, fixed: '', field: '' }); }; const mode = value?.mode ?? ResourceDimensionMode.Fixed; const showSourceRadio = item.settings?.showSourceRadio ?? true; const mediaType = item.settings?.resourceType ?? MediaType.Icon; const folderName = item.settings?.folderName ?? ResourceFolderName.Icon; let srcPath = ''; if (mediaType === MediaType.Icon) { if (value?.fixed) { srcPath = getPublicOrAbsoluteUrl(value.fixed); } else if (item.settings?.placeholderValue) { srcPath = getPublicOrAbsoluteUrl(item.settings.placeholderValue); } } return ( <> {showSourceRadio && ( )} {mode !== ResourceDimensionMode.Fixed && ( )} {mode === ResourceDimensionMode.Fixed && ( )} {mode === ResourceDimensionMode.Mapping && (
TODO mappings editor!
)} ); }; export function niceName(value?: string): string | undefined { if (!value) { return undefined; } const idx = value.lastIndexOf('/'); if (idx > 0) { return value.substring(idx + 1); } return value; }