create.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {Component, Fragment} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Layout from 'app/components/layouts/thirds';
  5. import SentryDocumentTitle from 'app/components/sentryDocumentTitle';
  6. import {t} from 'app/locale';
  7. import space from 'app/styles/space';
  8. import {Organization, Project} from 'app/types';
  9. import {trackAnalyticsEvent} from 'app/utils/analytics';
  10. import EventView from 'app/utils/discover/eventView';
  11. import {uniqueId} from 'app/utils/guid';
  12. import BuilderBreadCrumbs from 'app/views/alerts/builder/builderBreadCrumbs';
  13. import IncidentRulesCreate from 'app/views/alerts/incidentRules/create';
  14. import IssueRuleEditor from 'app/views/alerts/issueRuleEditor';
  15. import {
  16. AlertType as WizardAlertType,
  17. AlertWizardAlertNames,
  18. WizardRuleTemplate,
  19. } from 'app/views/alerts/wizard/options';
  20. import {getAlertTypeFromAggregateDataset} from 'app/views/alerts/wizard/utils';
  21. type RouteParams = {
  22. orgId: string;
  23. projectId: string;
  24. };
  25. type Props = RouteComponentProps<RouteParams, {}> & {
  26. organization: Organization;
  27. project: Project;
  28. hasMetricAlerts: boolean;
  29. };
  30. type AlertType = 'metric' | 'issue';
  31. type State = {
  32. alertType: AlertType;
  33. };
  34. class Create extends Component<Props, State> {
  35. state = this.getInitialState();
  36. getInitialState(): State {
  37. const {organization, location, project} = this.props;
  38. const {createFromDiscover, createFromWizard, aggregate, dataset, eventTypes} =
  39. location?.query ?? {};
  40. let alertType: AlertType = 'issue';
  41. // Alerts can only be created via create from discover or alert wizard
  42. if (createFromDiscover) {
  43. alertType = 'metric';
  44. } else if (createFromWizard) {
  45. if (aggregate && dataset && eventTypes) {
  46. alertType = 'metric';
  47. } else {
  48. // Just to be explicit
  49. alertType = 'issue';
  50. }
  51. } else {
  52. browserHistory.replace(
  53. `/organizations/${organization.slug}/alerts/${project.slug}/wizard`
  54. );
  55. }
  56. return {alertType};
  57. }
  58. componentDidMount() {
  59. const {organization, project} = this.props;
  60. trackAnalyticsEvent({
  61. eventKey: 'new_alert_rule.viewed',
  62. eventName: 'New Alert Rule: Viewed',
  63. organization_id: organization.id,
  64. project_id: project.id,
  65. session_id: this.sessionId,
  66. alert_type: this.state.alertType,
  67. });
  68. }
  69. /** Used to track analytics within one visit to the creation page */
  70. sessionId = uniqueId();
  71. render() {
  72. const {
  73. hasMetricAlerts,
  74. organization,
  75. project,
  76. params: {projectId},
  77. location,
  78. routes,
  79. } = this.props;
  80. const {alertType} = this.state;
  81. const {aggregate, dataset, eventTypes, createFromWizard, createFromDiscover} =
  82. location?.query ?? {};
  83. const wizardTemplate: WizardRuleTemplate = {aggregate, dataset, eventTypes};
  84. const eventView = createFromDiscover ? EventView.fromLocation(location) : undefined;
  85. let wizardAlertType: undefined | WizardAlertType;
  86. if (createFromWizard && alertType === 'metric') {
  87. wizardAlertType = wizardTemplate
  88. ? getAlertTypeFromAggregateDataset(wizardTemplate)
  89. : 'issues';
  90. }
  91. const title = t('New Alert Rule');
  92. return (
  93. <Fragment>
  94. <SentryDocumentTitle title={title} projectSlug={projectId} />
  95. <Layout.Header>
  96. <StyledHeaderContent>
  97. <BuilderBreadCrumbs
  98. orgSlug={organization.slug}
  99. alertName={t('Set Conditions')}
  100. title={wizardAlertType ? t('Select Alert') : title}
  101. projectSlug={projectId}
  102. routes={routes}
  103. location={location}
  104. canChangeProject
  105. />
  106. <Layout.Title>
  107. {wizardAlertType
  108. ? `${t('Set Conditions for')} ${AlertWizardAlertNames[wizardAlertType]}`
  109. : title}
  110. </Layout.Title>
  111. </StyledHeaderContent>
  112. </Layout.Header>
  113. <AlertConditionsBody>
  114. <StyledLayoutMain fullWidth>
  115. {(!hasMetricAlerts || alertType === 'issue') && (
  116. <IssueRuleEditor {...this.props} project={project} />
  117. )}
  118. {hasMetricAlerts && alertType === 'metric' && (
  119. <IncidentRulesCreate
  120. {...this.props}
  121. eventView={eventView}
  122. wizardTemplate={wizardTemplate}
  123. sessionId={this.sessionId}
  124. project={project}
  125. isCustomMetric={wizardAlertType === 'custom'}
  126. />
  127. )}
  128. </StyledLayoutMain>
  129. </AlertConditionsBody>
  130. </Fragment>
  131. );
  132. }
  133. }
  134. const AlertConditionsBody = styled(Layout.Body)`
  135. margin-bottom: -${space(3)};
  136. `;
  137. const StyledLayoutMain = styled(Layout.Main)`
  138. max-width: 1000px;
  139. `;
  140. const StyledHeaderContent = styled(Layout.HeaderContent)`
  141. overflow: visible;
  142. `;
  143. export default Create;