metricsExtractionRuleEditModal.tsx 3.7 KB

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