Files
Giordano Ricci bb45f5fedc Elasticsearch: Migrate queryeditor to React (#28033)
Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-12-04 14:29:40 +00:00

19 lines
756 B
TypeScript

import { useMemo } from 'react';
import { useQuery } from '../components/QueryEditor/ElasticsearchQueryContext';
import { BucketAggregation } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
import { MetricAggregation } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
const toId = <T extends { id: unknown }>(e: T): T['id'] => e.id;
const toInt = (idString: string) => parseInt(idString, 10);
export const useNextId = (): MetricAggregation['id'] | BucketAggregation['id'] => {
const { metrics, bucketAggs } = useQuery();
return useMemo(
() =>
(Math.max(...[...(metrics?.map(toId) || ['0']), ...(bucketAggs?.map(toId) || ['0'])].map(toInt)) + 1).toString(),
[metrics, bucketAggs]
);
};