utils.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import IdBadge from 'sentry/components/idBadge';
  2. import {t} from 'sentry/locale';
  3. import {Organization, Project} from 'sentry/types';
  4. import {
  5. IssueAlertActionType,
  6. IssueAlertRule,
  7. RuleActionsCategories,
  8. } from 'sentry/types/alerts';
  9. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  10. import {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  11. export function getProjectOptions({
  12. organization,
  13. projects,
  14. isFormDisabled,
  15. }: {
  16. isFormDisabled: boolean;
  17. organization: Organization;
  18. projects: Project[];
  19. }) {
  20. const hasOrgAlertWrite = organization.access.includes('alerts:write');
  21. const hasOrgWrite = organization.access.includes('org:write');
  22. const hasOpenMembership = organization.features.includes('open-membership');
  23. // If form is enabled, we want to limit to the subset of projects which the
  24. // user can create/edit alerts.
  25. const projectWithWrite =
  26. isFormDisabled || hasOrgAlertWrite
  27. ? projects
  28. : projects.filter(project => project.access.includes('alerts:write'));
  29. const myProjects = projectWithWrite.filter(project => project.isMember);
  30. const allProjects = projectWithWrite.filter(project => !project.isMember);
  31. const myProjectOptions = myProjects.map(myProject => ({
  32. value: myProject.id,
  33. label: myProject.slug,
  34. leadingItems: renderIdBadge(myProject),
  35. }));
  36. const openMembershipProjects = [
  37. {
  38. label: t('My Projects'),
  39. options: myProjectOptions,
  40. },
  41. {
  42. label: t('All Projects'),
  43. options: allProjects.map(allProject => ({
  44. value: allProject.id,
  45. label: allProject.slug,
  46. leadingItems: renderIdBadge(allProject),
  47. })),
  48. },
  49. ];
  50. return hasOpenMembership || hasOrgWrite || isActiveSuperuser()
  51. ? openMembershipProjects
  52. : myProjectOptions;
  53. }
  54. function renderIdBadge(project: Project) {
  55. return (
  56. <IdBadge
  57. project={project}
  58. avatarProps={{consistentWidth: true}}
  59. avatarSize={18}
  60. disableLink
  61. hideName
  62. />
  63. );
  64. }
  65. export function getRuleActionCategory(rule: IssueAlertRule) {
  66. const numDefaultActions = rule.actions.filter(
  67. action => action.id === IssueAlertActionType.NOTIFY_EMAIL
  68. ).length;
  69. switch (numDefaultActions) {
  70. // Are all actions default actions?
  71. case rule.actions.length:
  72. return RuleActionsCategories.ALL_DEFAULT;
  73. // Are none of the actions default actions?
  74. case 0:
  75. return RuleActionsCategories.NO_DEFAULT;
  76. default:
  77. return RuleActionsCategories.SOME_DEFAULT;
  78. }
  79. }
  80. export function getAlertRuleActionCategory(rule: MetricRule) {
  81. const actions = rule.triggers.map(trigger => trigger.actions).flat();
  82. const numDefaultActions = actions.filter(action => action.type === 'email').length;
  83. switch (numDefaultActions) {
  84. // Are all actions default actions?
  85. case actions.length:
  86. return RuleActionsCategories.ALL_DEFAULT;
  87. // Are none of the actions default actions?
  88. case 0:
  89. return RuleActionsCategories.NO_DEFAULT;
  90. default:
  91. return RuleActionsCategories.SOME_DEFAULT;
  92. }
  93. }