edit.tsx 3.6 KB

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