foldSection.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {type CSSProperties, forwardRef, Fragment, useCallback, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import ErrorBoundary from 'sentry/components/errorBoundary';
  4. import InteractionStateLayer from 'sentry/components/interactionStateLayer';
  5. import {IconChevron} from 'sentry/icons';
  6. import {space} from 'sentry/styles/space';
  7. import {trackAnalytics} from 'sentry/utils/analytics';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import {useSyncedLocalStorageState} from 'sentry/utils/useSyncedLocalStorageState';
  10. import type {SectionKey} from 'sentry/views/issueDetails/streamline/context';
  11. import {useEventDetails} from 'sentry/views/issueDetails/streamline/context';
  12. export function getFoldSectionKey(key: SectionKey) {
  13. return `'issue-details-fold-section-collapse:${key}`;
  14. }
  15. interface FoldSectionProps {
  16. children: React.ReactNode;
  17. sectionKey: SectionKey;
  18. /**
  19. * Title of the section, always visible
  20. */
  21. title: React.ReactNode;
  22. /**
  23. * Actions associated with the section, only visible when open
  24. */
  25. actions?: React.ReactNode;
  26. className?: string;
  27. /**
  28. * Should this section be initially open, gets overridden by user preferences
  29. */
  30. initialCollapse?: boolean;
  31. /**
  32. * Disable the ability for the user to collapse the section
  33. */
  34. preventCollapse?: boolean;
  35. style?: CSSProperties;
  36. }
  37. export const FoldSection = forwardRef<HTMLElement, FoldSectionProps>(function FoldSection(
  38. {
  39. children,
  40. title,
  41. actions,
  42. sectionKey,
  43. initialCollapse = false,
  44. preventCollapse = false,
  45. ...props
  46. },
  47. forwardedRef
  48. ) {
  49. const organization = useOrganization();
  50. const {sectionData, navScrollMargin, dispatch} = useEventDetails();
  51. // Does not control open/close state. Controls what state is persisted to local storage
  52. const [isCollapsed, setIsCollapsed] = useSyncedLocalStorageState(
  53. getFoldSectionKey(sectionKey),
  54. initialCollapse
  55. );
  56. if (!sectionData.hasOwnProperty(sectionKey)) {
  57. dispatch({type: 'UPDATE_SECTION', key: sectionKey, config: {initialCollapse}});
  58. }
  59. // This controls disabling the InteractionStateLayer when hovering over action items. We don't
  60. // want selecting an action to appear as though it'll fold/unfold the section.
  61. const [isLayerEnabled, setIsLayerEnabled] = useState(true);
  62. const toggleCollapse = useCallback(
  63. (e: React.MouseEvent) => {
  64. e.preventDefault(); // Prevent browser summary/details behaviour
  65. window.getSelection()?.removeAllRanges(); // Prevent text selection on expand
  66. trackAnalytics('issue_details.section_fold', {
  67. sectionKey,
  68. organization,
  69. open: !isCollapsed,
  70. });
  71. setIsCollapsed(!isCollapsed);
  72. },
  73. [organization, sectionKey, isCollapsed, setIsCollapsed]
  74. );
  75. return (
  76. <Fragment>
  77. <Section
  78. {...props}
  79. ref={forwardedRef}
  80. id={sectionKey}
  81. scrollMargin={navScrollMargin ?? 0}
  82. >
  83. <SectionExpander
  84. preventCollapse={preventCollapse}
  85. onClick={preventCollapse ? e => e.preventDefault() : toggleCollapse}
  86. >
  87. <InteractionStateLayer
  88. hidden={preventCollapse ? preventCollapse : !isLayerEnabled}
  89. />
  90. <TitleWithActions>
  91. <TitleWrapper>{title}</TitleWrapper>
  92. {!preventCollapse && !isCollapsed && (
  93. <div
  94. onClick={e => e.stopPropagation()}
  95. onMouseEnter={() => setIsLayerEnabled(false)}
  96. onMouseLeave={() => setIsLayerEnabled(true)}
  97. >
  98. {actions}
  99. </div>
  100. )}
  101. </TitleWithActions>
  102. <IconWrapper preventCollapse={preventCollapse}>
  103. <IconChevron direction={isCollapsed ? 'down' : 'up'} size="xs" />
  104. </IconWrapper>
  105. </SectionExpander>
  106. {isCollapsed ? null : (
  107. <ErrorBoundary mini>
  108. <Content>{children}</Content>
  109. </ErrorBoundary>
  110. )}
  111. </Section>
  112. <SectionDivider />
  113. </Fragment>
  114. );
  115. });
  116. export const SectionDivider = styled('hr')`
  117. border-color: ${p => p.theme.translucentBorder};
  118. margin: ${space(1)} 0;
  119. &:last-child {
  120. display: none;
  121. }
  122. `;
  123. export const Section = styled('section')<{scrollMargin: number}>`
  124. scroll-margin-top: calc(${space(1)} + ${p => p.scrollMargin ?? 0}px);
  125. `;
  126. const Content = styled('div')`
  127. padding: ${space(0.5)} ${space(0.75)};
  128. `;
  129. const SectionExpander = styled('div')<{preventCollapse: boolean}>`
  130. display: grid;
  131. grid-template-columns: 1fr auto;
  132. align-items: center;
  133. padding: ${space(0.5)} ${space(0.75)};
  134. border-radius: ${p => p.theme.borderRadius};
  135. cursor: ${p => (p.preventCollapse ? 'initial' : 'pointer')};
  136. position: relative;
  137. `;
  138. const TitleWrapper = styled('div')`
  139. font-size: ${p => p.theme.fontSizeMedium};
  140. font-weight: ${p => p.theme.fontWeightBold};
  141. `;
  142. const IconWrapper = styled('div')<{preventCollapse: boolean}>`
  143. color: ${p => p.theme.subText};
  144. line-height: 0;
  145. visibility: ${p => (p.preventCollapse ? 'hidden' : 'initial')};
  146. `;
  147. const TitleWithActions = styled('div')`
  148. display: grid;
  149. grid-template-columns: 1fr auto;
  150. margin-right: 8px;
  151. align-items: center;
  152. /* Usually the actions are buttons, this height allows actions appearing after opening the
  153. details section to not expand the summary */
  154. min-height: 26px;
  155. `;