index.tsx 9.8 KB

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