duplicate.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import pick from 'lodash/pick';
  2. import * as Layout from 'sentry/components/layouts/thirds';
  3. import LoadingError from 'sentry/components/loadingError';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  6. import type {Project} from 'sentry/types/project';
  7. import type EventView from 'sentry/utils/discover/eventView';
  8. import {uniqueId} from 'sentry/utils/guid';
  9. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import {makeAlertsPathname} from 'sentry/views/alerts/pathnames';
  12. import {
  13. DuplicateActionFields,
  14. DuplicateMetricFields,
  15. DuplicateTriggerFields,
  16. } from 'sentry/views/alerts/rules/metric/constants';
  17. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  18. import {useMetricRule} from 'sentry/views/alerts/rules/metric/utils/useMetricRule';
  19. import type {WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  20. import RuleForm from './ruleForm';
  21. interface MetricRuleDuplicateProps extends RouteComponentProps {
  22. project: Project;
  23. userTeamIds: string[];
  24. eventView?: EventView;
  25. sessionId?: string;
  26. wizardTemplate?: WizardRuleTemplate;
  27. }
  28. /**
  29. * Show metric rules form with values from an existing rule.
  30. */
  31. function MetricRuleDuplicate({
  32. project,
  33. sessionId,
  34. userTeamIds,
  35. ...otherProps
  36. }: MetricRuleDuplicateProps) {
  37. const organization = useOrganization();
  38. const duplicateRuleId: string = otherProps.location.query.duplicateRuleId;
  39. const {
  40. data: duplicateTargetRule,
  41. isPending,
  42. isError,
  43. refetch,
  44. } = useMetricRule({
  45. orgSlug: organization.slug,
  46. ruleId: duplicateRuleId,
  47. });
  48. const handleSubmitSuccess = (data: any) => {
  49. const alertRuleId: string | undefined = data
  50. ? (data.id as string | undefined)
  51. : undefined;
  52. const target = alertRuleId
  53. ? {
  54. pathname: makeAlertsPathname({
  55. path: `/rules/details/${alertRuleId}/`,
  56. organization,
  57. }),
  58. }
  59. : {
  60. pathname: makeAlertsPathname({
  61. path: `/rules/`,
  62. organization,
  63. }),
  64. query: {project: project.id},
  65. };
  66. otherProps.router.push(normalizeUrl(target));
  67. };
  68. if (isPending) {
  69. return <LoadingIndicator />;
  70. }
  71. if (isError) {
  72. return <LoadingError onRetry={refetch} />;
  73. }
  74. return (
  75. <Layout.Main>
  76. <RuleForm
  77. organization={organization}
  78. onSubmitSuccess={handleSubmitSuccess}
  79. rule={
  80. {
  81. ...pick(duplicateTargetRule, DuplicateMetricFields),
  82. triggers: duplicateTargetRule.triggers.map(trigger => ({
  83. ...pick(trigger, DuplicateTriggerFields),
  84. actions: trigger.actions.map(action => ({
  85. inputChannelId: null,
  86. integrationId: action.integrationId ?? undefined,
  87. options: null,
  88. sentryAppId: undefined,
  89. unsavedId: uniqueId(),
  90. unsavedDateCreated: new Date().toISOString(),
  91. ...pick(action, DuplicateActionFields),
  92. })),
  93. })),
  94. name: duplicateTargetRule.name + ' copy',
  95. } as MetricRule
  96. }
  97. sessionId={sessionId}
  98. project={project}
  99. userTeamIds={userTeamIds}
  100. isDuplicateRule
  101. {...otherProps}
  102. />
  103. </Layout.Main>
  104. );
  105. }
  106. export default MetricRuleDuplicate;