edit.tsx 3.7 KB

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