import {Fragment, useCallback, useMemo, useState} from 'react';
import styled from '@emotion/styled';
import {
type Field,
MetricSamplesTable,
SearchableMetricSamplesTable,
} from 'sentry/components/ddm/metricSamplesTable';
import HookOrDefault from 'sentry/components/hookOrDefault';
import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
import {Tooltip} from 'sentry/components/tooltip';
import {t} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {MRI} from 'sentry/types';
import {trackAnalytics} from 'sentry/utils/analytics';
import {isCustomMetric} from 'sentry/utils/metrics';
import type {
FocusedMetricsSeries,
MetricQueryWidgetParams,
MetricWidgetQueryParams,
} from 'sentry/utils/metrics/types';
import {MetricQueryType} from 'sentry/utils/metrics/types';
import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
import useOrganization from 'sentry/utils/useOrganization';
import {CodeLocations} from 'sentry/views/ddm/codeLocations';
import type {FocusAreaProps} from 'sentry/views/ddm/context';
import {useDDMContext} from 'sentry/views/ddm/context';
import {extendQueryWithGroupBys} from 'sentry/views/ddm/utils';
enum Tab {
SAMPLES = 'samples',
CODE_LOCATIONS = 'codeLocations',
}
export function WidgetDetails() {
const {
selectedWidgetIndex,
widgets,
focusArea,
setHighlightedSampleId,
setMetricsSamples,
} = useDDMContext();
const selectedWidget = widgets[selectedWidgetIndex] as
| MetricWidgetQueryParams
| undefined;
const handleSampleRowHover = useCallback(
(sampleId?: string) => {
setHighlightedSampleId(sampleId);
},
[setHighlightedSampleId]
);
// TODO(aknaus): better fallback
if (selectedWidget?.type === MetricQueryType.FORMULA) {
;
}
const {mri, op, query, focusedSeries} = selectedWidget as MetricQueryWidgetParams;
return (
);
}
interface MetricDetailsProps {
focusArea?: FocusAreaProps;
focusedSeries?: FocusedMetricsSeries[];
mri?: MRI;
onRowHover?: (sampleId?: string) => void;
op?: string;
query?: string;
setMetricsSamples?: React.Dispatch<
React.SetStateAction['data'] | undefined>
>;
}
export function MetricDetails({
mri,
op,
query,
focusedSeries,
onRowHover,
focusArea,
setMetricsSamples,
}: MetricDetailsProps) {
const organization = useOrganization();
const [selectedTab, setSelectedTab] = useState(Tab.SAMPLES);
const isCodeLocationsDisabled = mri && !isCustomMetric({mri});
if (isCodeLocationsDisabled && selectedTab === Tab.CODE_LOCATIONS) {
setSelectedTab(Tab.SAMPLES);
}
const queryWithFocusedSeries = useMemo(
() =>
focusedSeries &&
extendQueryWithGroupBys(
query || '',
focusedSeries.map(s => s.groupBy)
),
[focusedSeries, query]
);
const handleTabChange = useCallback(
(tab: Tab) => {
if (tab === Tab.CODE_LOCATIONS) {
trackAnalytics('ddm.code-locations', {
organization,
});
}
setSelectedTab(tab);
},
[organization]
);
return (
{t('Sampled Events')}
{t('Code Location')}
{organization.features.includes('metrics-samples-list-search') ? (
) : (
)}
);
}
const MetricSampleTableWrapper = HookOrDefault({
hookName: 'component:ddm-metrics-samples-list',
defaultComponent: ({children}) => {children},
});
const TrayWrapper = styled('div')`
padding-top: ${space(4)};
display: grid;
grid-template-rows: auto auto 1fr;
`;
const ContentWrapper = styled('div')`
position: relative;
padding-top: ${space(2)};
`;