useDataset.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {useCallback, useMemo} from 'react';
  2. import type {Location} from 'history';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {decodeScalar} from 'sentry/utils/queryString';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import {useNavigate} from 'sentry/utils/useNavigate';
  7. interface Options {
  8. location: Location;
  9. navigate: ReturnType<typeof useNavigate>;
  10. }
  11. export function useDataset(): [DiscoverDatasets, (dataset: DiscoverDatasets) => void] {
  12. const location = useLocation();
  13. const navigate = useNavigate();
  14. const options = {location, navigate};
  15. return useDatasetImpl(options);
  16. }
  17. function useDatasetImpl({
  18. location,
  19. navigate,
  20. }: Options): [DiscoverDatasets, (dataset: DiscoverDatasets) => void] {
  21. const dataset: DiscoverDatasets = useMemo(() => {
  22. const rawDataset = decodeScalar(location.query.dataset);
  23. if (rawDataset === 'spans') {
  24. return DiscoverDatasets.SPANS_EAP;
  25. }
  26. return DiscoverDatasets.SPANS_INDEXED;
  27. }, [location.query.dataset]);
  28. const setDataset = useCallback(
  29. (newDataset: DiscoverDatasets) => {
  30. navigate({
  31. ...location,
  32. query: {
  33. ...location.query,
  34. dataset: newDataset,
  35. },
  36. });
  37. },
  38. [location, navigate]
  39. );
  40. return [dataset, setDataset];
  41. }