useReplayLayout.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {useCallback} from 'react';
  2. import PreferencesStore from 'sentry/stores/preferencesStore';
  3. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  4. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import useUrlParams from 'sentry/utils/useUrlParams';
  7. import {getDefaultLayout} from 'sentry/views/replays/detail/layout/utils';
  8. export enum LayoutKey {
  9. /**
  10. * ### Top
  11. *┌────────────────────┐
  12. *│ Timeline │
  13. *├───────────┬────────┤
  14. *│ Details > Crumbs │
  15. *│ > │
  16. *│ > |
  17. *│ > │
  18. *│ > │
  19. *└───────────┴────────┘
  20. */
  21. no_video = 'no_video',
  22. /**
  23. * ### Video Only
  24. *┌────────────────────┐
  25. *│ Timeline │
  26. *├────────────────────┤
  27. *│ │
  28. *│ |
  29. *│ Video │
  30. *│ │
  31. *│ │
  32. *└────────────────────┘
  33. */
  34. video_only = 'video_only',
  35. /**
  36. * ### Topbar
  37. *┌────────────────────┐
  38. *│ Timeline │
  39. *├───────────┬────────┤
  40. *│ Video │ Crumbs │
  41. *│ │ │
  42. *├^^^^^^^^^^^^^^^^^^^^┤
  43. *│ Details │
  44. *│ │
  45. *└────────────────────┘
  46. */
  47. topbar = 'topbar',
  48. /**
  49. * ### Sidebar Left
  50. * ┌───────────────────┐
  51. * │ Timeline │
  52. * ├────────┬──────────┤
  53. * │ Video > Details │
  54. * │ > │
  55. * │^^^^^^^ > |
  56. * │ Crumbs > │
  57. * │ Tabs > │
  58. * └────────┴──────────┘
  59. */
  60. sidebar_left = 'sidebar_left',
  61. }
  62. function isLayout(val: string): val is LayoutKey {
  63. return val in LayoutKey;
  64. }
  65. function useActiveReplayTab() {
  66. const collapsed = !!useLegacyStore(PreferencesStore).collapsed;
  67. const defaultLayout = getDefaultLayout(collapsed);
  68. const organization = useOrganization();
  69. const {getParamValue, setParamValue} = useUrlParams('l_page', defaultLayout);
  70. const paramValue = getParamValue();
  71. return {
  72. getLayout: useCallback(
  73. (): LayoutKey =>
  74. isLayout(paramValue || '') ? (paramValue as LayoutKey) : defaultLayout,
  75. [defaultLayout, paramValue]
  76. ),
  77. setLayout: useCallback(
  78. (value: string) => {
  79. const chosenLayout = isLayout(value) ? value : defaultLayout;
  80. setParamValue(chosenLayout);
  81. trackAdvancedAnalyticsEvent('replay.details-layout-changed', {
  82. organization,
  83. default_layout: defaultLayout,
  84. chosen_layout: chosenLayout,
  85. });
  86. },
  87. [organization, defaultLayout, setParamValue]
  88. ),
  89. };
  90. }
  91. export default useActiveReplayTab;