index.tsx 9.9 KB

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