index.tsx 9.6 KB

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