index.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Feature from 'app/components/acl/feature';
  5. import FeatureDisabled from 'app/components/acl/featureDisabled';
  6. import CreateAlertButton from 'app/components/createAlertButton';
  7. import Hovercard from 'app/components/hovercard';
  8. import * as Layout from 'app/components/layouts/thirds';
  9. import ExternalLink from 'app/components/links/externalLink';
  10. import List from 'app/components/list';
  11. import ListItem from 'app/components/list/listItem';
  12. import {Panel, PanelBody, PanelHeader} from 'app/components/panels';
  13. import SentryDocumentTitle from 'app/components/sentryDocumentTitle';
  14. import {t} from 'app/locale';
  15. import space from 'app/styles/space';
  16. import {Organization, Project} from 'app/types';
  17. import {trackAnalyticsEvent} from 'app/utils/analytics';
  18. import BuilderBreadCrumbs from 'app/views/alerts/builder/builderBreadCrumbs';
  19. import {Dataset} from 'app/views/alerts/incidentRules/types';
  20. import {
  21. AlertType,
  22. AlertWizardAlertNames,
  23. AlertWizardOptions,
  24. AlertWizardPanelContent,
  25. AlertWizardRuleTemplates,
  26. } from './options';
  27. import RadioPanelGroup from './radioPanelGroup';
  28. type RouteParams = {
  29. orgId: string;
  30. projectId: string;
  31. };
  32. type Props = RouteComponentProps<RouteParams, {}> & {
  33. organization: Organization;
  34. project: Project;
  35. };
  36. type State = {
  37. alertOption: AlertType;
  38. };
  39. const DEFAULT_ALERT_OPTION = 'issues';
  40. class AlertWizard extends Component<Props, State> {
  41. state: State = {
  42. alertOption: DEFAULT_ALERT_OPTION,
  43. };
  44. componentDidMount() {
  45. // capture landing on the alert wizard page and viewing the issue alert by default
  46. const {organization} = this.props;
  47. trackAnalyticsEvent({
  48. eventKey: 'alert_wizard.option_viewed',
  49. eventName: 'Alert Wizard: Option Viewed',
  50. organization_id: organization.id,
  51. alert_type: DEFAULT_ALERT_OPTION,
  52. });
  53. }
  54. handleChangeAlertOption = (alertOption: AlertType) => {
  55. const {organization} = this.props;
  56. this.setState({alertOption});
  57. trackAnalyticsEvent({
  58. eventKey: 'alert_wizard.option_viewed',
  59. eventName: 'Alert Wizard: Option Viewed',
  60. organization_id: organization.id,
  61. alert_type: alertOption,
  62. });
  63. };
  64. renderCreateAlertButton() {
  65. const {
  66. organization,
  67. location,
  68. params: {projectId},
  69. } = this.props;
  70. const {alertOption} = this.state;
  71. const metricRuleTemplate = AlertWizardRuleTemplates[alertOption];
  72. const isMetricAlert = !!metricRuleTemplate;
  73. const isTransactionDataset = metricRuleTemplate?.dataset === Dataset.TRANSACTIONS;
  74. const to = {
  75. pathname: `/organizations/${organization.slug}/alerts/${projectId}/new/`,
  76. query: {
  77. ...(metricRuleTemplate && metricRuleTemplate),
  78. createFromWizard: true,
  79. referrer: location?.query?.referrer,
  80. },
  81. };
  82. const noFeatureMessage = t('Requires incidents feature.');
  83. const renderNoAccess = p => (
  84. <Hovercard
  85. body={
  86. <FeatureDisabled
  87. features={p.features}
  88. hideHelpToggle
  89. message={noFeatureMessage}
  90. featureName={noFeatureMessage}
  91. />
  92. }
  93. >
  94. {p.children(p)}
  95. </Hovercard>
  96. );
  97. return (
  98. <Feature
  99. features={
  100. isTransactionDataset
  101. ? ['incidents', 'performance-view']
  102. : isMetricAlert
  103. ? ['incidents']
  104. : []
  105. }
  106. requireAll
  107. organization={organization}
  108. hookName="feature-disabled:alert-wizard-performance"
  109. renderDisabled={renderNoAccess}
  110. >
  111. {({hasFeature}) => (
  112. <WizardButtonContainer
  113. onClick={() =>
  114. trackAnalyticsEvent({
  115. eventKey: 'alert_wizard.option_selected',
  116. eventName: 'Alert Wizard: Option Selected',
  117. organization_id: organization.id,
  118. alert_type: alertOption,
  119. })
  120. }
  121. >
  122. <CreateAlertButton
  123. organization={organization}
  124. projectSlug={projectId}
  125. disabled={!hasFeature}
  126. priority="primary"
  127. to={to}
  128. hideIcon
  129. >
  130. {t('Set Conditions')}
  131. </CreateAlertButton>
  132. </WizardButtonContainer>
  133. )}
  134. </Feature>
  135. );
  136. }
  137. render() {
  138. const {
  139. organization,
  140. params: {projectId},
  141. routes,
  142. location,
  143. } = this.props;
  144. const {alertOption} = this.state;
  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. orgSlug={organization.slug}
  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. <StyledLayoutBody>
  164. <Layout.Main fullWidth>
  165. <WizardBody>
  166. <WizardOptions>
  167. <Styledh2>{t('Errors')}</Styledh2>
  168. {AlertWizardOptions.map(({categoryHeading, options}, i) => (
  169. <OptionsWrapper key={categoryHeading}>
  170. {i > 0 && <Styledh2>{categoryHeading}</Styledh2>}
  171. <RadioPanelGroup
  172. choices={options.map(alertType => {
  173. return [alertType, AlertWizardAlertNames[alertType]];
  174. })}
  175. onChange={this.handleChangeAlertOption}
  176. value={alertOption}
  177. label="alert-option"
  178. />
  179. </OptionsWrapper>
  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. </StyledLayoutBody>
  210. </Fragment>
  211. );
  212. }
  213. }
  214. const StyledLayoutBody = styled(Layout.Body)`
  215. margin-bottom: -${space(3)};
  216. `;
  217. const StyledHeaderContent = styled(Layout.HeaderContent)`
  218. overflow: visible;
  219. `;
  220. const Styledh2 = styled('h2')`
  221. font-weight: normal;
  222. font-size: ${p => p.theme.fontSizeExtraLarge};
  223. margin-bottom: ${space(1)} !important;
  224. `;
  225. const WizardBody = styled('div')`
  226. display: flex;
  227. padding-top: ${space(1)};
  228. `;
  229. const WizardOptions = styled('div')`
  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 OptionsWrapper = styled('div')`
  278. margin-bottom: ${space(4)};
  279. &:last-child {
  280. margin-bottom: 0;
  281. }
  282. `;
  283. const WizardFooter = styled('div')`
  284. border-top: 1px solid ${p => p.theme.border};
  285. padding: ${space(1.5)} ${space(1.5)} ${space(1.5)} ${space(1.5)};
  286. `;
  287. const WizardButtonContainer = styled('div')`
  288. display: flex;
  289. justify-content: flex-end;
  290. `;
  291. export default AlertWizard;