edit.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {useCallback, useEffect} from 'react';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {Alert} from 'sentry/components/alert';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import {t} from 'sentry/locale';
  7. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  8. import type {Organization} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. import {metric} from 'sentry/utils/analytics';
  11. import {useApiQuery} from 'sentry/utils/queryClient';
  12. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  13. import {useNavigate} from 'sentry/utils/useNavigate';
  14. import RuleForm from 'sentry/views/alerts/rules/metric/ruleForm';
  15. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  16. type RouteParams = {
  17. projectId: string;
  18. ruleId: string;
  19. };
  20. type Props = {
  21. onChangeTitle: (data: string) => void;
  22. organization: Organization;
  23. project: Project;
  24. userTeamIds: string[];
  25. } & RouteComponentProps<RouteParams, {}>;
  26. export function MetricRulesEdit({
  27. organization,
  28. params,
  29. project,
  30. userTeamIds,
  31. onChangeTitle,
  32. ...props
  33. }: Props) {
  34. const navigate = useNavigate();
  35. const {
  36. isPending,
  37. isError,
  38. data: rule,
  39. error,
  40. } = useApiQuery<MetricRule>(
  41. [`/organizations/${organization.slug}/alert-rules/${params.ruleId}/`],
  42. {
  43. staleTime: 0,
  44. retry: false,
  45. }
  46. );
  47. useEffect(() => {
  48. if (!isPending && rule) {
  49. onChangeTitle(rule.name ?? '');
  50. }
  51. }, [onChangeTitle, isPending, rule]);
  52. useEffect(() => {
  53. if (isError && error?.responseText) {
  54. try {
  55. const {detail} = JSON.parse(error.responseText);
  56. if (detail) {
  57. addErrorMessage(detail);
  58. }
  59. } catch {
  60. // Ignore
  61. }
  62. }
  63. }, [isError, error]);
  64. const handleSubmitSuccess = useCallback(() => {
  65. metric.endSpan({name: 'saveAlertRule'});
  66. navigate(
  67. normalizeUrl({
  68. pathname: `/organizations/${organization.slug}/alerts/rules/details/${params.ruleId}/`,
  69. })
  70. );
  71. }, [params.ruleId, navigate, organization.slug]);
  72. if (isPending) {
  73. return <LoadingIndicator />;
  74. }
  75. if (isError) {
  76. if (error?.status === 404) {
  77. return (
  78. <Alert type="error" showIcon>
  79. {t('This alert rule could not be found.')}
  80. </Alert>
  81. );
  82. }
  83. return <LoadingError />;
  84. }
  85. return (
  86. <RuleForm
  87. {...props}
  88. params={params}
  89. project={project}
  90. userTeamIds={userTeamIds}
  91. organization={organization}
  92. ruleId={params.ruleId}
  93. rule={rule}
  94. onSubmitSuccess={handleSubmitSuccess}
  95. disableProjectSelector
  96. />
  97. );
  98. }