actionSet.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import {Fragment} from 'react';
  2. import ActionLink from 'sentry/components/actions/actionLink';
  3. import ArchiveActions from 'sentry/components/actions/archive';
  4. import {IssueActionWrapper} from 'sentry/components/actions/issueActionWrapper';
  5. import {Button} from 'sentry/components/button';
  6. import {openConfirmModal} from 'sentry/components/confirm';
  7. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  8. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  9. import {makeGroupPriorityDropdownOptions} from 'sentry/components/group/groupPriority';
  10. import {IconEllipsis} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import GroupStore from 'sentry/stores/groupStore';
  13. import type {BaseGroup} from 'sentry/types';
  14. import {GroupStatus} from 'sentry/types';
  15. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  16. import type {IssueTypeConfig} from 'sentry/utils/issueTypeConfig/types';
  17. import useMedia from 'sentry/utils/useMedia';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import type {IssueUpdateData} from 'sentry/views/issueList/types';
  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: IssueUpdateData) => 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. });
  57. const label = getLabel(numIssues, allInQuerySelected);
  58. const selectedIssues = [...issues]
  59. .map(issueId => GroupStore.get(issueId))
  60. .filter(issue => issue) as BaseGroup[];
  61. // Merges require multiple issues of a single project type
  62. const multipleIssueProjectsSelected = multiSelected && !selectedProjectSlug;
  63. const {enabled: mergeSupported, disabledReason: mergeDisabledReason} =
  64. isActionSupported(selectedIssues, 'merge');
  65. const {enabled: deleteSupported, disabledReason: deleteDisabledReason} =
  66. isActionSupported(selectedIssues, 'delete');
  67. const mergeDisabled =
  68. !multiSelected || multipleIssueProjectsSelected || !mergeSupported;
  69. const ignoreDisabled = !anySelected;
  70. const canMarkReviewed =
  71. anySelected && (allInQuerySelected || selectedIssues.some(issue => !!issue?.inbox));
  72. // determine which ... dropdown options to show based on issue(s) selected
  73. const canAddBookmark =
  74. allInQuerySelected || selectedIssues.some(issue => !issue.isBookmarked);
  75. const canRemoveBookmark =
  76. allInQuerySelected || selectedIssues.some(issue => issue.isBookmarked);
  77. const canSetUnresolved =
  78. allInQuerySelected ||
  79. selectedIssues.some(
  80. issue => issue.status === 'resolved' || issue.status === 'ignored'
  81. );
  82. const hasIssuePriority = organization.features.includes('issue-priority-ui');
  83. const makeMergeTooltip = () => {
  84. if (mergeDisabledReason) {
  85. return mergeDisabledReason;
  86. }
  87. if (multipleIssueProjectsSelected) {
  88. return t('Cannot merge issues from different projects');
  89. }
  90. return '';
  91. };
  92. // Determine whether to nest "Merge" and "Mark as Reviewed" buttons inside
  93. // the dropdown menu based on the current screen size
  94. const nestMergeAndReviewViewport = useMedia(`(max-width: 1700px)`);
  95. const nestMergeAndReview = nestMergeAndReviewViewport && !hasIssuePriority;
  96. const menuItems: MenuItemProps[] = [
  97. {
  98. key: 'merge',
  99. label: t('Merge'),
  100. hidden: !nestMergeAndReview,
  101. disabled: mergeDisabled,
  102. details: makeMergeTooltip(),
  103. onAction: () => {
  104. openConfirmModal({
  105. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  106. onConfirm: onMerge,
  107. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  108. confirmText: label('merge'),
  109. });
  110. },
  111. },
  112. {
  113. key: 'mark-reviewed',
  114. label: t('Mark Reviewed'),
  115. hidden: !nestMergeAndReview,
  116. disabled: !canMarkReviewed,
  117. onAction: () => onUpdate({inbox: false}),
  118. },
  119. {
  120. key: 'bookmark',
  121. label: t('Add to Bookmarks'),
  122. hidden: !canAddBookmark,
  123. onAction: () => {
  124. openConfirmModal({
  125. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  126. onConfirm: () => onUpdate({isBookmarked: true}),
  127. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  128. confirmText: label('bookmark'),
  129. });
  130. },
  131. },
  132. {
  133. key: 'remove-bookmark',
  134. label: t('Remove from Bookmarks'),
  135. hidden: !canRemoveBookmark,
  136. onAction: () => {
  137. openConfirmModal({
  138. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  139. onConfirm: () => onUpdate({isBookmarked: false}),
  140. message: confirm({
  141. action: ConfirmAction.UNBOOKMARK,
  142. canBeUndone: false,
  143. append: ' from your bookmarks',
  144. }),
  145. confirmText: label('remove', ' from your bookmarks'),
  146. });
  147. },
  148. },
  149. {
  150. key: 'unresolve',
  151. label: t('Set status to: Unresolved'),
  152. hidden: !canSetUnresolved,
  153. onAction: () => {
  154. openConfirmModal({
  155. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  156. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED, statusDetails: {}}),
  157. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  158. confirmText: label('unresolve'),
  159. });
  160. },
  161. },
  162. {
  163. key: 'delete',
  164. label: t('Delete'),
  165. priority: 'danger',
  166. disabled: !deleteSupported,
  167. details: deleteDisabledReason,
  168. onAction: () => {
  169. openConfirmModal({
  170. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  171. onConfirm: onDelete,
  172. priority: 'danger',
  173. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  174. confirmText: label('delete'),
  175. });
  176. },
  177. },
  178. ];
  179. return (
  180. <Fragment>
  181. {query.includes('is:archived') ? (
  182. <Button
  183. size="xs"
  184. onClick={() => {
  185. openConfirmModal({
  186. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  187. onConfirm: () =>
  188. onUpdate({status: GroupStatus.UNRESOLVED, statusDetails: {}}),
  189. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  190. confirmText: label('unarchive'),
  191. });
  192. }}
  193. disabled={!anySelected}
  194. >
  195. {t('Unarchive')}
  196. </Button>
  197. ) : null}
  198. <ResolveActions
  199. onShouldConfirm={onShouldConfirm}
  200. onUpdate={onUpdate}
  201. anySelected={anySelected}
  202. confirm={confirm}
  203. label={label}
  204. selectedProjectSlug={selectedProjectSlug}
  205. />
  206. <ArchiveActions
  207. onUpdate={onUpdate}
  208. shouldConfirm={onShouldConfirm(ConfirmAction.ARCHIVE)}
  209. confirmMessage={() => confirm({action: ConfirmAction.ARCHIVE, canBeUndone: true})}
  210. confirmLabel={label('archive')}
  211. disabled={ignoreDisabled}
  212. />
  213. {hasIssuePriority && (
  214. <IssueActionWrapper>
  215. <DropdownMenu
  216. triggerLabel={t('Set Priority')}
  217. size="xs"
  218. items={makeGroupPriorityDropdownOptions({
  219. onChange: priority => {
  220. openConfirmModal({
  221. bypass: !onShouldConfirm(ConfirmAction.SET_PRIORITY),
  222. onConfirm: () => onUpdate({priority}),
  223. message: confirm({
  224. action: ConfirmAction.SET_PRIORITY,
  225. append: ` to ${priority}`,
  226. canBeUndone: true,
  227. }),
  228. confirmText: label('reprioritize'),
  229. });
  230. },
  231. })}
  232. />
  233. </IssueActionWrapper>
  234. )}
  235. {!nestMergeAndReview && (
  236. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  237. )}
  238. {!nestMergeAndReview && (
  239. <IssueActionWrapper>
  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. </IssueActionWrapper>
  253. )}
  254. <IssueActionWrapper>
  255. <DropdownMenu
  256. size="sm"
  257. items={menuItems}
  258. triggerProps={{
  259. 'aria-label': t('More issue actions'),
  260. icon: <IconEllipsis />,
  261. showChevron: false,
  262. size: 'xs',
  263. }}
  264. isDisabled={!anySelected}
  265. />
  266. </IssueActionWrapper>
  267. </Fragment>
  268. );
  269. }
  270. function isActionSupported(
  271. selectedIssues: BaseGroup[],
  272. actionType: keyof IssueTypeConfig['actions']
  273. ) {
  274. for (const issue of selectedIssues) {
  275. const info = getConfigForIssueType(issue, issue.project).actions[actionType];
  276. if (!info.enabled) {
  277. return info;
  278. }
  279. }
  280. return {enabled: true};
  281. }
  282. export default ActionSet;