index.tsx 8.5 KB

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