useSamplesDrawer.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {useCallback, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import useDrawer from 'sentry/components/globalDrawer';
  5. import {t} from 'sentry/locale';
  6. import {trackAnalytics} from 'sentry/utils/analytics';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {useNavigate} from 'sentry/utils/useNavigate';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import type {ModuleName} from '../../types';
  11. interface UseSamplesDrawerProps {
  12. Component: React.ReactNode;
  13. moduleName: ModuleName;
  14. requiredParams: [string, ...string[]];
  15. onClose?: () => void;
  16. }
  17. export function useSamplesDrawer({
  18. Component,
  19. moduleName,
  20. requiredParams,
  21. onClose = undefined,
  22. }: UseSamplesDrawerProps): void {
  23. const organization = useOrganization();
  24. const {openDrawer, closeDrawer, isDrawerOpen} = useDrawer();
  25. const navigate = useNavigate();
  26. const location = useLocation();
  27. const onCloseAction = useCallback(() => {
  28. if (onClose) {
  29. onClose();
  30. } else {
  31. navigate({
  32. query: {
  33. ...location.query,
  34. transaction: undefined,
  35. transactionMethod: undefined,
  36. spanGroup: undefined,
  37. spanOp: undefined,
  38. query: undefined,
  39. responseCodeClass: undefined,
  40. panel: undefined,
  41. statusClass: undefined,
  42. spanSearchQuery: undefined,
  43. traceStatus: undefined,
  44. retryCount: undefined,
  45. },
  46. });
  47. }
  48. }, [navigate, onClose, location.query]);
  49. const shouldCloseOnLocationChange = useCallback(
  50. (newLocation: Location) => {
  51. if (!requiredParams.every(paramName => Boolean(newLocation.query[paramName]))) {
  52. return true;
  53. }
  54. if (newLocation.pathname.includes('/trace/')) {
  55. return true;
  56. }
  57. return false;
  58. },
  59. [requiredParams]
  60. );
  61. const openSamplesDrawer = useCallback(() => {
  62. if (isDrawerOpen) {
  63. return;
  64. }
  65. trackAnalytics('performance_views.sample_spans.opened', {
  66. organization,
  67. source: moduleName,
  68. });
  69. openDrawer(() => <FullHeightWrapper>{Component}</FullHeightWrapper>, {
  70. ariaLabel: t('Samples'),
  71. onClose: onCloseAction,
  72. transitionProps: {stiffness: 1000},
  73. shouldCloseOnLocationChange,
  74. shouldCloseOnInteractOutside: () => false,
  75. });
  76. }, [
  77. openDrawer,
  78. isDrawerOpen,
  79. onCloseAction,
  80. shouldCloseOnLocationChange,
  81. Component,
  82. organization,
  83. moduleName,
  84. ]);
  85. const shouldDrawerOpen = requiredParams.every(paramName =>
  86. Boolean(location.query[paramName])
  87. );
  88. useEffect(() => {
  89. if (shouldDrawerOpen) {
  90. openSamplesDrawer();
  91. } else {
  92. closeDrawer();
  93. }
  94. }, [shouldDrawerOpen, openSamplesDrawer, closeDrawer]);
  95. }
  96. const FullHeightWrapper = styled('div')`
  97. height: 100%;
  98. display: flex;
  99. flex-direction: column;
  100. `;