index.tsx 9.8 KB

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