index.tsx 9.7 KB

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