create.tsx 6.7 KB

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