create.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  2. import type {Organization} from 'sentry/types/organization';
  3. import type {Project} from 'sentry/types/project';
  4. import {metric} from 'sentry/utils/analytics';
  5. import type EventView from 'sentry/utils/discover/eventView';
  6. import {decodeScalar} from 'sentry/utils/queryString';
  7. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  8. import {
  9. createDefaultRule,
  10. createRuleFromEventView,
  11. createRuleFromWizardTemplate,
  12. } from 'sentry/views/alerts/rules/metric/constants';
  13. import type {WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  14. import RuleForm from './ruleForm';
  15. type RouteParams = {
  16. projectId?: string;
  17. ruleId?: string;
  18. };
  19. type Props = {
  20. eventView: EventView | undefined;
  21. organization: Organization;
  22. project: Project;
  23. userTeamIds: string[];
  24. sessionId?: string;
  25. wizardTemplate?: WizardRuleTemplate;
  26. } & RouteComponentProps<RouteParams, {}>;
  27. /**
  28. * Show metric rules form with an empty rule. Redirects to alerts list after creation.
  29. */
  30. function MetricRulesCreate(props: Props) {
  31. function handleSubmitSuccess(data: any) {
  32. const {organization, project, router} = props;
  33. const alertRuleId: string | undefined = data
  34. ? (data.id as string | undefined)
  35. : undefined;
  36. metric.endSpan({name: 'saveAlertRule'});
  37. const target = alertRuleId
  38. ? {
  39. pathname: `/organizations/${organization.slug}/alerts/rules/details/${alertRuleId}/`,
  40. }
  41. : {
  42. pathname: `/organizations/${organization.slug}/alerts/rules/`,
  43. query: {project: project.id},
  44. };
  45. router.push(normalizeUrl(target));
  46. }
  47. const {
  48. project,
  49. eventView,
  50. wizardTemplate,
  51. sessionId,
  52. userTeamIds,
  53. location,
  54. ...otherProps
  55. } = props;
  56. const defaultRule = eventView
  57. ? createRuleFromEventView(eventView)
  58. : wizardTemplate
  59. ? createRuleFromWizardTemplate(wizardTemplate)
  60. : createDefaultRule();
  61. const projectTeamIds = new Set(project.teams.map(({id}) => id));
  62. const defaultOwnerId = userTeamIds.find(id => projectTeamIds.has(id)) ?? null;
  63. defaultRule.owner = defaultOwnerId && `team:${defaultOwnerId}`;
  64. const environment = decodeScalar(location?.query?.environment) ?? null;
  65. return (
  66. <RuleForm
  67. onSubmitSuccess={handleSubmitSuccess}
  68. rule={{...defaultRule, environment, projects: [project.slug]}}
  69. sessionId={sessionId}
  70. project={project}
  71. userTeamIds={userTeamIds}
  72. eventView={eventView}
  73. location={location}
  74. {...otherProps}
  75. />
  76. );
  77. }
  78. export default MetricRulesCreate;