import {useEffect, useState} from 'react';
import styled from '@emotion/styled';
import Alert from 'sentry/components/alert';
import {Button} from 'sentry/components/button';
import type {RadioGroupProps} from 'sentry/components/forms/controls/radioGroup';
import RadioGroup from 'sentry/components/forms/controls/radioGroup';
import ExternalLink from 'sentry/components/links/externalLink';
import {IconClose} from 'sentry/icons';
import {t, tct} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import {DatasetSource} from 'sentry/utils/discover/types';
import useOrganization from 'sentry/utils/useOrganization';
import {DisplayType, type WidgetType} from 'sentry/views/dashboards/types';
import {hasDatasetSelector} from 'sentry/views/dashboards/utils';
import {DATASET_LABEL_MAP} from 'sentry/views/discover/savedQuery/datasetSelector';
import {DataSet} from '../utils';
import {BuildStep} from './buildStep';
function DiscoverSplitAlert({onDismiss, splitDecision}) {
const splitAlertMessage = splitDecision
? tct(
"We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to [splitDecision]. Edit as you see fit.",
{splitDecision: DATASET_LABEL_MAP[splitDecision]}
)
: null;
return (
}
aria-label={t('Close')}
onClick={onDismiss}
size="zero"
borderless
/>
}
>
{splitAlertMessage}
);
}
interface Props {
dataSet: DataSet;
displayType: DisplayType;
onChange: (dataSet: DataSet) => void;
source?: DatasetSource;
splitDecision?: WidgetType;
}
export function DataSetStep({
dataSet,
onChange,
displayType,
splitDecision,
source,
}: Props) {
const [showSplitAlert, setShowSplitAlert] = useState(
source === DatasetSource.FORCED
);
const organization = useOrganization();
const disabledChoices: RadioGroupProps['disabledChoices'] = [];
const hasDatasetSelectorFeature = hasDatasetSelector(organization);
useEffect(() => {
setShowSplitAlert(!!splitDecision);
}, [splitDecision]);
if (displayType !== DisplayType.TABLE) {
disabledChoices.push([
DataSet.ISSUES,
t('This dataset is restricted to tabular visualization.'),
]);
}
const datasetChoices = new Map();
if (hasDatasetSelectorFeature) {
// TODO: Finalize description copy
datasetChoices.set(DataSet.ERRORS, t('Errors (TypeError, InvalidSearchQuery, etc)'));
datasetChoices.set(DataSet.TRANSACTIONS, t('Transactions'));
}
if (!hasDatasetSelectorFeature) {
datasetChoices.set(DataSet.EVENTS, t('Errors and Transactions'));
}
datasetChoices.set(DataSet.ISSUES, t('Issues (States, Assignment, Time, etc.)'));
datasetChoices.set(DataSet.RELEASES, t('Releases (Sessions, Crash rates)'));
return (
),
}
)}
>
{showSplitAlert && hasDatasetSelectorFeature && (
setShowSplitAlert(false)}
splitDecision={splitDecision}
/>
)}
{
onChange(newDataSet as DataSet);
}}
orientInline
/>
);
}
const DataSetChoices = styled(RadioGroup)`
display: flex;
flex-wrap: wrap;
gap: ${space(2)};
`;
const StyledCloseButton = styled(Button)`
background-color: transparent;
transition: opacity 0.1s linear;
&:hover,
&:focus {
background-color: transparent;
opacity: 1;
}
`;