Files
Ivana Huckova 20ec54f1f9 Elasticsearch: Use generated types in data source (#62753)
* Use generated types in frontend

* Export missing MetricAggregationWithMeta

* Remove not needed changes

* Update

* Fix lint

* Update comments
2023-02-03 12:04:12 -05:00

19 lines
620 B
TypeScript

import { useMemo } from 'react';
import { useQuery } from '../components/QueryEditor/ElasticsearchQueryContext';
import { MetricAggregation, BucketAggregation } from '../types';
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]
);
};