actionSet.tsx 9.1 KB

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