edit.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {Fragment, useState} 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 useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  10. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  11. import {useLocation} from 'sentry/utils/useLocation';
  12. import {useUserTeams} from 'sentry/utils/useUserTeams';
  13. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  14. import IssueEditor from './rules/issue';
  15. import {MetricRulesEdit} from './rules/metric/edit';
  16. import {UptimeRulesEdit} from './rules/uptime/edit';
  17. import {CombinedAlertType} from './types';
  18. type RouteParams = {
  19. projectId: string;
  20. ruleId: string;
  21. };
  22. type Props = RouteComponentProps<RouteParams, {}> & {
  23. hasMetricAlerts: boolean;
  24. hasUptimeAlerts: boolean;
  25. members: Member[] | undefined;
  26. organization: Organization;
  27. project: Project;
  28. };
  29. function ProjectAlertsEditor(props: Props) {
  30. const {members, organization, project} = props;
  31. const location = useLocation();
  32. const [title, setTitle] = useState('');
  33. const alertTypeUrls = [
  34. {url: '/alerts/metric-rules/', type: CombinedAlertType.METRIC},
  35. {url: '/alerts/uptime-rules/', type: CombinedAlertType.UPTIME},
  36. {url: '/alerts/rules/', type: CombinedAlertType.ISSUE},
  37. ] as const;
  38. const alertType =
  39. alertTypeUrls.find(({url}) => location.pathname.includes(url))?.type ??
  40. CombinedAlertType.ISSUE;
  41. useRouteAnalyticsEventNames('edit_alert_rule.viewed', 'Edit Alert Rule: Viewed');
  42. useRouteAnalyticsParams({
  43. organization,
  44. project_id: project.id,
  45. alert_type: alertType,
  46. });
  47. // Used to hide specific fields like actions while migrating metric alert rules.
  48. // Currently used to help people add `is:unresolved` to their metric alert query.
  49. const isMigration = location?.query?.migration === '1';
  50. const {teams, isLoading: teamsLoading} = useUserTeams();
  51. return (
  52. <Fragment>
  53. <SentryDocumentTitle
  54. title={title}
  55. orgSlug={organization.slug}
  56. projectSlug={project.slug}
  57. />
  58. <Layout.Header>
  59. <Layout.HeaderContent>
  60. <BuilderBreadCrumbs
  61. organization={organization}
  62. title={isMigration ? t('Review Thresholds') : t('Edit Alert Rule')}
  63. projectSlug={project.slug}
  64. />
  65. <Layout.Title>{title}</Layout.Title>
  66. </Layout.HeaderContent>
  67. </Layout.Header>
  68. <Layout.Body>
  69. {!teamsLoading ? (
  70. <Fragment>
  71. {alertType === CombinedAlertType.ISSUE && (
  72. <IssueEditor
  73. {...props}
  74. project={project}
  75. onChangeTitle={setTitle}
  76. userTeamIds={teams.map(({id}) => id)}
  77. members={members}
  78. />
  79. )}
  80. {alertType === CombinedAlertType.METRIC && (
  81. <MetricRulesEdit
  82. {...props}
  83. project={project}
  84. onChangeTitle={setTitle}
  85. userTeamIds={teams.map(({id}) => id)}
  86. />
  87. )}
  88. {alertType === CombinedAlertType.UPTIME && (
  89. <UptimeRulesEdit
  90. {...props}
  91. project={project}
  92. onChangeTitle={setTitle}
  93. userTeamIds={teams.map(({id}) => id)}
  94. />
  95. )}
  96. </Fragment>
  97. ) : (
  98. <LoadingIndicator />
  99. )}
  100. </Layout.Body>
  101. </Fragment>
  102. );
  103. }
  104. export default ProjectAlertsEditor;