foldSection.tsx 6.2 KB

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