index.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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: any) => (
  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
  109. ? AlertRuleType.METRIC
  110. : alertOption === 'uptime_monitor'
  111. ? AlertRuleType.UPTIME
  112. : alertOption === 'crons_monitor'
  113. ? AlertRuleType.CRONS
  114. : AlertRuleType.ISSUE
  115. }/`,
  116. query: {
  117. ...(metricRuleTemplate ? metricRuleTemplate : {}),
  118. project: projectSlug,
  119. referrer: location?.query?.referrer,
  120. },
  121. }}
  122. hideIcon
  123. >
  124. {t('Set Conditions')}
  125. </CreateAlertButton>
  126. </WizardButtonContainer>
  127. )}
  128. </Feature>
  129. );
  130. }
  131. const panelContent = AlertWizardPanelContent[alertOption];
  132. return (
  133. <Layout.Page>
  134. <SentryDocumentTitle title={t('Alert Creation Wizard')} projectSlug={projectSlug} />
  135. <Layout.Header>
  136. <StyledHeaderContent>
  137. <BuilderBreadCrumbs
  138. organization={organization}
  139. projectSlug={projectSlug}
  140. title={t('Select Alert')}
  141. />
  142. <Layout.Title>{t('Select Alert')}</Layout.Title>
  143. </StyledHeaderContent>
  144. </Layout.Header>
  145. <Layout.Body>
  146. <Layout.Main fullWidth>
  147. <WizardBody>
  148. <WizardOptions>
  149. {getAlertWizardCategories(organization).map(
  150. ({categoryHeading, options}: any) => (
  151. <div key={categoryHeading}>
  152. <CategoryTitle>{categoryHeading} </CategoryTitle>
  153. <WizardGroupedOptions
  154. choices={options.map((alertType: any) => {
  155. return [
  156. alertType,
  157. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  158. AlertWizardAlertNames[alertType],
  159. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  160. AlertWizardExtraContent[alertType],
  161. ];
  162. })}
  163. onChange={option => handleChangeAlertOption(option as AlertType)}
  164. value={alertOption}
  165. label="alert-option"
  166. />
  167. </div>
  168. )
  169. )}
  170. </WizardOptions>
  171. <WizardPanel visible={!!panelContent && !!alertOption}>
  172. <WizardPanelBody>
  173. <div>
  174. <PanelHeader>{AlertWizardAlertNames[alertOption]}</PanelHeader>
  175. <PanelBody withPadding>
  176. <PanelDescription>
  177. {panelContent.description}{' '}
  178. {panelContent.docsLink && (
  179. <ExternalLink href={panelContent.docsLink}>
  180. {t('Learn more')}
  181. </ExternalLink>
  182. )}
  183. </PanelDescription>
  184. <WizardImage src={panelContent.illustration} />
  185. <ExampleHeader>{t('Examples')}</ExampleHeader>
  186. <ExampleList symbol="bullet">
  187. {panelContent.examples.map((example, i) => (
  188. <ExampleItem key={i}>{example}</ExampleItem>
  189. ))}
  190. </ExampleList>
  191. </PanelBody>
  192. </div>
  193. <WizardFooter>{renderCreateAlertButton()}</WizardFooter>
  194. </WizardPanelBody>
  195. </WizardPanel>
  196. </WizardBody>
  197. </Layout.Main>
  198. </Layout.Body>
  199. </Layout.Page>
  200. );
  201. }
  202. const StyledHeaderContent = styled(Layout.HeaderContent)`
  203. overflow: visible;
  204. `;
  205. const CategoryTitle = styled('h2')`
  206. font-weight: ${p => p.theme.fontWeightNormal};
  207. font-size: ${p => p.theme.fontSizeExtraLarge};
  208. margin-bottom: ${space(1)} !important;
  209. `;
  210. const WizardBody = styled('div')`
  211. display: flex;
  212. padding-top: ${space(1)};
  213. `;
  214. const WizardOptions = styled('div')`
  215. display: flex;
  216. flex-direction: column;
  217. gap: ${space(4)};
  218. flex: 3;
  219. margin-right: ${space(3)};
  220. padding-right: ${space(3)};
  221. max-width: 300px;
  222. `;
  223. const WizardImage = styled('img')`
  224. max-height: 300px;
  225. margin-bottom: ${space(2)};
  226. `;
  227. const WizardPanel = styled(Panel)<{visible?: boolean}>`
  228. max-width: 700px;
  229. position: sticky;
  230. top: 20px;
  231. flex: 5;
  232. display: flex;
  233. ${p => !p.visible && 'visibility: hidden'};
  234. flex-direction: column;
  235. align-items: start;
  236. align-self: flex-start;
  237. ${p => p.visible && 'animation: 0.6s pop ease forwards'};
  238. @keyframes pop {
  239. 0% {
  240. transform: translateY(30px);
  241. opacity: 0;
  242. }
  243. 100% {
  244. transform: translateY(0);
  245. opacity: 1;
  246. }
  247. }
  248. `;
  249. const ExampleList = styled(List)`
  250. margin-bottom: ${space(2)} !important;
  251. `;
  252. const WizardPanelBody = styled(PanelBody)`
  253. flex: 1;
  254. min-width: 100%;
  255. `;
  256. const PanelDescription = styled('p')`
  257. margin-bottom: ${space(2)};
  258. `;
  259. const ExampleHeader = styled('div')`
  260. margin: 0 0 ${space(1)} 0;
  261. font-size: ${p => p.theme.fontSizeLarge};
  262. `;
  263. const ExampleItem = styled(ListItem)`
  264. font-size: ${p => p.theme.fontSizeMedium};
  265. `;
  266. const WizardFooter = styled('div')`
  267. border-top: 1px solid ${p => p.theme.border};
  268. padding: ${space(1.5)} ${space(1.5)} ${space(1.5)} ${space(1.5)};
  269. `;
  270. const WizardButtonContainer = styled('div')`
  271. display: flex;
  272. justify-content: flex-end;
  273. a:not(:last-child) {
  274. margin-right: ${space(1)};
  275. }
  276. `;
  277. const WizardGroupedOptions = styled(RadioPanelGroup)`
  278. label {
  279. grid-template-columns: repeat(3, max-content);
  280. }
  281. `;
  282. export default AlertWizard;