duplicate.tsx 3.2 KB

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