useDetailsSplit.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type {RefObject} from 'react';
  2. import {useCallback} from 'react';
  3. import {useResizableDrawer} from 'sentry/utils/useResizableDrawer';
  4. import useUrlParams from 'sentry/utils/useUrlParams';
  5. interface OnClickProps {
  6. dataIndex: number;
  7. rowIndex: number;
  8. }
  9. interface Props {
  10. containerRef: RefObject<HTMLDivElement>;
  11. frames: undefined | ReadonlyArray<unknown>;
  12. handleHeight: number;
  13. urlParamName: string;
  14. onHideDetails?: () => void;
  15. onShowDetails?: (props: OnClickProps) => void;
  16. }
  17. export default function useDetailsSplit({
  18. containerRef,
  19. frames,
  20. handleHeight,
  21. onHideDetails,
  22. onShowDetails,
  23. urlParamName,
  24. }: Props) {
  25. const {getParamValue: getDetailIndex, setParamValue: setDetailIndex} = useUrlParams(
  26. urlParamName,
  27. ''
  28. );
  29. const onClickCell = useCallback(
  30. ({dataIndex, rowIndex}: OnClickProps) => {
  31. if (getDetailIndex() === String(dataIndex)) {
  32. setDetailIndex('');
  33. onHideDetails?.();
  34. } else {
  35. setDetailIndex(String(dataIndex));
  36. onShowDetails?.({dataIndex, rowIndex});
  37. }
  38. },
  39. [getDetailIndex, setDetailIndex, onHideDetails, onShowDetails]
  40. );
  41. const onCloseDetailsSplit = useCallback(() => {
  42. setDetailIndex('');
  43. onHideDetails?.();
  44. }, [setDetailIndex, onHideDetails]);
  45. // `initialSize` cannot depend on containerRef because the ref starts as
  46. // `undefined` which then gets set into the hook and doesn't update.
  47. const initialSize = Math.max(150, window.innerHeight * 0.4);
  48. const {size: containerSize, ...resizableDrawerProps} = useResizableDrawer({
  49. direction: 'up',
  50. initialSize,
  51. min: 0,
  52. onResize: () => {},
  53. });
  54. const maxContainerHeight =
  55. (containerRef.current?.clientHeight || window.innerHeight) - handleHeight;
  56. const splitSize =
  57. frames && getDetailIndex() ? Math.min(maxContainerHeight, containerSize) : undefined;
  58. return {
  59. onClickCell,
  60. onCloseDetailsSplit,
  61. resizableDrawerProps,
  62. selectedIndex: getDetailIndex(),
  63. splitSize,
  64. };
  65. }