index.tsx 11 KB

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