create.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {Component, Fragment} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. import {Organization, Project} from 'sentry/types';
  10. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  11. import EventView from 'sentry/utils/discover/eventView';
  12. import {uniqueId} from 'sentry/utils/guid';
  13. import Teams from 'sentry/utils/teams';
  14. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  15. import IncidentRulesCreate from 'sentry/views/alerts/incidentRules/create';
  16. import IssueRuleEditor from 'sentry/views/alerts/issueRuleEditor';
  17. import {
  18. AlertType as WizardAlertType,
  19. AlertWizardAlertNames,
  20. WizardRuleTemplate,
  21. } from 'sentry/views/alerts/wizard/options';
  22. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  23. type RouteParams = {
  24. orgId: string;
  25. projectId: string;
  26. };
  27. type Props = RouteComponentProps<RouteParams, {}> & {
  28. hasMetricAlerts: boolean;
  29. organization: Organization;
  30. project: Project;
  31. };
  32. type AlertType = 'metric' | 'issue';
  33. type State = {
  34. alertType: AlertType;
  35. };
  36. class Create extends Component<Props, State> {
  37. state = this.getInitialState();
  38. getInitialState(): State {
  39. const {organization, location, project} = this.props;
  40. const {createFromDiscover, createFromWizard, aggregate, dataset, eventTypes} =
  41. location?.query ?? {};
  42. let alertType: AlertType = 'issue';
  43. // Alerts can only be created via create from discover or alert wizard
  44. if (createFromDiscover) {
  45. alertType = 'metric';
  46. } else if (createFromWizard) {
  47. if (aggregate && dataset && eventTypes) {
  48. alertType = 'metric';
  49. } else {
  50. // Just to be explicit
  51. alertType = 'issue';
  52. }
  53. } else {
  54. browserHistory.replace(
  55. `/organizations/${organization.slug}/alerts/${project.slug}/wizard`
  56. );
  57. }
  58. return {alertType};
  59. }
  60. componentDidMount() {
  61. const {organization, project} = this.props;
  62. trackAdvancedAnalyticsEvent('new_alert_rule.viewed', {
  63. organization,
  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. <Teams provideUserTeams>
  116. {({teams, initiallyLoaded}) =>
  117. initiallyLoaded ? (
  118. <Fragment>
  119. {(!hasMetricAlerts || alertType === 'issue') && (
  120. <IssueRuleEditor
  121. {...this.props}
  122. project={project}
  123. userTeamIds={teams.map(({id}) => id)}
  124. />
  125. )}
  126. {hasMetricAlerts && alertType === 'metric' && (
  127. <IncidentRulesCreate
  128. {...this.props}
  129. eventView={eventView}
  130. wizardTemplate={wizardTemplate}
  131. sessionId={this.sessionId}
  132. project={project}
  133. isCustomMetric={wizardAlertType === 'custom'}
  134. userTeamIds={teams.map(({id}) => id)}
  135. />
  136. )}
  137. </Fragment>
  138. ) : (
  139. <LoadingIndicator />
  140. )
  141. }
  142. </Teams>
  143. </StyledLayoutMain>
  144. </AlertConditionsBody>
  145. </Fragment>
  146. );
  147. }
  148. }
  149. const AlertConditionsBody = styled(Layout.Body)`
  150. margin-bottom: -${space(3)};
  151. `;
  152. const StyledLayoutMain = styled(Layout.Main)`
  153. max-width: 1000px;
  154. `;
  155. const StyledHeaderContent = styled(Layout.HeaderContent)`
  156. overflow: visible;
  157. `;
  158. export default Create;