|
| 1 | +import {useMemo} from 'react'; |
| 2 | +import type {Query} from 'history'; |
| 3 | + |
| 4 | +import {getFieldTypeFromUnit} from 'sentry/components/events/eventCustomPerformanceMetrics'; |
| 5 | +import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse'; |
| 6 | +import type {PageFilters} from 'sentry/types/core'; |
| 7 | +import type {Organization} from 'sentry/types/organization'; |
| 8 | +import type {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements'; |
| 9 | +import {useApiQuery} from 'sentry/utils/queryClient'; |
| 10 | + |
| 11 | +type MeasurementsMetaResponse = Record<string, {functions: string[]; unit: string}>; |
| 12 | + |
| 13 | +export function useCustomMeasurements( |
| 14 | + organization: Organization, |
| 15 | + selection?: PageFilters |
| 16 | +) { |
| 17 | + const query: Query = selection?.datetime |
| 18 | + ? normalizeDateTimeParams(selection.datetime) |
| 19 | + : {}; |
| 20 | + |
| 21 | + if (selection?.projects) { |
| 22 | + query.project = selection.projects.map(String); |
| 23 | + } |
| 24 | + |
| 25 | + const {data, isLoading, isError} = useApiQuery<MeasurementsMetaResponse>( |
| 26 | + [`/organizations/${organization.slug}/measurements-meta/`, {query}], |
| 27 | + { |
| 28 | + staleTime: Infinity, |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + const customMeasurements = useMemo(() => { |
| 33 | + if (!data) { |
| 34 | + return {}; |
| 35 | + } |
| 36 | + |
| 37 | + return Object.entries(data).reduce<CustomMeasurementCollection>( |
| 38 | + (acc, [customMeasurement, measurementData]) => { |
| 39 | + acc[customMeasurement] = { |
| 40 | + key: customMeasurement, |
| 41 | + name: customMeasurement, |
| 42 | + functions: measurementData.functions, |
| 43 | + unit: measurementData.unit, |
| 44 | + fieldType: getFieldTypeFromUnit(measurementData.unit), |
| 45 | + }; |
| 46 | + return acc; |
| 47 | + }, |
| 48 | + {} |
| 49 | + ); |
| 50 | + }, [data]); |
| 51 | + |
| 52 | + return { |
| 53 | + customMeasurements, |
| 54 | + isLoading, |
| 55 | + isError, |
| 56 | + }; |
| 57 | +} |
0 commit comments