index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Feature from 'sentry/components/acl/feature';
  4. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  5. import CreateAlertButton from 'sentry/components/createAlertButton';
  6. import {Hovercard} from 'sentry/components/hovercard';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import List from 'sentry/components/list';
  10. import ListItem from 'sentry/components/list/listItem';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import PanelHeader from 'sentry/components/panels/panelHeader';
  14. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  18. import type {Organization} from 'sentry/types/organization';
  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 type {AlertType, WizardRuleTemplate} from './options';
  24. import {
  25. AlertWizardAlertNames,
  26. AlertWizardExtraContent,
  27. AlertWizardRuleTemplates,
  28. getAlertWizardCategories,
  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 theres anything using the legacy sessions dataset, we need to convert it to metrics
  56. if (metricRuleTemplate?.dataset === Dataset.SESSIONS) {
  57. metricRuleTemplate = {...metricRuleTemplate, dataset: Dataset.METRICS};
  58. }
  59. if (metricRuleTemplate?.dataset === Dataset.ERRORS) {
  60. // Pre-fill is:unresolved for error metric alerts
  61. // Filters out events in issues that are archived or resolved
  62. metricRuleTemplate = {...metricRuleTemplate, query: 'is:unresolved'};
  63. }
  64. const renderNoAccess = p => (
  65. <Hovercard
  66. body={
  67. <FeatureDisabled
  68. features={p.features}
  69. hideHelpToggle
  70. featureName={t('Metric Alerts')}
  71. />
  72. }
  73. >
  74. {p.children(p)}
  75. </Hovercard>
  76. );
  77. return (
  78. <Feature
  79. features={
  80. isTransactionDataset
  81. ? ['organizations:incidents', 'organizations:performance-view']
  82. : isMetricAlert
  83. ? ['organizations:incidents']
  84. : []
  85. }
  86. requireAll
  87. organization={organization}
  88. hookName="feature-disabled:alert-wizard-performance"
  89. renderDisabled={renderNoAccess}
  90. >
  91. {({hasFeature}) => (
  92. <WizardButtonContainer
  93. onClick={() =>
  94. trackAnalytics('alert_wizard.option_selected', {
  95. organization,
  96. alert_type: alertOption,
  97. })
  98. }
  99. >
  100. <CreateAlertButton
  101. organization={organization}
  102. projectSlug={projectSlug}
  103. disabled={!hasFeature}
  104. priority="primary"
  105. to={{
  106. pathname: `/organizations/${organization.slug}/alerts/new/${
  107. isMetricAlert
  108. ? AlertRuleType.METRIC
  109. : alertOption === 'uptime_monitor'
  110. ? AlertRuleType.UPTIME
  111. : AlertRuleType.ISSUE
  112. }/`,
  113. query: {
  114. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  115. project: projectSlug,
  116. referrer: location?.query?.referrer,
  117. },
  118. }}
  119. hideIcon
  120. >
  121. {t('Set Conditions')}
  122. </CreateAlertButton>
  123. </WizardButtonContainer>
  124. )}
  125. </Feature>
  126. );
  127. }
  128. const panelContent = AlertWizardPanelContent[alertOption];
  129. return (
  130. <Layout.Page>
  131. <SentryDocumentTitle title={t('Alert Creation Wizard')} projectSlug={projectSlug} />
  132. <Layout.Header>
  133. <StyledHeaderContent>
  134. <BuilderBreadCrumbs
  135. organization={organization}
  136. projectSlug={projectSlug}
  137. title={t('Select Alert')}
  138. />
  139. <Layout.Title>{t('Select Alert')}</Layout.Title>
  140. </StyledHeaderContent>
  141. </Layout.Header>
  142. <Layout.Body>
  143. <Layout.Main fullWidth>
  144. <WizardBody>
  145. <WizardOptions>
  146. {getAlertWizardCategories(organization).map(
  147. ({categoryHeading, options}) => (
  148. <div key={categoryHeading}>
  149. <CategoryTitle>{categoryHeading} </CategoryTitle>
  150. <WizardGroupedOptions
  151. choices={options.map(alertType => {
  152. return [
  153. alertType,
  154. AlertWizardAlertNames[alertType],
  155. AlertWizardExtraContent[alertType],
  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;