utils.tsx 3.2 KB

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