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 {makeGroupPriorityDropdownOptions} from 'sentry/components/badge/groupPriority';
  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 {IconEllipsis} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import GroupStore from 'sentry/stores/groupStore';
  12. import type {BaseGroup} from 'sentry/types/group';
  13. import {GroupStatus} from 'sentry/types/group';
  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 {FOR_REVIEW_QUERIES} from 'sentry/views/issueList/utils';
  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. const nestMergeAndReviewViewport = useMedia(`(max-width: 1700px)`);
  93. const nestReview = hasIssuePriority
  94. ? !FOR_REVIEW_QUERIES.includes(query)
  95. : nestMergeAndReviewViewport;
  96. const nestMerge = hasIssuePriority ? true : nestMergeAndReviewViewport;
  97. const menuItems: MenuItemProps[] = [
  98. {
  99. key: 'merge',
  100. label: t('Merge'),
  101. hidden: !nestMerge,
  102. disabled: mergeDisabled,
  103. details: makeMergeTooltip(),
  104. onAction: () => {
  105. openConfirmModal({
  106. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  107. onConfirm: onMerge,
  108. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  109. confirmText: label('merge'),
  110. });
  111. },
  112. },
  113. {
  114. key: 'mark-reviewed',
  115. label: t('Mark Reviewed'),
  116. hidden: !nestReview,
  117. disabled: !canMarkReviewed,
  118. onAction: () => onUpdate({inbox: false}),
  119. },
  120. {
  121. key: 'bookmark',
  122. label: t('Add to Bookmarks'),
  123. hidden: !canAddBookmark,
  124. onAction: () => {
  125. openConfirmModal({
  126. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  127. onConfirm: () => onUpdate({isBookmarked: true}),
  128. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  129. confirmText: label('bookmark'),
  130. });
  131. },
  132. },
  133. {
  134. key: 'remove-bookmark',
  135. label: t('Remove from Bookmarks'),
  136. hidden: !canRemoveBookmark,
  137. onAction: () => {
  138. openConfirmModal({
  139. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  140. onConfirm: () => onUpdate({isBookmarked: false}),
  141. message: confirm({
  142. action: ConfirmAction.UNBOOKMARK,
  143. canBeUndone: false,
  144. append: ' from your bookmarks',
  145. }),
  146. confirmText: label('remove', ' from your bookmarks'),
  147. });
  148. },
  149. },
  150. {
  151. key: 'unresolve',
  152. label: t('Set status to: Unresolved'),
  153. hidden: !canSetUnresolved,
  154. onAction: () => {
  155. openConfirmModal({
  156. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  157. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED, statusDetails: {}}),
  158. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  159. confirmText: label('unresolve'),
  160. });
  161. },
  162. },
  163. {
  164. key: 'delete',
  165. label: t('Delete'),
  166. priority: 'danger',
  167. disabled: !deleteSupported,
  168. details: deleteDisabledReason,
  169. onAction: () => {
  170. openConfirmModal({
  171. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  172. onConfirm: onDelete,
  173. priority: 'danger',
  174. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  175. confirmText: label('delete'),
  176. });
  177. },
  178. },
  179. ];
  180. return (
  181. <Fragment>
  182. {query.includes('is:archived') ? (
  183. <Button
  184. size="xs"
  185. onClick={() => {
  186. openConfirmModal({
  187. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  188. onConfirm: () =>
  189. onUpdate({status: GroupStatus.UNRESOLVED, statusDetails: {}}),
  190. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  191. confirmText: label('unarchive'),
  192. });
  193. }}
  194. disabled={!anySelected}
  195. >
  196. {t('Unarchive')}
  197. </Button>
  198. ) : null}
  199. <ResolveActions
  200. onShouldConfirm={onShouldConfirm}
  201. onUpdate={onUpdate}
  202. anySelected={anySelected}
  203. confirm={confirm}
  204. label={label}
  205. selectedProjectSlug={selectedProjectSlug}
  206. />
  207. <ArchiveActions
  208. onUpdate={onUpdate}
  209. shouldConfirm={onShouldConfirm(ConfirmAction.ARCHIVE)}
  210. confirmMessage={() => confirm({action: ConfirmAction.ARCHIVE, canBeUndone: true})}
  211. confirmLabel={label('archive')}
  212. disabled={ignoreDisabled}
  213. />
  214. {hasIssuePriority && (
  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. )}
  234. {!nestReview && <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />}
  235. {!nestMerge && (
  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;