datasetSelector.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import omit from 'lodash/omit';
  2. import {CompactSelect} from 'sentry/components/compactSelect';
  3. import {t} from 'sentry/locale';
  4. import type {SavedQuery} from 'sentry/types';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import type {SavedQueryDatasets} from 'sentry/utils/discover/types';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {useNavigate} from 'sentry/utils/useNavigate';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {DEFAULT_EVENT_VIEW_MAP} from 'sentry/views/discover/data';
  11. import {getSavedQueryDataset} from 'sentry/views/discover/savedQuery/utils';
  12. export const DATASET_PARAM = 'queryDataset';
  13. type Props = {
  14. isHomepage: boolean | undefined;
  15. savedQuery: SavedQuery | undefined;
  16. splitDecision?: SavedQueryDatasets;
  17. };
  18. export function DatasetSelector(props: Props) {
  19. const {savedQuery, isHomepage, splitDecision} = props;
  20. const location = useLocation();
  21. const organization = useOrganization();
  22. const navigate = useNavigate();
  23. const value = getSavedQueryDataset(location, savedQuery, splitDecision);
  24. const options = [
  25. {value: 'error-events', label: t('Errors')},
  26. {value: 'transaction-like', label: t('Transactions')},
  27. ];
  28. if (value === 'discover') {
  29. options.push({value: 'discover', label: t('Unknown')});
  30. }
  31. return (
  32. <CompactSelect
  33. triggerProps={{prefix: t('Dataset')}}
  34. value={value}
  35. options={options}
  36. onChange={newValue => {
  37. const query = DEFAULT_EVENT_VIEW_MAP[newValue.value];
  38. const newQuery = savedQuery
  39. ? omit(query, ['name', 'id', 'projects', 'range'])
  40. : query;
  41. const nextEventView = EventView.fromNewQueryWithLocation(newQuery, location);
  42. const nextLocation = nextEventView.getResultsViewUrlTarget(
  43. organization.slug,
  44. isHomepage
  45. );
  46. navigate({
  47. ...location,
  48. query: {
  49. ...nextLocation.query,
  50. [DATASET_PARAM]: newValue.value,
  51. },
  52. });
  53. }}
  54. size="sm"
  55. />
  56. );
  57. }