2023-06-07 15:27:33 +03:00
|
|
|
import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
|
|
|
|
|
import {
|
|
|
|
|
GetMetricQueryRange,
|
|
|
|
|
GetQueryResultsProps,
|
2023-10-08 23:21:17 +05:30
|
|
|
} from 'lib/dashboard/getQueryResults';
|
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
import { useQuery, UseQueryOptions, UseQueryResult } from 'react-query';
|
2023-06-07 15:27:33 +03:00
|
|
|
import { SuccessResponse } from 'types/api';
|
|
|
|
|
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
|
|
|
|
|
|
2023-06-16 13:38:39 +03:00
|
|
|
type UseGetQueryRange = (
|
2023-06-07 15:27:33 +03:00
|
|
|
requestData: GetQueryResultsProps,
|
|
|
|
|
options?: UseQueryOptions<SuccessResponse<MetricRangePayloadProps>, Error>,
|
2023-06-16 13:38:39 +03:00
|
|
|
) => UseQueryResult<SuccessResponse<MetricRangePayloadProps>, Error>;
|
2023-06-13 16:26:12 +05:30
|
|
|
|
2023-06-16 13:38:39 +03:00
|
|
|
export const useGetQueryRange: UseGetQueryRange = (requestData, options) => {
|
2023-06-13 16:26:12 +05:30
|
|
|
const queryKey = useMemo(() => {
|
|
|
|
|
if (options?.queryKey) {
|
2023-06-16 13:38:39 +03:00
|
|
|
return [...options.queryKey];
|
2023-06-13 16:26:12 +05:30
|
|
|
}
|
2023-06-16 13:38:39 +03:00
|
|
|
return [REACT_QUERY_KEY.GET_QUERY_RANGE, requestData];
|
|
|
|
|
}, [options?.queryKey, requestData]);
|
2023-06-13 16:26:12 +05:30
|
|
|
|
|
|
|
|
return useQuery<SuccessResponse<MetricRangePayloadProps>, Error>({
|
2023-06-07 15:27:33 +03:00
|
|
|
queryFn: async () => GetMetricQueryRange(requestData),
|
|
|
|
|
...options,
|
2023-06-13 16:26:12 +05:30
|
|
|
queryKey,
|
2023-06-07 15:27:33 +03:00
|
|
|
});
|
2023-06-13 16:26:12 +05:30
|
|
|
};
|