duplicate.tsx 3.3 KB

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