index.tsx 9.5 KB

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