duplicate.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {RouteComponentProps} from 'react-router';
  2. import pick from 'lodash/pick';
  3. import * as Layout from 'sentry/components/layouts/thirds';
  4. import {Organization, Project} from 'sentry/types';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {uniqueId} from 'sentry/utils/guid';
  7. import {
  8. DuplicateActionFields,
  9. DuplicateMetricFields,
  10. DuplicateTriggerFields,
  11. } from 'sentry/views/alerts/rules/metric/constants';
  12. import {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  13. import {WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  14. import AsyncView from 'sentry/views/asyncView';
  15. import RuleForm from './ruleForm';
  16. type RouteParams = {
  17. orgId: string;
  18. };
  19. type Props = {
  20. organization: Organization;
  21. project: Project;
  22. userTeamIds: string[];
  23. eventView?: EventView;
  24. sessionId?: string;
  25. wizardTemplate?: WizardRuleTemplate;
  26. } & RouteComponentProps<RouteParams, {}>;
  27. type State = {
  28. duplicateTargetRule?: MetricRule;
  29. } & AsyncView['state'];
  30. /**
  31. * Show metric rules form with values from an existing rule. Redirects to alerts list after creation.
  32. */
  33. class MetricRulesDuplicate extends AsyncView<Props, State> {
  34. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  35. const {
  36. params: {orgId},
  37. location: {query},
  38. } = this.props;
  39. return [
  40. [
  41. 'duplicateTargetRule',
  42. `/organizations/${orgId}/alert-rules/${query.duplicateRuleId}/`,
  43. ],
  44. ];
  45. }
  46. handleSubmitSuccess = (data: any) => {
  47. const {
  48. router,
  49. project,
  50. params: {orgId},
  51. } = this.props;
  52. const alertRuleId: string | undefined = data
  53. ? (data.id as string | undefined)
  54. : undefined;
  55. router.push(
  56. alertRuleId
  57. ? {pathname: `/organizations/${orgId}/alerts/rules/details/${alertRuleId}/`}
  58. : {
  59. pathname: `/organizations/${orgId}/alerts/rules/`,
  60. query: {project: project.id},
  61. }
  62. );
  63. };
  64. renderBody() {
  65. const {project, sessionId, userTeamIds, ...otherProps} = this.props;
  66. const {duplicateTargetRule} = this.state;
  67. if (!duplicateTargetRule) {
  68. return this.renderLoading();
  69. }
  70. return (
  71. <Layout.Main>
  72. <RuleForm
  73. onSubmitSuccess={this.handleSubmitSuccess}
  74. rule={
  75. {
  76. ...pick(duplicateTargetRule, DuplicateMetricFields),
  77. triggers: duplicateTargetRule.triggers.map(trigger => ({
  78. ...pick(trigger, DuplicateTriggerFields),
  79. actions: trigger.actions.map(action => ({
  80. inputChannelId: null,
  81. integrationId: undefined,
  82. options: null,
  83. sentryAppId: undefined,
  84. unsavedId: uniqueId(),
  85. unsavedDateCreated: new Date().toISOString(),
  86. ...pick(action, DuplicateActionFields),
  87. })),
  88. })),
  89. name: duplicateTargetRule.name + ' copy',
  90. } as MetricRule
  91. }
  92. sessionId={sessionId}
  93. project={project}
  94. userTeamIds={userTeamIds}
  95. isDuplicateRule
  96. {...otherProps}
  97. />
  98. </Layout.Main>
  99. );
  100. }
  101. }
  102. export default MetricRulesDuplicate;