index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import {Component, Fragment} 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, PanelBody, PanelHeader} from 'sentry/components/panels';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {t} from 'sentry/locale';
  15. import space from 'sentry/styles/space';
  16. import {Organization, Project} from 'sentry/types';
  17. import {logExperiment} from 'sentry/utils/analytics';
  18. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  19. import withProjects from 'sentry/utils/withProjects';
  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 {PRESET_AGGREGATES} from '../rules/metric/presets';
  24. import {
  25. AlertType,
  26. AlertWizardAlertNames,
  27. AlertWizardRuleTemplates,
  28. getAlertWizardCategories,
  29. WizardRuleTemplate,
  30. } from './options';
  31. import {AlertWizardPanelContent} from './panelContent';
  32. import RadioPanelGroup from './radioPanelGroup';
  33. type RouteParams = {
  34. orgId: string;
  35. projectId?: string;
  36. };
  37. type Props = RouteComponentProps<RouteParams, {}> & {
  38. organization: Organization;
  39. projectId: string;
  40. projects: Project[];
  41. };
  42. type State = {
  43. alertOption: AlertType;
  44. };
  45. const DEFAULT_ALERT_OPTION = 'issues';
  46. class AlertWizard extends Component<Props, State> {
  47. state: State = {
  48. alertOption:
  49. this.props.location.query.alert_option in AlertWizardAlertNames
  50. ? this.props.location.query.alert_option
  51. : DEFAULT_ALERT_OPTION,
  52. };
  53. componentDidMount() {
  54. // capture landing on the alert wizard page and viewing the issue alert by default
  55. this.trackView();
  56. }
  57. trackView(alertType: AlertType = DEFAULT_ALERT_OPTION) {
  58. const {organization} = this.props;
  59. trackAdvancedAnalyticsEvent('alert_wizard.option_viewed', {
  60. organization,
  61. alert_type: alertType,
  62. });
  63. }
  64. handleChangeAlertOption = (alertOption: AlertType) => {
  65. this.setState({alertOption});
  66. this.trackView(alertOption);
  67. };
  68. renderCreateAlertButton() {
  69. const {organization, location, params, projectId: _projectId} = this.props;
  70. const {alertOption} = this.state;
  71. const projectId = params.projectId ?? _projectId;
  72. const project = this.props.projects.find(p => p.slug === projectId);
  73. let metricRuleTemplate: Readonly<WizardRuleTemplate> | undefined =
  74. AlertWizardRuleTemplates[alertOption];
  75. const isMetricAlert = !!metricRuleTemplate;
  76. const isTransactionDataset = metricRuleTemplate?.dataset === Dataset.TRANSACTIONS;
  77. const hasAlertWizardV3 = organization.features.includes('alert-wizard-v3');
  78. if (
  79. organization.features.includes('alert-crash-free-metrics') &&
  80. metricRuleTemplate?.dataset === Dataset.SESSIONS
  81. ) {
  82. metricRuleTemplate = {...metricRuleTemplate, dataset: Dataset.METRICS};
  83. }
  84. const supportedPreset = PRESET_AGGREGATES.filter(
  85. agg => agg.alertType === alertOption
  86. )[0];
  87. const to = hasAlertWizardV3
  88. ? {
  89. pathname: `/organizations/${organization.slug}/alerts/new/${
  90. isMetricAlert ? AlertRuleType.METRIC : AlertRuleType.ISSUE
  91. }/`,
  92. query: {
  93. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  94. project: projectId,
  95. referrer: location?.query?.referrer,
  96. },
  97. }
  98. : {
  99. pathname: `/organizations/${organization.slug}/alerts/${projectId}/new/`,
  100. query: {
  101. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  102. createFromWizard: true,
  103. referrer: location?.query?.referrer,
  104. },
  105. };
  106. const renderNoAccess = p => (
  107. <Hovercard
  108. body={
  109. <FeatureDisabled
  110. features={p.features}
  111. hideHelpToggle
  112. featureName={t('Metric Alerts')}
  113. />
  114. }
  115. >
  116. {p.children(p)}
  117. </Hovercard>
  118. );
  119. let showUseTemplateBtn: boolean =
  120. !!project?.firstTransactionEvent &&
  121. isMetricAlert &&
  122. metricRuleTemplate?.dataset === Dataset.TRANSACTIONS &&
  123. !!supportedPreset;
  124. if (showUseTemplateBtn) {
  125. logExperiment({
  126. key: 'MetricAlertPresetExperiment',
  127. organization,
  128. });
  129. }
  130. showUseTemplateBtn =
  131. showUseTemplateBtn && !!organization.experiments.MetricAlertPresetExperiment;
  132. return (
  133. <Feature
  134. features={
  135. isTransactionDataset
  136. ? ['organizations:incidents', 'organizations:performance-view']
  137. : isMetricAlert
  138. ? ['organizations:incidents']
  139. : []
  140. }
  141. requireAll
  142. organization={organization}
  143. hookName="feature-disabled:alert-wizard-performance"
  144. renderDisabled={renderNoAccess}
  145. >
  146. {({hasFeature}) => (
  147. <WizardButtonContainer
  148. onClick={() =>
  149. trackAdvancedAnalyticsEvent('alert_wizard.option_selected', {
  150. organization,
  151. alert_type: alertOption,
  152. })
  153. }
  154. >
  155. {showUseTemplateBtn && (
  156. <CreateAlertButton
  157. organization={organization}
  158. projectSlug={projectId}
  159. disabled={!hasFeature}
  160. priority="default"
  161. to={{
  162. pathname: to.pathname,
  163. query: {
  164. ...to.query,
  165. preset: supportedPreset.id,
  166. },
  167. }}
  168. onEnter={() => {
  169. trackAdvancedAnalyticsEvent('growth.metric_alert_preset_use_template', {
  170. organization,
  171. preset: supportedPreset.id,
  172. });
  173. }}
  174. hideIcon
  175. >
  176. {t('Use Template')}
  177. </CreateAlertButton>
  178. )}
  179. <CreateAlertButton
  180. organization={organization}
  181. projectSlug={projectId}
  182. disabled={!hasFeature}
  183. priority="primary"
  184. to={to}
  185. hideIcon
  186. >
  187. {t('Set Conditions')}
  188. </CreateAlertButton>
  189. </WizardButtonContainer>
  190. )}
  191. </Feature>
  192. );
  193. }
  194. render() {
  195. const {organization, params, projectId: _projectId, routes, location} = this.props;
  196. const {alertOption} = this.state;
  197. const projectId = params.projectId ?? _projectId;
  198. const title = t('Alert Creation Wizard');
  199. const panelContent = AlertWizardPanelContent[alertOption];
  200. return (
  201. <Fragment>
  202. <SentryDocumentTitle title={title} projectSlug={projectId} />
  203. <Layout.Header>
  204. <StyledHeaderContent>
  205. <BuilderBreadCrumbs
  206. organization={organization}
  207. projectSlug={projectId}
  208. title={t('Select Alert')}
  209. routes={routes}
  210. location={location}
  211. canChangeProject
  212. />
  213. <Layout.Title>{t('Select Alert')}</Layout.Title>
  214. </StyledHeaderContent>
  215. </Layout.Header>
  216. <Layout.Body>
  217. <Layout.Main fullWidth>
  218. <WizardBody>
  219. <WizardOptions>
  220. <CategoryTitle>{t('Errors')}</CategoryTitle>
  221. {getAlertWizardCategories(organization).map(
  222. ({categoryHeading, options}, i) => (
  223. <OptionsWrapper key={categoryHeading}>
  224. {i > 0 && <CategoryTitle>{categoryHeading} </CategoryTitle>}
  225. <RadioPanelGroup
  226. choices={options.map(alertType => {
  227. return [alertType, AlertWizardAlertNames[alertType]];
  228. })}
  229. onChange={this.handleChangeAlertOption}
  230. value={alertOption}
  231. label="alert-option"
  232. />
  233. </OptionsWrapper>
  234. )
  235. )}
  236. </WizardOptions>
  237. <WizardPanel visible={!!panelContent && !!alertOption}>
  238. <WizardPanelBody>
  239. <div>
  240. <PanelHeader>{AlertWizardAlertNames[alertOption]}</PanelHeader>
  241. <PanelBody withPadding>
  242. <PanelDescription>
  243. {panelContent.description}{' '}
  244. {panelContent.docsLink && (
  245. <ExternalLink href={panelContent.docsLink}>
  246. {t('Learn more')}
  247. </ExternalLink>
  248. )}
  249. </PanelDescription>
  250. <WizardImage src={panelContent.illustration} />
  251. <ExampleHeader>{t('Examples')}</ExampleHeader>
  252. <ExampleList symbol="bullet">
  253. {panelContent.examples.map((example, i) => (
  254. <ExampleItem key={i}>{example}</ExampleItem>
  255. ))}
  256. </ExampleList>
  257. </PanelBody>
  258. </div>
  259. <WizardFooter>{this.renderCreateAlertButton()}</WizardFooter>
  260. </WizardPanelBody>
  261. </WizardPanel>
  262. </WizardBody>
  263. </Layout.Main>
  264. </Layout.Body>
  265. </Fragment>
  266. );
  267. }
  268. }
  269. const StyledHeaderContent = styled(Layout.HeaderContent)`
  270. overflow: visible;
  271. `;
  272. const CategoryTitle = styled('h2')`
  273. font-weight: normal;
  274. font-size: ${p => p.theme.fontSizeExtraLarge};
  275. margin-bottom: ${space(1)} !important;
  276. `;
  277. const WizardBody = styled('div')`
  278. display: flex;
  279. padding-top: ${space(1)};
  280. `;
  281. const WizardOptions = styled('div')`
  282. flex: 3;
  283. margin-right: ${space(3)};
  284. padding-right: ${space(3)};
  285. max-width: 300px;
  286. `;
  287. const WizardImage = styled('img')`
  288. max-height: 300px;
  289. `;
  290. const WizardPanel = styled(Panel)<{visible?: boolean}>`
  291. max-width: 700px;
  292. position: sticky;
  293. top: 20px;
  294. flex: 5;
  295. display: flex;
  296. ${p => !p.visible && 'visibility: hidden'};
  297. flex-direction: column;
  298. align-items: start;
  299. align-self: flex-start;
  300. ${p => p.visible && 'animation: 0.6s pop ease forwards'};
  301. @keyframes pop {
  302. 0% {
  303. transform: translateY(30px);
  304. opacity: 0;
  305. }
  306. 100% {
  307. transform: translateY(0);
  308. opacity: 1;
  309. }
  310. }
  311. `;
  312. const ExampleList = styled(List)`
  313. margin-bottom: ${space(2)} !important;
  314. `;
  315. const WizardPanelBody = styled(PanelBody)`
  316. flex: 1;
  317. min-width: 100%;
  318. `;
  319. const PanelDescription = styled('p')`
  320. margin-bottom: ${space(2)};
  321. `;
  322. const ExampleHeader = styled('div')`
  323. margin: 0 0 ${space(1)} 0;
  324. font-size: ${p => p.theme.fontSizeLarge};
  325. `;
  326. const ExampleItem = styled(ListItem)`
  327. font-size: ${p => p.theme.fontSizeMedium};
  328. `;
  329. const OptionsWrapper = styled('div')`
  330. margin-bottom: ${space(4)};
  331. &:last-child {
  332. margin-bottom: 0;
  333. }
  334. `;
  335. const WizardFooter = styled('div')`
  336. border-top: 1px solid ${p => p.theme.border};
  337. padding: ${space(1.5)} ${space(1.5)} ${space(1.5)} ${space(1.5)};
  338. `;
  339. const WizardButtonContainer = styled('div')`
  340. display: flex;
  341. justify-content: flex-end;
  342. a:not(:last-child) {
  343. margin-right: ${space(1)};
  344. }
  345. `;
  346. export default withProjects(AlertWizard);