solutionsSection.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import useDrawer from 'sentry/components/globalDrawer';
  5. import {useGroupSummary} from 'sentry/components/group/groupSummary';
  6. import Placeholder from 'sentry/components/placeholder';
  7. import {IconChevron} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {Event} from 'sentry/types/event';
  11. import type {Group} from 'sentry/types/group';
  12. import type {Project} from 'sentry/types/project';
  13. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  14. import {singleLineRenderer} from 'sentry/utils/marked';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. import Resources from 'sentry/views/issueDetails/streamline/resources';
  17. import {SidebarSectionTitle} from 'sentry/views/issueDetails/streamline/sidebar';
  18. import {SolutionsHubDrawer} from 'sentry/views/issueDetails/streamline/solutionsHubDrawer';
  19. const isSummaryEnabled = (
  20. hasGenAIConsent: boolean,
  21. hasIssueSummary: boolean,
  22. hideAiFeatures: boolean
  23. ) => {
  24. return hasGenAIConsent && hasIssueSummary && !hideAiFeatures;
  25. };
  26. export default function SolutionsSection({
  27. group,
  28. project,
  29. event,
  30. }: {
  31. event: Event | undefined;
  32. group: Group;
  33. project: Project;
  34. }) {
  35. const organization = useOrganization();
  36. const [isExpanded, setIsExpanded] = useState(false);
  37. const openButtonRef = useRef<HTMLButtonElement>(null);
  38. const {openDrawer} = useDrawer();
  39. const openSolutionsDrawer = () => {
  40. if (!event) {
  41. return;
  42. }
  43. openDrawer(
  44. () => <SolutionsHubDrawer group={group} project={project} event={event} />,
  45. {
  46. ariaLabel: t('Solutions drawer'),
  47. // We prevent a click on the Open/Close Autofix button from closing the drawer so that
  48. // we don't reopen it immediately, and instead let the button handle this itself.
  49. shouldCloseOnInteractOutside: element => {
  50. const viewAllButton = openButtonRef.current;
  51. if (
  52. viewAllButton?.contains(element) ||
  53. document.getElementById('sentry-feedback')?.contains(element)
  54. ) {
  55. return false;
  56. }
  57. return true;
  58. },
  59. transitionProps: {stiffness: 1000},
  60. }
  61. );
  62. };
  63. const hasGenAIConsent = organization.genAIConsent;
  64. const {data: summaryData} = useGroupSummary(group.id, group.issueCategory);
  65. const issueTypeConfig = getConfigForIssueType(group, group.project);
  66. const hasSummary = isSummaryEnabled(
  67. hasGenAIConsent,
  68. issueTypeConfig.issueSummary.enabled,
  69. organization.hideAiFeatures
  70. );
  71. const aiNeedsSetup =
  72. !hasGenAIConsent &&
  73. issueTypeConfig.issueSummary.enabled &&
  74. !organization.hideAiFeatures;
  75. const hasResources = issueTypeConfig.resources;
  76. return (
  77. <div>
  78. <SidebarSectionTitle style={{marginTop: 0}}>
  79. {t('Solutions Hub')}
  80. </SidebarSectionTitle>
  81. {hasSummary && !summaryData && (
  82. <Placeholder
  83. height="60px"
  84. style={{marginBottom: space(1)}}
  85. testId="loading-placeholder"
  86. />
  87. )}
  88. {hasSummary && summaryData && (
  89. <Summary>
  90. <HeadlineText
  91. dangerouslySetInnerHTML={{
  92. __html: singleLineRenderer(
  93. summaryData.whatsWrong?.replaceAll('**', '') ?? ''
  94. ),
  95. }}
  96. />
  97. </Summary>
  98. )}
  99. {aiNeedsSetup && (
  100. <Summary>
  101. <HeadlineText
  102. dangerouslySetInnerHTML={{
  103. __html: singleLineRenderer(
  104. 'Explore potential root causes and solutions with Sentry AI.'
  105. ),
  106. }}
  107. />
  108. </Summary>
  109. )}
  110. {!hasSummary && hasResources && !aiNeedsSetup && (
  111. <ResourcesWrapper isExpanded={isExpanded}>
  112. <ResourcesContent isExpanded={isExpanded}>
  113. <Resources
  114. configResources={issueTypeConfig.resources!}
  115. eventPlatform={event?.platform}
  116. group={group}
  117. />
  118. </ResourcesContent>
  119. <ExpandButton onClick={() => setIsExpanded(!isExpanded)} size="zero">
  120. {isExpanded ? t('SHOW LESS') : t('READ MORE')}
  121. </ExpandButton>
  122. </ResourcesWrapper>
  123. )}
  124. {(hasSummary || aiNeedsSetup) && (
  125. <StyledButton ref={openButtonRef} onClick={() => openSolutionsDrawer()}>
  126. {t('Open Solutions Hub')}
  127. <IconChevron direction="right" size="xs" />
  128. </StyledButton>
  129. )}
  130. </div>
  131. );
  132. }
  133. const Summary = styled('div')`
  134. margin-bottom: ${space(0.5)};
  135. `;
  136. const HeadlineText = styled('span')`
  137. margin-right: ${space(0.5)};
  138. word-break: break-word;
  139. `;
  140. const ResourcesWrapper = styled('div')<{isExpanded: boolean}>`
  141. position: relative;
  142. margin-bottom: ${space(1)};
  143. `;
  144. const ResourcesContent = styled('div')<{isExpanded: boolean}>`
  145. position: relative;
  146. max-height: ${p => (p.isExpanded ? 'none' : '68px')};
  147. overflow: hidden;
  148. padding-bottom: ${p => (p.isExpanded ? space(2) : 0)};
  149. ${p =>
  150. !p.isExpanded &&
  151. `
  152. &::after {
  153. content: '';
  154. position: absolute;
  155. bottom: 0;
  156. left: 0;
  157. right: 0;
  158. height: 40px;
  159. background: linear-gradient(transparent, ${p.theme.background});
  160. }
  161. `}
  162. `;
  163. const ExpandButton = styled(Button)`
  164. position: absolute;
  165. bottom: -${space(1)};
  166. right: 0;
  167. font-size: ${p => p.theme.fontSizeExtraSmall};
  168. color: ${p => p.theme.gray300};
  169. border: none;
  170. box-shadow: none;
  171. &:hover {
  172. color: ${p => p.theme.gray400};
  173. }
  174. `;
  175. const StyledButton = styled(Button)`
  176. margin-top: ${space(1)};
  177. width: 100%;
  178. background: ${p => p.theme.background}
  179. linear-gradient(to right, ${p => p.theme.background}, ${p => p.theme.pink400}20);
  180. color: ${p => p.theme.pink400};
  181. `;