create.tsx 2.3 KB

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