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