edit.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import * as Layout from 'sentry/components/layouts/thirds';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  6. import {t} from 'sentry/locale';
  7. import {Member, Organization, Project} from 'sentry/types';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import Teams from 'sentry/utils/teams';
  10. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  11. import IssueEditor from 'sentry/views/alerts/rules/issue';
  12. import MetricRulesEdit from 'sentry/views/alerts/rules/metric/edit';
  13. import {AlertRuleType} from 'sentry/views/alerts/types';
  14. type RouteParams = {
  15. projectId: string;
  16. ruleId: string;
  17. };
  18. type Props = RouteComponentProps<RouteParams, {}> & {
  19. hasMetricAlerts: boolean;
  20. members: Member[] | undefined;
  21. organization: Organization;
  22. project: Project;
  23. };
  24. type State = {
  25. ruleName: string;
  26. };
  27. class ProjectAlertsEditor extends Component<Props, State> {
  28. state: State = {
  29. ruleName: '',
  30. };
  31. componentDidMount() {
  32. const {organization, project} = this.props;
  33. trackAnalytics('edit_alert_rule.viewed', {
  34. organization,
  35. project_id: project.id,
  36. alert_type: this.getAlertType(),
  37. });
  38. }
  39. handleChangeTitle = (ruleName: string) => {
  40. this.setState({ruleName});
  41. };
  42. getTitle() {
  43. const {ruleName} = this.state;
  44. return `${ruleName}`;
  45. }
  46. getAlertType(): AlertRuleType {
  47. return location.pathname.includes('/alerts/metric-rules/')
  48. ? AlertRuleType.METRIC
  49. : AlertRuleType.ISSUE;
  50. }
  51. render() {
  52. const {hasMetricAlerts, organization, project, members} = this.props;
  53. const alertType = this.getAlertType();
  54. return (
  55. <Fragment>
  56. <SentryDocumentTitle
  57. title={this.getTitle()}
  58. orgSlug={organization.slug}
  59. projectSlug={project.slug}
  60. />
  61. <Layout.Header>
  62. <Layout.HeaderContent>
  63. <BuilderBreadCrumbs
  64. organization={organization}
  65. title={t('Edit Alert Rule')}
  66. projectSlug={project.slug}
  67. />
  68. <Layout.Title>{this.getTitle()}</Layout.Title>
  69. </Layout.HeaderContent>
  70. </Layout.Header>
  71. <Layout.Body>
  72. <Teams provideUserTeams>
  73. {({teams, initiallyLoaded}) =>
  74. initiallyLoaded ? (
  75. <Fragment>
  76. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  77. <IssueEditor
  78. {...this.props}
  79. project={project}
  80. onChangeTitle={this.handleChangeTitle}
  81. userTeamIds={teams.map(({id}) => id)}
  82. members={members}
  83. />
  84. )}
  85. {hasMetricAlerts && alertType === AlertRuleType.METRIC && (
  86. <MetricRulesEdit
  87. {...this.props}
  88. project={project}
  89. onChangeTitle={this.handleChangeTitle}
  90. userTeamIds={teams.map(({id}) => id)}
  91. />
  92. )}
  93. </Fragment>
  94. ) : (
  95. <LoadingIndicator />
  96. )
  97. }
  98. </Teams>
  99. </Layout.Body>
  100. </Fragment>
  101. );
  102. }
  103. }
  104. export default ProjectAlertsEditor;