datasetSelector.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {CompactSelect} from 'sentry/components/compactSelect';
  2. import {t} from 'sentry/locale';
  3. import type {SavedQuery} from 'sentry/types';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import {useNavigate} from 'sentry/utils/useNavigate';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import {DEFAULT_EVENT_VIEW_MAP} from 'sentry/views/discover/data';
  10. import {
  11. getDatasetFromLocationOrSavedQueryDataset,
  12. getSavedQueryDataset,
  13. } from 'sentry/views/discover/savedQuery/utils';
  14. export const DATASET_PARAM = 'queryDataset';
  15. export const DATASET_LABEL_MAP = {
  16. [SavedQueryDatasets.ERRORS]: t('Errors'),
  17. [SavedQueryDatasets.TRANSACTIONS]: t('Transactions'),
  18. [SavedQueryDatasets.DISCOVER]: t('Unknown'),
  19. };
  20. type Props = {
  21. eventView: EventView;
  22. isHomepage: boolean | undefined;
  23. savedQuery: SavedQuery | undefined;
  24. splitDecision?: SavedQueryDatasets;
  25. };
  26. export function DatasetSelector(props: Props) {
  27. const {savedQuery, isHomepage, splitDecision, eventView} = props;
  28. const location = useLocation();
  29. const organization = useOrganization();
  30. const navigate = useNavigate();
  31. const value = getSavedQueryDataset(location, savedQuery, splitDecision);
  32. const options = [
  33. {
  34. value: SavedQueryDatasets.ERRORS,
  35. label: DATASET_LABEL_MAP[SavedQueryDatasets.ERRORS],
  36. },
  37. {
  38. value: SavedQueryDatasets.TRANSACTIONS,
  39. label: DATASET_LABEL_MAP[SavedQueryDatasets.TRANSACTIONS],
  40. },
  41. ];
  42. if (value === 'discover') {
  43. options.push({
  44. value: SavedQueryDatasets.DISCOVER,
  45. label: DATASET_LABEL_MAP[SavedQueryDatasets.DISCOVER],
  46. });
  47. }
  48. return (
  49. <CompactSelect
  50. triggerProps={{prefix: t('Dataset')}}
  51. value={value}
  52. options={options}
  53. onChange={newValue => {
  54. let nextEventView: EventView;
  55. if (eventView.id) {
  56. nextEventView = eventView.withDataset(
  57. getDatasetFromLocationOrSavedQueryDataset(undefined, newValue.value)
  58. );
  59. } else {
  60. const query = DEFAULT_EVENT_VIEW_MAP[newValue.value];
  61. nextEventView = EventView.fromNewQueryWithLocation(query, location);
  62. }
  63. const nextLocation = nextEventView.getResultsViewUrlTarget(
  64. organization.slug,
  65. isHomepage
  66. );
  67. navigate({
  68. ...location,
  69. query: {
  70. ...nextLocation.query,
  71. [DATASET_PARAM]: newValue.value,
  72. },
  73. });
  74. }}
  75. />
  76. );
  77. }