create.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {RouteComponentProps} from 'react-router';
  2. import {Organization, Project} from 'sentry/types';
  3. import {metric} from 'sentry/utils/analytics';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {
  6. createDefaultRule,
  7. createRuleFromEventView,
  8. createRuleFromWizardTemplate,
  9. } from 'sentry/views/alerts/rules/metric/constants';
  10. import {WizardRuleTemplate} from 'sentry/views/alerts/wizard/options';
  11. import RuleForm from './ruleForm';
  12. type RouteParams = {
  13. orgId: string;
  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 {router, project} = props;
  31. const {orgId} = props.params;
  32. const alertRuleId: string | undefined = data
  33. ? (data.id as string | undefined)
  34. : undefined;
  35. metric.endTransaction({name: 'saveAlertRule'});
  36. router.push(
  37. alertRuleId
  38. ? {pathname: `/organizations/${orgId}/alerts/rules/details/${alertRuleId}/`}
  39. : {
  40. pathname: `/organizations/${orgId}/alerts/rules/`,
  41. query: {project: project.id},
  42. }
  43. );
  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;