index.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import {useState} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Feature from 'sentry/components/acl/feature';
  5. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  6. import CreateAlertButton from 'sentry/components/createAlertButton';
  7. import {Hovercard} from 'sentry/components/hovercard';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import List from 'sentry/components/list';
  11. import ListItem from 'sentry/components/list/listItem';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelBody from 'sentry/components/panels/panelBody';
  14. import PanelHeader from 'sentry/components/panels/panelHeader';
  15. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  16. import {t} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {Organization} from 'sentry/types';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  21. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  22. import {AlertRuleType} from 'sentry/views/alerts/types';
  23. import type {AlertType, WizardRuleTemplate} from './options';
  24. import {
  25. AlertWizardAlertNames,
  26. AlertWizardRuleTemplates,
  27. getAlertWizardCategories,
  28. } from './options';
  29. import {AlertWizardPanelContent} from './panelContent';
  30. import RadioPanelGroup from './radioPanelGroup';
  31. type RouteParams = {
  32. projectId?: string;
  33. };
  34. type AlertWizardProps = RouteComponentProps<RouteParams, {}> & {
  35. organization: Organization;
  36. projectId: string;
  37. };
  38. const DEFAULT_ALERT_OPTION = 'issues';
  39. function AlertWizard({organization, params, location, projectId}: AlertWizardProps) {
  40. const [alertOption, setAlertOption] = useState<AlertType>(
  41. location.query.alert_option in AlertWizardAlertNames
  42. ? location.query.alert_option
  43. : DEFAULT_ALERT_OPTION
  44. );
  45. const projectSlug = params.projectId ?? projectId;
  46. const handleChangeAlertOption = (option: AlertType) => {
  47. setAlertOption(option);
  48. };
  49. function renderCreateAlertButton() {
  50. let metricRuleTemplate: Readonly<WizardRuleTemplate> | undefined =
  51. AlertWizardRuleTemplates[alertOption];
  52. const isMetricAlert = !!metricRuleTemplate;
  53. const isTransactionDataset = metricRuleTemplate?.dataset === Dataset.TRANSACTIONS;
  54. if (
  55. organization.features.includes('alert-crash-free-metrics') &&
  56. metricRuleTemplate?.dataset === Dataset.SESSIONS
  57. ) {
  58. metricRuleTemplate = {...metricRuleTemplate, dataset: Dataset.METRICS};
  59. }
  60. if (
  61. organization.features.includes('metric-alert-ignore-archived') &&
  62. metricRuleTemplate?.dataset === Dataset.ERRORS
  63. ) {
  64. // Pre-fill is:unresolved for error metric alerts
  65. // Filters out events in issues that are archived or resolved
  66. metricRuleTemplate = {...metricRuleTemplate, query: 'is:unresolved'};
  67. }
  68. const renderNoAccess = p => (
  69. <Hovercard
  70. body={
  71. <FeatureDisabled
  72. features={p.features}
  73. hideHelpToggle
  74. featureName={t('Metric Alerts')}
  75. />
  76. }
  77. >
  78. {p.children(p)}
  79. </Hovercard>
  80. );
  81. return (
  82. <Feature
  83. features={
  84. isTransactionDataset
  85. ? ['organizations:incidents', 'organizations:performance-view']
  86. : isMetricAlert
  87. ? ['organizations:incidents']
  88. : []
  89. }
  90. requireAll
  91. organization={organization}
  92. hookName="feature-disabled:alert-wizard-performance"
  93. renderDisabled={renderNoAccess}
  94. >
  95. {({hasFeature}) => (
  96. <WizardButtonContainer
  97. onClick={() =>
  98. trackAnalytics('alert_wizard.option_selected', {
  99. organization,
  100. alert_type: alertOption,
  101. })
  102. }
  103. >
  104. <CreateAlertButton
  105. organization={organization}
  106. projectSlug={projectSlug}
  107. disabled={!hasFeature}
  108. priority="primary"
  109. to={{
  110. pathname: `/organizations/${organization.slug}/alerts/new/${
  111. isMetricAlert ? AlertRuleType.METRIC : AlertRuleType.ISSUE
  112. }/`,
  113. query: {
  114. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  115. project: projectSlug,
  116. referrer: location?.query?.referrer,
  117. },
  118. }}
  119. hideIcon
  120. >
  121. {t('Set Conditions')}
  122. </CreateAlertButton>
  123. </WizardButtonContainer>
  124. )}
  125. </Feature>
  126. );
  127. }
  128. const panelContent = AlertWizardPanelContent[alertOption];
  129. return (
  130. <Layout.Page>
  131. <SentryDocumentTitle title={t('Alert Creation Wizard')} projectSlug={projectSlug} />
  132. <Layout.Header>
  133. <StyledHeaderContent>
  134. <BuilderBreadCrumbs
  135. organization={organization}
  136. projectSlug={projectSlug}
  137. title={t('Select Alert')}
  138. />
  139. <Layout.Title>{t('Select Alert')}</Layout.Title>
  140. </StyledHeaderContent>
  141. </Layout.Header>
  142. <Layout.Body>
  143. <Layout.Main fullWidth>
  144. <WizardBody>
  145. <WizardOptions>
  146. {getAlertWizardCategories(organization).map(
  147. ({categoryHeading, options}) => (
  148. <div key={categoryHeading}>
  149. <CategoryTitle>{categoryHeading} </CategoryTitle>
  150. <RadioPanelGroup
  151. choices={options.map(alertType => {
  152. return [alertType, AlertWizardAlertNames[alertType]];
  153. })}
  154. onChange={handleChangeAlertOption}
  155. value={alertOption}
  156. label="alert-option"
  157. />
  158. </div>
  159. )
  160. )}
  161. </WizardOptions>
  162. <WizardPanel visible={!!panelContent && !!alertOption}>
  163. <WizardPanelBody>
  164. <div>
  165. <PanelHeader>{AlertWizardAlertNames[alertOption]}</PanelHeader>
  166. <PanelBody withPadding>
  167. <PanelDescription>
  168. {panelContent.description}{' '}
  169. {panelContent.docsLink && (
  170. <ExternalLink href={panelContent.docsLink}>
  171. {t('Learn more')}
  172. </ExternalLink>
  173. )}
  174. </PanelDescription>
  175. <WizardImage src={panelContent.illustration} />
  176. <ExampleHeader>{t('Examples')}</ExampleHeader>
  177. <ExampleList symbol="bullet">
  178. {panelContent.examples.map((example, i) => (
  179. <ExampleItem key={i}>{example}</ExampleItem>
  180. ))}
  181. </ExampleList>
  182. </PanelBody>
  183. </div>
  184. <WizardFooter>{renderCreateAlertButton()}</WizardFooter>
  185. </WizardPanelBody>
  186. </WizardPanel>
  187. </WizardBody>
  188. </Layout.Main>
  189. </Layout.Body>
  190. </Layout.Page>
  191. );
  192. }
  193. const StyledHeaderContent = styled(Layout.HeaderContent)`
  194. overflow: visible;
  195. `;
  196. const CategoryTitle = styled('h2')`
  197. font-weight: normal;
  198. font-size: ${p => p.theme.fontSizeExtraLarge};
  199. margin-bottom: ${space(1)} !important;
  200. `;
  201. const WizardBody = styled('div')`
  202. display: flex;
  203. padding-top: ${space(1)};
  204. `;
  205. const WizardOptions = styled('div')`
  206. display: flex;
  207. flex-direction: column;
  208. gap: ${space(4)};
  209. flex: 3;
  210. margin-right: ${space(3)};
  211. padding-right: ${space(3)};
  212. max-width: 300px;
  213. `;
  214. const WizardImage = styled('img')`
  215. max-height: 300px;
  216. `;
  217. const WizardPanel = styled(Panel)<{visible?: boolean}>`
  218. max-width: 700px;
  219. position: sticky;
  220. top: 20px;
  221. flex: 5;
  222. display: flex;
  223. ${p => !p.visible && 'visibility: hidden'};
  224. flex-direction: column;
  225. align-items: start;
  226. align-self: flex-start;
  227. ${p => p.visible && 'animation: 0.6s pop ease forwards'};
  228. @keyframes pop {
  229. 0% {
  230. transform: translateY(30px);
  231. opacity: 0;
  232. }
  233. 100% {
  234. transform: translateY(0);
  235. opacity: 1;
  236. }
  237. }
  238. `;
  239. const ExampleList = styled(List)`
  240. margin-bottom: ${space(2)} !important;
  241. `;
  242. const WizardPanelBody = styled(PanelBody)`
  243. flex: 1;
  244. min-width: 100%;
  245. `;
  246. const PanelDescription = styled('p')`
  247. margin-bottom: ${space(2)};
  248. `;
  249. const ExampleHeader = styled('div')`
  250. margin: 0 0 ${space(1)} 0;
  251. font-size: ${p => p.theme.fontSizeLarge};
  252. `;
  253. const ExampleItem = styled(ListItem)`
  254. font-size: ${p => p.theme.fontSizeMedium};
  255. `;
  256. const WizardFooter = styled('div')`
  257. border-top: 1px solid ${p => p.theme.border};
  258. padding: ${space(1.5)} ${space(1.5)} ${space(1.5)} ${space(1.5)};
  259. `;
  260. const WizardButtonContainer = styled('div')`
  261. display: flex;
  262. justify-content: flex-end;
  263. a:not(:last-child) {
  264. margin-right: ${space(1)};
  265. }
  266. `;
  267. export default AlertWizard;