metricsExtractionRuleEditModal.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  6. import {t} from 'sentry/locale';
  7. import type {Project} from 'sentry/types/project';
  8. import {getReadableMetricType} from 'sentry/utils/metrics/formatters';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {
  11. type FormData,
  12. MetricsExtractionRuleForm,
  13. } from 'sentry/views/settings/projectMetrics/metricsExtractionRuleForm';
  14. import {
  15. type MetricsExtractionRule,
  16. useUpdateMetricsExtractionRules,
  17. } from 'sentry/views/settings/projectMetrics/utils/api';
  18. interface Props extends ModalRenderProps {
  19. metricExtractionRule: MetricsExtractionRule;
  20. project: Project;
  21. }
  22. export function MetricsExtractionRuleEditModal({
  23. Header,
  24. Body,
  25. closeModal,
  26. CloseButton,
  27. metricExtractionRule,
  28. project,
  29. }: Props) {
  30. const organization = useOrganization();
  31. const updateExtractionRuleMutation = useUpdateMetricsExtractionRules(
  32. organization.slug,
  33. project.slug
  34. );
  35. const initialData: FormData = useMemo(() => {
  36. return {
  37. spanAttribute: metricExtractionRule.spanAttribute,
  38. type: metricExtractionRule.type,
  39. tags: metricExtractionRule.tags,
  40. conditions: metricExtractionRule.conditions.length
  41. ? metricExtractionRule.conditions
  42. : [''],
  43. };
  44. }, [metricExtractionRule]);
  45. const handleSubmit = useCallback(
  46. (
  47. data: FormData,
  48. onSubmitSuccess: (data: FormData) => void,
  49. onSubmitError: (error: any) => void
  50. ) => {
  51. const extractionRule: MetricsExtractionRule = {
  52. spanAttribute: data.spanAttribute!,
  53. tags: data.tags,
  54. type: data.type!,
  55. unit: 'none',
  56. conditions: data.conditions.filter(Boolean),
  57. };
  58. updateExtractionRuleMutation.mutate(
  59. {
  60. metricsExtractionRules: [extractionRule],
  61. },
  62. {
  63. onSuccess: () => {
  64. onSubmitSuccess(data);
  65. addSuccessMessage(t('Metric extraction rule updated'));
  66. closeModal();
  67. },
  68. onError: error => {
  69. const message = error?.responseJSON?.detail
  70. ? (error.responseJSON.detail as string)
  71. : t('Unable to save your changes.');
  72. onSubmitError(message);
  73. addErrorMessage(message);
  74. },
  75. }
  76. );
  77. onSubmitSuccess(data);
  78. },
  79. [closeModal, updateExtractionRuleMutation]
  80. );
  81. return (
  82. <Fragment>
  83. <Header>
  84. <h4>
  85. <Capitalize>{getReadableMetricType(metricExtractionRule.type)}</Capitalize>
  86. {' — '}
  87. {metricExtractionRule.spanAttribute}
  88. </h4>
  89. </Header>
  90. <CloseButton />
  91. <Body>
  92. <MetricsExtractionRuleForm
  93. initialData={initialData}
  94. project={project}
  95. submitLabel={t('Update')}
  96. cancelLabel={t('Cancel')}
  97. onCancel={closeModal}
  98. onSubmit={handleSubmit}
  99. isEdit
  100. requireChanges
  101. />
  102. </Body>
  103. </Fragment>
  104. );
  105. }
  106. export const modalCss = css`
  107. width: 100%;
  108. max-width: 1000px;
  109. `;
  110. const Capitalize = styled('span')`
  111. text-transform: capitalize;
  112. `;