datasetSelector.tsx 1.9 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 {decodeScalar} from 'sentry/utils/queryString';
  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. export const DATASET_PARAM = 'queryDataset';
  12. type Props = {
  13. isHomepage: boolean | undefined;
  14. savedQuery: SavedQuery | undefined;
  15. };
  16. export function DatasetSelector(props: Props) {
  17. const {savedQuery, isHomepage} = props;
  18. const location = useLocation();
  19. const organization = useOrganization();
  20. const navigate = useNavigate();
  21. const value =
  22. decodeScalar(location.query[DATASET_PARAM]) ??
  23. savedQuery?.queryDataset ??
  24. 'error-events';
  25. const options = [
  26. {value: 'error-events', label: t('Errors')},
  27. {value: 'transaction-like', label: t('Transactions')},
  28. ];
  29. if (value === 'discover') {
  30. options.push({value: 'discover', label: t('Unknown')});
  31. }
  32. return (
  33. <CompactSelect
  34. triggerProps={{prefix: t('Dataset')}}
  35. value={value}
  36. options={options}
  37. onChange={newValue => {
  38. const query = DEFAULT_EVENT_VIEW_MAP[newValue.value];
  39. const newQuery = savedQuery
  40. ? omit(query, ['name', 'id', 'projects', 'range'])
  41. : query;
  42. const nextEventView = EventView.fromNewQueryWithLocation(newQuery, location);
  43. const nextLocation = nextEventView.getResultsViewUrlTarget(
  44. organization.slug,
  45. isHomepage
  46. );
  47. navigate({
  48. ...location,
  49. query: {
  50. ...nextLocation.query,
  51. [DATASET_PARAM]: newValue.value,
  52. },
  53. });
  54. }}
  55. size="sm"
  56. />
  57. );
  58. }