create.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import {Component, Fragment} from 'react';
  2. import {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 {Organization, Project} from 'sentry/types';
  9. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import {uniqueId} from 'sentry/utils/guid';
  12. import Teams from 'sentry/utils/teams';
  13. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  14. import IssueRuleEditor from 'sentry/views/alerts/rules/issue';
  15. import MetricRulesCreate from 'sentry/views/alerts/rules/metric/create';
  16. import MetricRulesDuplicate from 'sentry/views/alerts/rules/metric/duplicate';
  17. import {AlertRuleType} from 'sentry/views/alerts/types';
  18. import {
  19. AlertType as WizardAlertType,
  20. AlertWizardAlertNames,
  21. DEFAULT_WIZARD_TEMPLATE,
  22. WizardRuleTemplate,
  23. } from 'sentry/views/alerts/wizard/options';
  24. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  25. type RouteParams = {
  26. orgId: string;
  27. alertType?: AlertRuleType;
  28. projectId?: string;
  29. };
  30. type Props = RouteComponentProps<RouteParams, {}> & {
  31. hasMetricAlerts: boolean;
  32. organization: Organization;
  33. project: Project;
  34. };
  35. type State = {
  36. alertType: AlertRuleType;
  37. };
  38. class Create extends Component<Props, State> {
  39. state = this.getInitialState();
  40. getInitialState(): State {
  41. const {organization, location, project, params, router} = this.props;
  42. const {aggregate, dataset, eventTypes, createFromDuplicate} = location?.query ?? {};
  43. const alertType = params.alertType || AlertRuleType.METRIC;
  44. // TODO(taylangocmen): Remove redirect with aggregate && dataset && eventTypes, init from template
  45. if (
  46. alertType === AlertRuleType.METRIC &&
  47. !(aggregate && dataset && eventTypes) &&
  48. !createFromDuplicate
  49. ) {
  50. router.replace({
  51. ...location,
  52. pathname: `/organizations/${organization.slug}/alerts/new/${alertType}`,
  53. query: {
  54. ...location.query,
  55. ...DEFAULT_WIZARD_TEMPLATE,
  56. project: project.slug,
  57. },
  58. });
  59. }
  60. return {alertType};
  61. }
  62. componentDidMount() {
  63. const {organization, project} = this.props;
  64. trackAdvancedAnalyticsEvent('new_alert_rule.viewed', {
  65. organization,
  66. project_id: project.id,
  67. session_id: this.sessionId,
  68. alert_type: this.state.alertType,
  69. duplicate_rule: this.isDuplicateRule ? 'true' : 'false',
  70. wizard_v3: 'true',
  71. });
  72. }
  73. /** Used to track analytics within one visit to the creation page */
  74. sessionId = uniqueId();
  75. get isDuplicateRule(): boolean {
  76. const {location} = this.props;
  77. const createFromDuplicate = location?.query.createFromDuplicate === 'true';
  78. return createFromDuplicate && location?.query.duplicateRuleId;
  79. }
  80. render() {
  81. const {hasMetricAlerts, organization, project, location, routes} = this.props;
  82. const {alertType} = this.state;
  83. const {aggregate, dataset, eventTypes, createFromWizard, createFromDiscover} =
  84. location?.query ?? {};
  85. const wizardTemplate: WizardRuleTemplate = {
  86. aggregate: aggregate ?? DEFAULT_WIZARD_TEMPLATE.aggregate,
  87. dataset: dataset ?? DEFAULT_WIZARD_TEMPLATE.dataset,
  88. eventTypes: eventTypes ?? DEFAULT_WIZARD_TEMPLATE.eventTypes,
  89. };
  90. const eventView = createFromDiscover ? EventView.fromLocation(location) : undefined;
  91. let wizardAlertType: undefined | WizardAlertType;
  92. if (createFromWizard && alertType === AlertRuleType.METRIC) {
  93. wizardAlertType = wizardTemplate
  94. ? getAlertTypeFromAggregateDataset(wizardTemplate)
  95. : 'issues';
  96. }
  97. const title = t('New Alert Rule');
  98. return (
  99. <Fragment>
  100. <SentryDocumentTitle title={title} projectSlug={project.slug} />
  101. <Layout.Header>
  102. <StyledHeaderContent>
  103. <BuilderBreadCrumbs
  104. organization={organization}
  105. alertName={t('Set Conditions')}
  106. title={wizardAlertType ? t('Select Alert') : title}
  107. projectSlug={project.slug}
  108. alertType={alertType}
  109. routes={routes}
  110. location={location}
  111. canChangeProject
  112. />
  113. <Layout.Title>
  114. {wizardAlertType
  115. ? `${t('Set Conditions for')} ${AlertWizardAlertNames[wizardAlertType]}`
  116. : title}
  117. </Layout.Title>
  118. </StyledHeaderContent>
  119. </Layout.Header>
  120. <Body>
  121. <Teams provideUserTeams>
  122. {({teams, initiallyLoaded}) =>
  123. initiallyLoaded ? (
  124. <Fragment>
  125. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  126. <IssueRuleEditor
  127. {...this.props}
  128. project={project}
  129. userTeamIds={teams.map(({id}) => id)}
  130. />
  131. )}
  132. {hasMetricAlerts &&
  133. alertType === AlertRuleType.METRIC &&
  134. (this.isDuplicateRule ? (
  135. <MetricRulesDuplicate
  136. {...this.props}
  137. eventView={eventView}
  138. wizardTemplate={wizardTemplate}
  139. sessionId={this.sessionId}
  140. project={project}
  141. userTeamIds={teams.map(({id}) => id)}
  142. />
  143. ) : (
  144. <MetricRulesCreate
  145. {...this.props}
  146. eventView={eventView}
  147. wizardTemplate={wizardTemplate}
  148. sessionId={this.sessionId}
  149. project={project}
  150. userTeamIds={teams.map(({id}) => id)}
  151. />
  152. ))}
  153. </Fragment>
  154. ) : (
  155. <LoadingIndicator />
  156. )
  157. }
  158. </Teams>
  159. </Body>
  160. </Fragment>
  161. );
  162. }
  163. }
  164. const StyledHeaderContent = styled(Layout.HeaderContent)`
  165. overflow: visible;
  166. `;
  167. const Body = styled(Layout.Body)`
  168. && {
  169. padding: 0;
  170. gap: 0;
  171. }
  172. grid-template-rows: 1fr;
  173. @media (min-width: ${p => p.theme.breakpoints.large}) {
  174. grid-template-columns: minmax(100px, auto) 400px;
  175. }
  176. `;
  177. export default Create;