import {Fragment, useEffect, useLayoutEffect} from 'react'; import {getUtcToLocalDateObject} from 'sentry/utils/dates'; import { getIsMetricsDataFromResults, useMEPDataContext, } from 'sentry/utils/performance/contexts/metricsEnhancedPerformanceDataContext'; import type { QueryDefinitionWithKey, QueryHandlerProps, WidgetDataConstraint, } from '../types'; import type {PerformanceWidgetSetting} from '../widgetDefinitions'; /** * Component to handle switching component-style queries over to state. This * should be temporary to make it easier to switch away from waterfall style * api components. */ export function QueryHandler( props: QueryHandlerProps ) { const children = props.children ?? ; if (!props.queries.length) { return {children}; } return ( {props.queries .filter(q => (q.enabled ? q.enabled(props.widgetData) : true)) .map(query => ( ))} ); } function genericQueryReferrer(setting: PerformanceWidgetSetting) { return `api.performance.generic-widget-chart.${setting.replace(/_/g, '-')}`; } function SingleQueryHandler( props: QueryHandlerProps & {query: QueryDefinitionWithKey} ) { const query = props.query; const globalSelection = props.queryProps.eventView.getPageFilters(); const start = globalSelection.datetime.start ? getUtcToLocalDateObject(globalSelection.datetime.start) : null; const end = globalSelection.datetime.end ? getUtcToLocalDateObject(globalSelection.datetime.end) : null; useEffect( () => () => { // Destroy previous data on unmount, in case enabled value changes and unmounts the query component. props.removeWidgetDataForKey(query.queryKey); }, // eslint-disable-next-line react-hooks/exhaustive-deps [] ); return ( {results => { return ( results={results} {...props} query={query} /> ); }} ); } function QueryResultSaver( props: { // TODO(k-fish): Fix this any. query: QueryDefinitionWithKey; results: any; } & QueryHandlerProps ) { const mepContext = useMEPDataContext(); const {results, query} = props; const transformed = query.transform(props.queryProps, results, props.query); useLayoutEffect(() => { const isMetricsData = getIsMetricsDataFromResults( results, props.queryProps.fields[0] ); mepContext.setIsMetricsData(isMetricsData); props.setWidgetDataForKey(query.queryKey, transformed); // eslint-disable-next-line react-hooks/exhaustive-deps }, [transformed?.hasData, transformed?.isLoading, transformed?.isErrored]); return ; }