create.tsx 6.6 KB

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