useActiveReplayTab.tsx 862 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {useCallback} from 'react';
  2. import useUrlParams from 'sentry/utils/useUrlParams';
  3. export enum TabKey {
  4. console = 'console',
  5. dom = 'dom',
  6. network = 'network',
  7. trace = 'trace',
  8. issues = 'issues',
  9. memory = 'memory',
  10. }
  11. function isReplayTab(tab: string): tab is TabKey {
  12. return tab in TabKey;
  13. }
  14. const DEFAULT_TAB = TabKey.console;
  15. function useActiveReplayTab() {
  16. const {getParamValue, setParamValue} = useUrlParams('t_main', DEFAULT_TAB);
  17. const paramValue = getParamValue();
  18. return {
  19. getActiveTab: useCallback(
  20. () => (isReplayTab(paramValue || '') ? (paramValue as TabKey) : DEFAULT_TAB),
  21. [paramValue]
  22. ),
  23. setActiveTab: useCallback(
  24. (value: string) =>
  25. isReplayTab(value) ? setParamValue(value) : setParamValue(DEFAULT_TAB),
  26. [setParamValue]
  27. ),
  28. };
  29. }
  30. export default useActiveReplayTab;