create.tsx 2.3 KB

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