1import React from 'react'; 2import { Seg } from './Seg'; 3import { toSelectableValue } from './toSelectableValue'; 4 5const DEFAULT_POLICY = 'default'; 6 7// we use the value "default" as a magic-value, it means 8// we use the default retention-policy. 9// unfortunately, IF the user has a retention-policy named "default", 10// and it is not the default-retention-policy in influxdb, 11// bad things will happen. 12// https://github.com/grafana/grafana/issues/4347 :-( 13// FIXME: we could maybe at least detect here that problem-is-happening, 14// and show an error message or something. 15// unfortunately, currently the ResponseParser does not return the 16// is-default info for the retention-policies, so that should change first. 17 18type Props = { 19 onChange: (policy: string | undefined, measurement: string | undefined) => void; 20 policy: string | undefined; 21 measurement: string | undefined; 22 getPolicyOptions: () => Promise<string[]>; 23 getMeasurementOptions: (filter: string) => Promise<string[]>; 24}; 25 26export const FromSection = ({ 27 policy, 28 measurement, 29 onChange, 30 getPolicyOptions, 31 getMeasurementOptions, 32}: Props): JSX.Element => { 33 const handlePolicyLoadOptions = async () => { 34 const allPolicies = await getPolicyOptions(); 35 // if `default` does not exist in the list of policies, we add it 36 const allPoliciesWithDefault = allPolicies.some((p) => p === 'default') 37 ? allPolicies 38 : [DEFAULT_POLICY, ...allPolicies]; 39 40 return allPoliciesWithDefault.map(toSelectableValue); 41 }; 42 43 const handleMeasurementLoadOptions = async (filter: string) => { 44 const allMeasurements = await getMeasurementOptions(filter); 45 return allMeasurements.map(toSelectableValue); 46 }; 47 48 return ( 49 <> 50 <Seg 51 allowCustomValue 52 value={policy ?? 'using default policy'} 53 loadOptions={handlePolicyLoadOptions} 54 onChange={(v) => { 55 onChange(v.value, measurement); 56 }} 57 /> 58 <Seg 59 allowCustomValue 60 value={measurement ?? 'select measurement'} 61 loadOptions={handleMeasurementLoadOptions} 62 filterByLoadOptions 63 onChange={(v) => { 64 onChange(policy, v.value); 65 }} 66 /> 67 </> 68 ); 69}; 70