edit.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, organization, project, members} = 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. />
  69. <Layout.Title>{this.getTitle()}</Layout.Title>
  70. </Layout.HeaderContent>
  71. </Layout.Header>
  72. <EditConditionsBody>
  73. <Teams provideUserTeams>
  74. {({teams, initiallyLoaded}) =>
  75. initiallyLoaded ? (
  76. <Fragment>
  77. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  78. <IssueEditor
  79. {...this.props}
  80. project={project}
  81. onChangeTitle={this.handleChangeTitle}
  82. userTeamIds={teams.map(({id}) => id)}
  83. members={members}
  84. />
  85. )}
  86. {hasMetricAlerts && alertType === AlertRuleType.METRIC && (
  87. <MetricRulesEdit
  88. {...this.props}
  89. project={project}
  90. onChangeTitle={this.handleChangeTitle}
  91. userTeamIds={teams.map(({id}) => id)}
  92. />
  93. )}
  94. </Fragment>
  95. ) : (
  96. <LoadingIndicator />
  97. )
  98. }
  99. </Teams>
  100. </EditConditionsBody>
  101. </Fragment>
  102. );
  103. }
  104. }
  105. const EditConditionsBody = styled(Layout.Body)`
  106. *:not(img) {
  107. max-width: 1000px;
  108. }
  109. `;
  110. export default ProjectAlertsEditor;