datasetSelector.tsx 2.3 KB

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