actionSet.tsx 8.7 KB

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