create.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {Organization, Project, Team} from 'app/types';
  4. import {metric} from 'app/utils/analytics';
  5. import EventView from 'app/utils/discover/eventView';
  6. import withTeams from 'app/utils/withTeams';
  7. import {WizardRuleTemplate} from 'app/views/alerts/wizard/options';
  8. import {
  9. createDefaultRule,
  10. createRuleFromEventView,
  11. createRuleFromWizardTemplate,
  12. } from 'app/views/settings/incidentRules/constants';
  13. import RuleForm from './ruleForm';
  14. type RouteParams = {
  15. orgId: string;
  16. projectId: string;
  17. ruleId?: string;
  18. };
  19. type Props = {
  20. organization: Organization;
  21. project: Project;
  22. eventView: EventView | undefined;
  23. wizardTemplate?: WizardRuleTemplate;
  24. sessionId?: string;
  25. teams: Team[];
  26. } & RouteComponentProps<RouteParams, {}>;
  27. /**
  28. * Show metric rules form with an empty rule. Redirects to alerts list after creation.
  29. */
  30. class IncidentRulesCreate extends React.Component<Props> {
  31. handleSubmitSuccess = () => {
  32. const {router} = this.props;
  33. const {orgId} = this.props.params;
  34. metric.endTransaction({name: 'saveAlertRule'});
  35. router.push(`/organizations/${orgId}/alerts/rules/`);
  36. };
  37. render() {
  38. const {project, eventView, wizardTemplate, sessionId, teams, ...props} = this.props;
  39. const defaultRule = eventView
  40. ? createRuleFromEventView(eventView)
  41. : wizardTemplate
  42. ? createRuleFromWizardTemplate(wizardTemplate)
  43. : createDefaultRule();
  44. const userTeamIdArr = teams.filter(({isMember}) => isMember).map(({id}) => id);
  45. const userTeamIds = new Set(userTeamIdArr);
  46. if (props.organization.features.includes('team-alerts-ownership')) {
  47. const projectTeamIds = new Set(project.teams.map(({id}) => id));
  48. const defaultOwnerId = userTeamIdArr.find(id => projectTeamIds.has(id)) ?? null;
  49. defaultRule.owner = defaultOwnerId && `team:${defaultOwnerId}`;
  50. }
  51. return (
  52. <RuleForm
  53. onSubmitSuccess={this.handleSubmitSuccess}
  54. rule={{...defaultRule, projects: [project.slug]}}
  55. sessionId={sessionId}
  56. project={project}
  57. userTeamIds={userTeamIds}
  58. {...props}
  59. />
  60. );
  61. }
  62. }
  63. export default withTeams(IncidentRulesCreate);