actionSet.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import {Fragment} from 'react';
  2. import ActionLink from 'sentry/components/actions/actionLink';
  3. import ArchiveActions from 'sentry/components/actions/archive';
  4. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  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 {GroupPriorityBadge} 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, Project} from 'sentry/types';
  14. import {GroupStatus, PriorityLevel} from 'sentry/types';
  15. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  16. import type {IssueTypeConfig} from 'sentry/utils/issueTypeConfig/types';
  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. });
  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 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 nestMergeAndReview = useMedia(`(max-width: 1700px`);
  94. const menuItems: MenuItemProps[] = [
  95. {
  96. key: 'merge',
  97. label: t('Merge'),
  98. hidden: !nestMergeAndReview,
  99. disabled: mergeDisabled,
  100. details: makeMergeTooltip(),
  101. onAction: () => {
  102. openConfirmModal({
  103. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  104. onConfirm: onMerge,
  105. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  106. confirmText: label('merge'),
  107. });
  108. },
  109. },
  110. {
  111. key: 'mark-reviewed',
  112. label: t('Mark Reviewed'),
  113. hidden: !nestMergeAndReview,
  114. disabled: !canMarkReviewed,
  115. onAction: () => onUpdate({inbox: false}),
  116. },
  117. {
  118. key: 'bookmark',
  119. label: t('Add to Bookmarks'),
  120. hidden: !canAddBookmark,
  121. onAction: () => {
  122. openConfirmModal({
  123. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  124. onConfirm: () => onUpdate({isBookmarked: true}),
  125. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  126. confirmText: label('bookmark'),
  127. });
  128. },
  129. },
  130. {
  131. key: 'remove-bookmark',
  132. label: t('Remove from Bookmarks'),
  133. hidden: !canRemoveBookmark,
  134. onAction: () => {
  135. openConfirmModal({
  136. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  137. onConfirm: () => onUpdate({isBookmarked: false}),
  138. message: confirm({
  139. action: ConfirmAction.UNBOOKMARK,
  140. canBeUndone: false,
  141. append: ' from your bookmarks',
  142. }),
  143. confirmText: label('remove', ' from your bookmarks'),
  144. });
  145. },
  146. },
  147. {
  148. key: 'unresolve',
  149. label: t('Set status to: Unresolved'),
  150. hidden: !canSetUnresolved,
  151. onAction: () => {
  152. openConfirmModal({
  153. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  154. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED}),
  155. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  156. confirmText: label('unresolve'),
  157. });
  158. },
  159. },
  160. ...(organization.features.includes('issue-priority-ui')
  161. ? [
  162. {
  163. key: 'set-priority',
  164. label: t('Set Priority to...'),
  165. hidden: !organization.features.includes('issue-priority-ui'),
  166. isSubmenu: true,
  167. children: [PriorityLevel.HIGH, PriorityLevel.MEDIUM, PriorityLevel.LOW].map(
  168. priority => ({
  169. key: `priority-${priority}`,
  170. textValue: t('Set priority to %s', priority),
  171. label: <GroupPriorityBadge priority={priority} />,
  172. onAction: () =>
  173. openConfirmModal({
  174. bypass: !onShouldConfirm(ConfirmAction.SET_PRIORITY),
  175. onConfirm: () => onUpdate({priority}),
  176. message: confirm({
  177. action: ConfirmAction.SET_PRIORITY,
  178. append: ` to ${priority}`,
  179. canBeUndone: true,
  180. }),
  181. confirmText: label('reprioritize'),
  182. }),
  183. })
  184. ),
  185. },
  186. ]
  187. : []),
  188. {
  189. key: 'delete',
  190. label: t('Delete'),
  191. priority: 'danger',
  192. disabled: !deleteSupported,
  193. details: deleteDisabledReason,
  194. onAction: () => {
  195. openConfirmModal({
  196. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  197. onConfirm: onDelete,
  198. priority: 'danger',
  199. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  200. confirmText: label('delete'),
  201. });
  202. },
  203. },
  204. ];
  205. return (
  206. <Fragment>
  207. {query.includes('is:archived') ? (
  208. <Button
  209. size="xs"
  210. onClick={() => {
  211. openConfirmModal({
  212. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  213. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED}),
  214. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  215. confirmText: label('unarchive'),
  216. });
  217. }}
  218. disabled={!anySelected}
  219. >
  220. {t('Unarchive')}
  221. </Button>
  222. ) : null}
  223. {selectedProjectSlug ? (
  224. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  225. {({projects, initiallyLoaded, fetchError}) => {
  226. const selectedProject = projects[0];
  227. return (
  228. <ResolveActions
  229. onShouldConfirm={onShouldConfirm}
  230. onUpdate={onUpdate}
  231. anySelected={anySelected}
  232. params={{
  233. hasRelease: selectedProject.hasOwnProperty('features')
  234. ? (selectedProject as Project).features.includes('releases')
  235. : false,
  236. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  237. ? (selectedProject as Project).latestRelease
  238. : undefined,
  239. projectSlug: selectedProject.slug,
  240. confirm,
  241. label,
  242. loadingProjects: !initiallyLoaded,
  243. projectFetchError: !!fetchError,
  244. }}
  245. />
  246. );
  247. }}
  248. </Projects>
  249. ) : (
  250. <ResolveActions
  251. onShouldConfirm={onShouldConfirm}
  252. onUpdate={onUpdate}
  253. anySelected={anySelected}
  254. params={{
  255. hasRelease: false,
  256. multipleProjectsSelected: true,
  257. disabled: true,
  258. confirm,
  259. label,
  260. }}
  261. />
  262. )}
  263. <GuideAnchor
  264. target="issue_stream_archive_button"
  265. position="bottom"
  266. disabled={ignoreDisabled}
  267. >
  268. <ArchiveActions
  269. onUpdate={onUpdate}
  270. shouldConfirm={onShouldConfirm(ConfirmAction.ARCHIVE)}
  271. confirmMessage={() =>
  272. confirm({action: ConfirmAction.ARCHIVE, canBeUndone: true})
  273. }
  274. confirmLabel={label('archive')}
  275. disabled={ignoreDisabled}
  276. />
  277. </GuideAnchor>
  278. {!nestMergeAndReview && (
  279. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  280. )}
  281. {!nestMergeAndReview && (
  282. <ActionLink
  283. aria-label={t('Merge Selected Issues')}
  284. type="button"
  285. disabled={mergeDisabled}
  286. onAction={onMerge}
  287. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  288. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  289. confirmLabel={label('merge')}
  290. title={makeMergeTooltip()}
  291. >
  292. {t('Merge')}
  293. </ActionLink>
  294. )}
  295. <DropdownMenu
  296. size="sm"
  297. items={menuItems}
  298. triggerProps={{
  299. 'aria-label': t('More issue actions'),
  300. icon: <IconEllipsis />,
  301. showChevron: false,
  302. size: 'xs',
  303. }}
  304. isDisabled={!anySelected}
  305. />
  306. </Fragment>
  307. );
  308. }
  309. function isActionSupported(
  310. selectedIssues: BaseGroup[],
  311. actionType: keyof IssueTypeConfig['actions']
  312. ) {
  313. for (const issue of selectedIssues) {
  314. const info = getConfigForIssueType(issue, issue.project).actions[actionType];
  315. if (!info.enabled) {
  316. return info;
  317. }
  318. }
  319. return {enabled: true};
  320. }
  321. export default ActionSet;