index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {useState} 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} from 'sentry/types';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  19. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  20. import {AlertRuleType} from 'sentry/views/alerts/types';
  21. import {
  22. AlertType,
  23. AlertWizardAlertNames,
  24. AlertWizardRuleTemplates,
  25. getAlertWizardCategories,
  26. WizardRuleTemplate,
  27. } from './options';
  28. import {AlertWizardPanelContent} from './panelContent';
  29. import RadioPanelGroup from './radioPanelGroup';
  30. type RouteParams = {
  31. projectId?: string;
  32. };
  33. type AlertWizardProps = RouteComponentProps<RouteParams, {}> & {
  34. organization: Organization;
  35. projectId: string;
  36. };
  37. const DEFAULT_ALERT_OPTION = 'issues';
  38. function AlertWizard({organization, params, location, projectId}: AlertWizardProps) {
  39. const [alertOption, setAlertOption] = useState<AlertType>(
  40. location.query.alert_option in AlertWizardAlertNames
  41. ? location.query.alert_option
  42. : DEFAULT_ALERT_OPTION
  43. );
  44. const projectSlug = params.projectId ?? projectId;
  45. const handleChangeAlertOption = (option: AlertType) => {
  46. setAlertOption(option);
  47. };
  48. function renderCreateAlertButton() {
  49. let metricRuleTemplate: Readonly<WizardRuleTemplate> | undefined =
  50. AlertWizardRuleTemplates[alertOption];
  51. const isMetricAlert = !!metricRuleTemplate;
  52. const isTransactionDataset = metricRuleTemplate?.dataset === Dataset.TRANSACTIONS;
  53. if (
  54. organization.features.includes('alert-crash-free-metrics') &&
  55. metricRuleTemplate?.dataset === Dataset.SESSIONS
  56. ) {
  57. metricRuleTemplate = {...metricRuleTemplate, dataset: Dataset.METRICS};
  58. }
  59. const renderNoAccess = p => (
  60. <Hovercard
  61. body={
  62. <FeatureDisabled
  63. features={p.features}
  64. hideHelpToggle
  65. featureName={t('Metric Alerts')}
  66. />
  67. }
  68. >
  69. {p.children(p)}
  70. </Hovercard>
  71. );
  72. return (
  73. <Feature
  74. features={
  75. isTransactionDataset
  76. ? ['organizations:incidents', 'organizations:performance-view']
  77. : isMetricAlert
  78. ? ['organizations:incidents']
  79. : []
  80. }
  81. requireAll
  82. organization={organization}
  83. hookName="feature-disabled:alert-wizard-performance"
  84. renderDisabled={renderNoAccess}
  85. >
  86. {({hasFeature}) => (
  87. <WizardButtonContainer
  88. onClick={() =>
  89. trackAnalytics('alert_wizard.option_selected', {
  90. organization,
  91. alert_type: alertOption,
  92. })
  93. }
  94. >
  95. <CreateAlertButton
  96. organization={organization}
  97. projectSlug={projectSlug}
  98. disabled={!hasFeature}
  99. priority="primary"
  100. to={{
  101. pathname: `/organizations/${organization.slug}/alerts/new/${
  102. isMetricAlert ? AlertRuleType.METRIC : AlertRuleType.ISSUE
  103. }/`,
  104. query: {
  105. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  106. project: projectSlug,
  107. referrer: location?.query?.referrer,
  108. },
  109. }}
  110. hideIcon
  111. >
  112. {t('Set Conditions')}
  113. </CreateAlertButton>
  114. </WizardButtonContainer>
  115. )}
  116. </Feature>
  117. );
  118. }
  119. const panelContent = AlertWizardPanelContent[alertOption];
  120. return (
  121. <Layout.Page>
  122. <SentryDocumentTitle title={t('Alert Creation Wizard')} projectSlug={projectSlug} />
  123. <Layout.Header>
  124. <StyledHeaderContent>
  125. <BuilderBreadCrumbs
  126. organization={organization}
  127. projectSlug={projectSlug}
  128. title={t('Select Alert')}
  129. />
  130. <Layout.Title>{t('Select Alert')}</Layout.Title>
  131. </StyledHeaderContent>
  132. </Layout.Header>
  133. <Layout.Body>
  134. <Layout.Main fullWidth>
  135. <WizardBody>
  136. <WizardOptions>
  137. {getAlertWizardCategories(organization).map(
  138. ({categoryHeading, options}) => (
  139. <div key={categoryHeading}>
  140. <CategoryTitle>{categoryHeading} </CategoryTitle>
  141. <RadioPanelGroup
  142. choices={options.map(alertType => {
  143. return [alertType, AlertWizardAlertNames[alertType]];
  144. })}
  145. onChange={handleChangeAlertOption}
  146. value={alertOption}
  147. label="alert-option"
  148. />
  149. </div>
  150. )
  151. )}
  152. </WizardOptions>
  153. <WizardPanel visible={!!panelContent && !!alertOption}>
  154. <WizardPanelBody>
  155. <div>
  156. <PanelHeader>{AlertWizardAlertNames[alertOption]}</PanelHeader>
  157. <PanelBody withPadding>
  158. <PanelDescription>
  159. {panelContent.description}{' '}
  160. {panelContent.docsLink && (
  161. <ExternalLink href={panelContent.docsLink}>
  162. {t('Learn more')}
  163. </ExternalLink>
  164. )}
  165. </PanelDescription>
  166. <WizardImage src={panelContent.illustration} />
  167. <ExampleHeader>{t('Examples')}</ExampleHeader>
  168. <ExampleList symbol="bullet">
  169. {panelContent.examples.map((example, i) => (
  170. <ExampleItem key={i}>{example}</ExampleItem>
  171. ))}
  172. </ExampleList>
  173. </PanelBody>
  174. </div>
  175. <WizardFooter>{renderCreateAlertButton()}</WizardFooter>
  176. </WizardPanelBody>
  177. </WizardPanel>
  178. </WizardBody>
  179. </Layout.Main>
  180. </Layout.Body>
  181. </Layout.Page>
  182. );
  183. }
  184. const StyledHeaderContent = styled(Layout.HeaderContent)`
  185. overflow: visible;
  186. `;
  187. const CategoryTitle = styled('h2')`
  188. font-weight: normal;
  189. font-size: ${p => p.theme.fontSizeExtraLarge};
  190. margin-bottom: ${space(1)} !important;
  191. `;
  192. const WizardBody = styled('div')`
  193. display: flex;
  194. padding-top: ${space(1)};
  195. `;
  196. const WizardOptions = styled('div')`
  197. display: flex;
  198. flex-direction: column;
  199. gap: ${space(4)};
  200. flex: 3;
  201. margin-right: ${space(3)};
  202. padding-right: ${space(3)};
  203. max-width: 300px;
  204. `;
  205. const WizardImage = styled('img')`
  206. max-height: 300px;
  207. `;
  208. const WizardPanel = styled(Panel)<{visible?: boolean}>`
  209. max-width: 700px;
  210. position: sticky;
  211. top: 20px;
  212. flex: 5;
  213. display: flex;
  214. ${p => !p.visible && 'visibility: hidden'};
  215. flex-direction: column;
  216. align-items: start;
  217. align-self: flex-start;
  218. ${p => p.visible && 'animation: 0.6s pop ease forwards'};
  219. @keyframes pop {
  220. 0% {
  221. transform: translateY(30px);
  222. opacity: 0;
  223. }
  224. 100% {
  225. transform: translateY(0);
  226. opacity: 1;
  227. }
  228. }
  229. `;
  230. const ExampleList = styled(List)`
  231. margin-bottom: ${space(2)} !important;
  232. `;
  233. const WizardPanelBody = styled(PanelBody)`
  234. flex: 1;
  235. min-width: 100%;
  236. `;
  237. const PanelDescription = styled('p')`
  238. margin-bottom: ${space(2)};
  239. `;
  240. const ExampleHeader = styled('div')`
  241. margin: 0 0 ${space(1)} 0;
  242. font-size: ${p => p.theme.fontSizeLarge};
  243. `;
  244. const ExampleItem = styled(ListItem)`
  245. font-size: ${p => p.theme.fontSizeMedium};
  246. `;
  247. const WizardFooter = styled('div')`
  248. border-top: 1px solid ${p => p.theme.border};
  249. padding: ${space(1.5)} ${space(1.5)} ${space(1.5)} ${space(1.5)};
  250. `;
  251. const WizardButtonContainer = styled('div')`
  252. display: flex;
  253. justify-content: flex-end;
  254. a:not(:last-child) {
  255. margin-right: ${space(1)};
  256. }
  257. `;
  258. export default AlertWizard;