index.tsx 8.9 KB

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