create.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {Fragment, useEffect, useRef} from 'react';
  2. import * as Layout from 'sentry/components/layouts/thirds';
  3. import LoadingIndicator from 'sentry/components/loadingIndicator';
  4. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  5. import {t} from 'sentry/locale';
  6. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  7. import type {Member, Organization} from 'sentry/types/organization';
  8. import type {Project} from 'sentry/types/project';
  9. import EventView from 'sentry/utils/discover/eventView';
  10. import {uniqueId} from 'sentry/utils/guid';
  11. import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  12. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  13. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  14. import {useUserTeams} from 'sentry/utils/useUserTeams';
  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 MetricRuleDuplicate from 'sentry/views/alerts/rules/metric/duplicate';
  19. import {UptimeAlertForm} from 'sentry/views/alerts/rules/uptime/uptimeAlertForm';
  20. import {AlertRuleType} from 'sentry/views/alerts/types';
  21. import type {
  22. AlertType as WizardAlertType,
  23. WizardRuleTemplate,
  24. } from 'sentry/views/alerts/wizard/options';
  25. import {
  26. AlertWizardAlertNames,
  27. DEFAULT_WIZARD_TEMPLATE,
  28. } from 'sentry/views/alerts/wizard/options';
  29. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  30. type RouteParams = {
  31. alertType?: AlertRuleType;
  32. projectId?: string;
  33. };
  34. type Props = RouteComponentProps<RouteParams, {}> & {
  35. hasMetricAlerts: boolean;
  36. members: Member[] | undefined;
  37. organization: Organization;
  38. project: Project;
  39. };
  40. function Create(props: Props) {
  41. const {hasMetricAlerts, organization, project, location, members, params, router} =
  42. props;
  43. const {
  44. aggregate,
  45. dataset,
  46. eventTypes,
  47. createFromDuplicate,
  48. duplicateRuleId,
  49. createFromDiscover,
  50. query,
  51. createFromWizard,
  52. } = location?.query ?? {};
  53. const alertType = params.alertType || AlertRuleType.METRIC;
  54. const sessionId = useRef(uniqueId());
  55. const isDuplicateRule = createFromDuplicate === 'true' && duplicateRuleId;
  56. useEffect(() => {
  57. // TODO(taylangocmen): Remove redirect with aggregate && dataset && eventTypes, init from template
  58. if (
  59. alertType === AlertRuleType.METRIC &&
  60. !(aggregate && dataset && eventTypes) &&
  61. !createFromDuplicate
  62. ) {
  63. router.replace(
  64. normalizeUrl({
  65. ...location,
  66. pathname: `/organizations/${organization.slug}/alerts/new/${alertType}`,
  67. query: {
  68. ...location.query,
  69. ...DEFAULT_WIZARD_TEMPLATE,
  70. project: project.slug,
  71. },
  72. })
  73. );
  74. }
  75. }, [
  76. alertType,
  77. aggregate,
  78. dataset,
  79. eventTypes,
  80. createFromDuplicate,
  81. router,
  82. location,
  83. organization.slug,
  84. project.slug,
  85. ]);
  86. const {teams, isLoading} = useUserTeams();
  87. useRouteAnalyticsParams({
  88. project_id: project.id,
  89. session_id: sessionId.current,
  90. alert_type: alertType,
  91. duplicate_rule: isDuplicateRule ? 'true' : 'false',
  92. wizard_v3: 'true',
  93. });
  94. useRouteAnalyticsEventNames('new_alert_rule.viewed', 'New Alert Rule: Viewed');
  95. const wizardTemplate: WizardRuleTemplate = {
  96. aggregate: aggregate ?? DEFAULT_WIZARD_TEMPLATE.aggregate,
  97. dataset: dataset ?? DEFAULT_WIZARD_TEMPLATE.dataset,
  98. eventTypes: eventTypes ?? DEFAULT_WIZARD_TEMPLATE.eventTypes,
  99. query: query ?? DEFAULT_WIZARD_TEMPLATE.query,
  100. };
  101. const eventView = createFromDiscover ? EventView.fromLocation(location) : undefined;
  102. let wizardAlertType: undefined | WizardAlertType;
  103. if (createFromWizard && alertType === AlertRuleType.METRIC) {
  104. wizardAlertType = wizardTemplate
  105. ? getAlertTypeFromAggregateDataset(wizardTemplate)
  106. : 'issues';
  107. }
  108. const title = t('New Alert Rule');
  109. return (
  110. <Fragment>
  111. <SentryDocumentTitle title={title} projectSlug={project.slug} />
  112. <Layout.Header>
  113. <Layout.HeaderContent>
  114. <BuilderBreadCrumbs
  115. organization={organization}
  116. alertName={t('Set Conditions')}
  117. title={wizardAlertType ? t('Select Alert') : title}
  118. projectSlug={project.slug}
  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. <Layout.Body>
  128. {isLoading ? (
  129. <LoadingIndicator />
  130. ) : (
  131. <Fragment>
  132. {alertType === AlertRuleType.UPTIME ? (
  133. <UptimeAlertForm {...props} />
  134. ) : !hasMetricAlerts || alertType === AlertRuleType.ISSUE ? (
  135. <IssueRuleEditor
  136. {...props}
  137. userTeamIds={teams.map(({id}) => id)}
  138. members={members}
  139. />
  140. ) : (
  141. hasMetricAlerts &&
  142. alertType === AlertRuleType.METRIC &&
  143. (isDuplicateRule ? (
  144. <MetricRuleDuplicate
  145. {...props}
  146. eventView={eventView}
  147. wizardTemplate={wizardTemplate}
  148. sessionId={sessionId.current}
  149. userTeamIds={teams.map(({id}) => id)}
  150. />
  151. ) : (
  152. <MetricRulesCreate
  153. {...props}
  154. eventView={eventView}
  155. wizardTemplate={wizardTemplate}
  156. sessionId={sessionId.current}
  157. userTeamIds={teams.map(({id}) => id)}
  158. />
  159. ))
  160. )}
  161. </Fragment>
  162. )}
  163. </Layout.Body>
  164. </Fragment>
  165. );
  166. }
  167. export default Create;