import { css } from '@emotion/css'; import React, { useCallback, useEffect, useState } from 'react'; import { SelectableValue } from '@grafana/data'; import { fuzzyMatch, InlineField, InlineFieldRow, Input, Select } from '@grafana/ui'; import { notifyApp } from 'app/core/actions'; import { createErrorNotification } from 'app/core/copy/appNotification'; import { dispatch } from 'app/store/store'; import { JaegerDatasource } from '../datasource'; import { JaegerQuery } from '../types'; import { transformToLogfmt } from '../util'; import { AdvancedOptions } from './AdvancedOptions'; type Props = { datasource: JaegerDatasource; query: JaegerQuery; onChange: (value: JaegerQuery) => void; }; export const ALL_OPERATIONS_KEY = 'All'; const allOperationsOption: SelectableValue = { label: ALL_OPERATIONS_KEY, value: undefined, }; export function SearchForm({ datasource, query, onChange }: Props) { const [serviceOptions, setServiceOptions] = useState>>(); const [operationOptions, setOperationOptions] = useState>>(); const [isLoading, setIsLoading] = useState<{ services: boolean; operations: boolean; }>({ services: false, operations: false, }); const loadOptions = useCallback( async (url: string, loaderOfType: string, query = ''): Promise>> => { setIsLoading((prevValue) => ({ ...prevValue, [loaderOfType]: true })); try { const values: string[] | null = await datasource.metadataRequest(url); if (!values) { return [{ label: `No ${loaderOfType} found`, value: `No ${loaderOfType} found` }]; } const options: SelectableValue[] = values.sort().map((option) => ({ label: option, value: option, })); const filteredOptions = options.filter((item) => (item.value ? fuzzyMatch(item.value, query).found : false)); return filteredOptions; } catch (error) { dispatch(notifyApp(createErrorNotification('Error', error))); return []; } finally { setIsLoading((prevValue) => ({ ...prevValue, [loaderOfType]: false })); } }, [datasource] ); useEffect(() => { const getServices = async () => { const services = await loadOptions('/api/services', 'services'); setServiceOptions(services); }; getServices(); }, [datasource, loadOptions]); useEffect(() => { const getOperations = async () => { const operations = await loadOptions( `/api/services/${encodeURIComponent(query.service!)}/operations`, 'operations' ); setOperationOptions([allOperationsOption, ...operations]); }; if (query.service) { getOperations(); } }, [datasource, query.service, loadOptions]); return (
loadOptions(`/api/services/${encodeURIComponent(query.service!)}/operations`, 'operations') } isLoading={isLoading.operations} value={operationOptions?.find((v) => v.value === query.operation) || null} onChange={(v) => onChange({ ...query, operation: v?.value! || undefined, }) } menuPlacement="bottom" isClearable aria-label={'select-operation-name'} /> onChange({ ...query, tags: v.currentTarget.value, }) } />
); } export default SearchForm;