create.tsx 2.7 KB

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