edit.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 {trackAnalytics} from 'sentry/utils/analytics';
  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. projectId: string;
  17. ruleId: string;
  18. };
  19. type Props = RouteComponentProps<RouteParams, {}> & {
  20. hasMetricAlerts: boolean;
  21. members: Member[] | undefined;
  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. trackAnalytics('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, members} =
  54. this.props;
  55. const alertType = this.getAlertType();
  56. return (
  57. <Fragment>
  58. <SentryDocumentTitle
  59. title={this.getTitle()}
  60. orgSlug={organization.slug}
  61. projectSlug={project.slug}
  62. />
  63. <Layout.Header>
  64. <Layout.HeaderContent>
  65. <BuilderBreadCrumbs
  66. organization={organization}
  67. title={t('Edit Alert Rule')}
  68. projectSlug={project.slug}
  69. routes={routes}
  70. location={location}
  71. />
  72. <Layout.Title>{this.getTitle()}</Layout.Title>
  73. </Layout.HeaderContent>
  74. </Layout.Header>
  75. <EditConditionsBody>
  76. <Teams provideUserTeams>
  77. {({teams, initiallyLoaded}) =>
  78. initiallyLoaded ? (
  79. <Fragment>
  80. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  81. <IssueEditor
  82. {...this.props}
  83. project={project}
  84. onChangeTitle={this.handleChangeTitle}
  85. userTeamIds={teams.map(({id}) => id)}
  86. members={members}
  87. />
  88. )}
  89. {hasMetricAlerts && alertType === AlertRuleType.METRIC && (
  90. <MetricRulesEdit
  91. {...this.props}
  92. project={project}
  93. onChangeTitle={this.handleChangeTitle}
  94. userTeamIds={teams.map(({id}) => id)}
  95. />
  96. )}
  97. </Fragment>
  98. ) : (
  99. <LoadingIndicator />
  100. )
  101. }
  102. </Teams>
  103. </EditConditionsBody>
  104. </Fragment>
  105. );
  106. }
  107. }
  108. const EditConditionsBody = styled(Layout.Body)`
  109. *:not(img) {
  110. max-width: 1000px;
  111. }
  112. `;
  113. export default ProjectAlertsEditor;