useActiveReplayTab.tsx 974 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {useCallback} from 'react';
  2. import {t} from 'sentry/locale';
  3. import useUrlParams from 'sentry/utils/replays/hooks/useUrlParams';
  4. export const ReplayTabs = {
  5. console: t('Console'),
  6. network: t('Network Waterfall'),
  7. network_table: t('Network Table'),
  8. trace: t('Trace'),
  9. issues: t('Issues'),
  10. tags: t('Tags'),
  11. memory: t('Memory'),
  12. };
  13. type TabKey = keyof typeof ReplayTabs;
  14. export function isReplayTab(tab: string): tab is TabKey {
  15. return tab in ReplayTabs;
  16. }
  17. const DEFAULT_TAB = 'console';
  18. function useActiveReplayTab() {
  19. const {getParamValue, setParamValue} = useUrlParams('t_main', DEFAULT_TAB);
  20. const paramValue = getParamValue();
  21. return {
  22. getActiveTab: useCallback(
  23. () => (isReplayTab(paramValue || '') ? (paramValue as TabKey) : DEFAULT_TAB),
  24. [paramValue]
  25. ),
  26. setActiveTab: (value: string) =>
  27. isReplayTab(value) ? setParamValue(value) : setParamValue(DEFAULT_TAB),
  28. };
  29. }
  30. export default useActiveReplayTab;