foldSection.tsx 5.4 KB

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