actionSet.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import ActionLink from 'sentry/components/actions/actionLink';
  4. import IgnoreActions from 'sentry/components/actions/ignore';
  5. import {openConfirmModal} from 'sentry/components/confirm';
  6. import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
  7. import {MenuItemProps} from 'sentry/components/dropdownMenuItem';
  8. import {IconEllipsis} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import GroupStore from 'sentry/stores/groupStore';
  11. import {
  12. BaseGroup,
  13. IssueCategory,
  14. IssueCategoryCapabilities,
  15. Project,
  16. ResolutionStatus,
  17. } from 'sentry/types';
  18. import {getIssueCapability} from 'sentry/utils/groupCapabilities';
  19. import Projects from 'sentry/utils/projects';
  20. import useMedia from 'sentry/utils/useMedia';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import ResolveActions from './resolveActions';
  23. import ReviewAction from './reviewAction';
  24. import {ConfirmAction, getConfirm, getLabel} from './utils';
  25. type Props = {
  26. allInQuerySelected: boolean;
  27. anySelected: boolean;
  28. issues: Set<string>;
  29. multiSelected: boolean;
  30. onDelete: () => void;
  31. onMerge: () => void;
  32. onShouldConfirm: (action: ConfirmAction) => boolean;
  33. onUpdate: (data?: any) => void;
  34. query: string;
  35. queryCount: number;
  36. selectedProjectSlug?: string;
  37. };
  38. function ActionSet({
  39. queryCount,
  40. query,
  41. allInQuerySelected,
  42. anySelected,
  43. multiSelected,
  44. issues,
  45. onUpdate,
  46. onShouldConfirm,
  47. onDelete,
  48. onMerge,
  49. selectedProjectSlug,
  50. }: Props) {
  51. const organization = useOrganization();
  52. const numIssues = issues.size;
  53. const confirm = getConfirm({
  54. numIssues,
  55. allInQuerySelected,
  56. query,
  57. queryCount,
  58. organization,
  59. });
  60. const label = getLabel(numIssues, allInQuerySelected);
  61. const selectedIssues = [...issues]
  62. .map(issueId => GroupStore.get(issueId))
  63. .filter(issue => issue) as BaseGroup[];
  64. // Merges require multiple issues of a single project type
  65. const multipleIssueProjectsSelected = multiSelected && !selectedProjectSlug;
  66. const {enabled: mergeSupported, disabledReason: mergeDisabledReason} =
  67. isActionSupported(selectedIssues, 'merge');
  68. const {enabled: deleteSupported, disabledReason: deleteDisabledReason} =
  69. isActionSupported(selectedIssues, 'delete');
  70. const mergeDisabled =
  71. !multiSelected || multipleIssueProjectsSelected || !mergeSupported;
  72. const ignoreDisabled = !anySelected;
  73. const canMarkReviewed =
  74. anySelected && (allInQuerySelected || selectedIssues.some(issue => !!issue?.inbox));
  75. // determine which ... dropdown options to show based on issue(s) selected
  76. const canAddBookmark =
  77. allInQuerySelected || selectedIssues.some(issue => !issue.isBookmarked);
  78. const canRemoveBookmark =
  79. allInQuerySelected || selectedIssues.some(issue => issue.isBookmarked);
  80. const canSetUnresolved =
  81. allInQuerySelected || selectedIssues.some(issue => issue.status === 'resolved');
  82. const makeMergeTooltip = () => {
  83. if (mergeDisabledReason) {
  84. return mergeDisabledReason;
  85. }
  86. if (multipleIssueProjectsSelected) {
  87. return t('Cannot merge issues from different projects');
  88. }
  89. return '';
  90. };
  91. // Determine whether to nest "Merge" and "Mark as Reviewed" buttons inside
  92. // the dropdown menu based on the current screen size
  93. const theme = useTheme();
  94. const nestMergeAndReview = useMedia(`(max-width: ${theme.breakpoints.xlarge})`);
  95. // If at least one Performance Issue is selected, some of the ignore dropdown options must be disabled.
  96. const issueCategory: IssueCategory = selectedIssues.some(
  97. issue => issue?.issueCategory === IssueCategory.PERFORMANCE
  98. )
  99. ? IssueCategory.PERFORMANCE
  100. : IssueCategory.ERROR;
  101. const menuItems: MenuItemProps[] = [
  102. {
  103. key: 'merge',
  104. label: t('Merge'),
  105. hidden: !nestMergeAndReview,
  106. disabled: mergeDisabled,
  107. details: makeMergeTooltip(),
  108. onAction: () => {
  109. openConfirmModal({
  110. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  111. onConfirm: onMerge,
  112. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  113. confirmText: label('merge'),
  114. });
  115. },
  116. },
  117. {
  118. key: 'mark-reviewed',
  119. label: t('Mark Reviewed'),
  120. hidden: !nestMergeAndReview,
  121. disabled: !canMarkReviewed,
  122. onAction: () => onUpdate({inbox: false}),
  123. },
  124. {
  125. key: 'bookmark',
  126. label: t('Add to Bookmarks'),
  127. hidden: !canAddBookmark,
  128. onAction: () => {
  129. openConfirmModal({
  130. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  131. onConfirm: () => onUpdate({isBookmarked: true}),
  132. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  133. confirmText: label('bookmark'),
  134. });
  135. },
  136. },
  137. {
  138. key: 'remove-bookmark',
  139. label: t('Remove from Bookmarks'),
  140. hidden: !canRemoveBookmark,
  141. onAction: () => {
  142. openConfirmModal({
  143. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  144. onConfirm: () => onUpdate({isBookmarked: false}),
  145. message: confirm({
  146. action: ConfirmAction.UNBOOKMARK,
  147. canBeUndone: false,
  148. append: ' from your bookmarks',
  149. }),
  150. confirmText: label('remove', ' from your bookmarks'),
  151. });
  152. },
  153. },
  154. {
  155. key: 'unresolve',
  156. label: t('Set status to: Unresolved'),
  157. hidden: !canSetUnresolved,
  158. onAction: () => {
  159. openConfirmModal({
  160. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  161. onConfirm: () => onUpdate({status: ResolutionStatus.UNRESOLVED}),
  162. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  163. confirmText: label('unresolve'),
  164. });
  165. },
  166. },
  167. {
  168. key: 'delete',
  169. label: t('Delete'),
  170. priority: 'danger',
  171. disabled: !deleteSupported,
  172. details: deleteDisabledReason,
  173. onAction: () => {
  174. openConfirmModal({
  175. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  176. onConfirm: onDelete,
  177. priority: 'danger',
  178. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  179. confirmText: label('delete'),
  180. });
  181. },
  182. },
  183. ];
  184. return (
  185. <Fragment>
  186. {selectedProjectSlug ? (
  187. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  188. {({projects, initiallyLoaded, fetchError}) => {
  189. const selectedProject = projects[0];
  190. return (
  191. <ResolveActions
  192. onShouldConfirm={onShouldConfirm}
  193. onUpdate={onUpdate}
  194. anySelected={anySelected}
  195. orgSlug={organization.slug}
  196. params={{
  197. hasReleases: selectedProject.hasOwnProperty('features')
  198. ? (selectedProject as Project).features.includes('releases')
  199. : false,
  200. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  201. ? (selectedProject as Project).latestRelease
  202. : undefined,
  203. projectId: selectedProject.slug,
  204. confirm,
  205. label,
  206. loadingProjects: !initiallyLoaded,
  207. projectFetchError: !!fetchError,
  208. }}
  209. />
  210. );
  211. }}
  212. </Projects>
  213. ) : (
  214. <ResolveActions
  215. onShouldConfirm={onShouldConfirm}
  216. onUpdate={onUpdate}
  217. anySelected={anySelected}
  218. orgSlug={organization.slug}
  219. params={{
  220. hasReleases: false,
  221. confirm,
  222. label,
  223. }}
  224. />
  225. )}
  226. <IgnoreActions
  227. onUpdate={onUpdate}
  228. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  229. confirmMessage={statusDetails =>
  230. confirm({action: ConfirmAction.IGNORE, canBeUndone: true, statusDetails})
  231. }
  232. confirmLabel={label('ignore')}
  233. issueCategory={issueCategory}
  234. disabled={ignoreDisabled}
  235. />
  236. {!nestMergeAndReview && (
  237. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  238. )}
  239. {!nestMergeAndReview && (
  240. <ActionLink
  241. aria-label={t('Merge Selected Issues')}
  242. type="button"
  243. disabled={mergeDisabled}
  244. onAction={onMerge}
  245. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  246. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  247. confirmLabel={label('merge')}
  248. title={makeMergeTooltip()}
  249. >
  250. {t('Merge')}
  251. </ActionLink>
  252. )}
  253. <DropdownMenuControl
  254. size="sm"
  255. items={menuItems}
  256. triggerProps={{
  257. 'aria-label': t('More issue actions'),
  258. icon: <IconEllipsis size="xs" />,
  259. showChevron: false,
  260. size: 'xs',
  261. }}
  262. isDisabled={!anySelected}
  263. />
  264. </Fragment>
  265. );
  266. }
  267. function isActionSupported(
  268. selectedIssues: BaseGroup[],
  269. capability: keyof IssueCategoryCapabilities
  270. ) {
  271. for (const issue of selectedIssues) {
  272. const info = getIssueCapability(issue.issueCategory, capability);
  273. if (!info.enabled) {
  274. return info;
  275. }
  276. }
  277. return {enabled: true};
  278. }
  279. export default ActionSet;