index.tsx 8.9 KB

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