index.tsx 9.4 KB

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