index.tsx 10 KB

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