useTab.tsx 862 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {useCallback, useMemo} from 'react';
  2. import {decodeScalar} from 'sentry/utils/queryString';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import {useNavigate} from 'sentry/utils/useNavigate';
  5. export enum Tab {
  6. SPAN = 'span',
  7. TRACE = 'trace',
  8. }
  9. export function useTab(): [Tab, (tab: Tab) => void] {
  10. const location = useLocation();
  11. const navigate = useNavigate();
  12. const tab = useMemo(() => {
  13. const rawTab = decodeScalar(location.query.table);
  14. if (rawTab === 'trace') {
  15. return Tab.TRACE;
  16. }
  17. return Tab.SPAN;
  18. }, [location.query.table]);
  19. const setTab = useCallback(
  20. (newTab: Tab) => {
  21. navigate({
  22. ...location,
  23. query: {
  24. ...location.query,
  25. table: newTab,
  26. cursor: undefined,
  27. },
  28. });
  29. },
  30. [location, navigate]
  31. );
  32. return [tab, setTab];
  33. }