index.tsx 9.7 KB

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