utils.tsx 3.3 KB

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