actionSet.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 type {IssueUpdateData} from 'sentry/views/issueList/types';
  21. import ResolveActions from './resolveActions';
  22. import ReviewAction from './reviewAction';
  23. import {ConfirmAction, getConfirm, getLabel} from './utils';
  24. type Props = {
  25. allInQuerySelected: boolean;
  26. anySelected: boolean;
  27. issues: Set<string>;
  28. multiSelected: boolean;
  29. onDelete: () => void;
  30. onMerge: () => void;
  31. onShouldConfirm: (action: ConfirmAction) => boolean;
  32. onUpdate: (data: IssueUpdateData) => void;
  33. query: string;
  34. queryCount: number;
  35. selectedProjectSlug?: string;
  36. };
  37. function ActionSet({
  38. queryCount,
  39. query,
  40. allInQuerySelected,
  41. anySelected,
  42. multiSelected,
  43. issues,
  44. onUpdate,
  45. onShouldConfirm,
  46. onDelete,
  47. onMerge,
  48. selectedProjectSlug,
  49. }: Props) {
  50. const organization = useOrganization();
  51. const numIssues = issues.size;
  52. const confirm = getConfirm({
  53. numIssues,
  54. allInQuerySelected,
  55. query,
  56. queryCount,
  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 ||
  80. selectedIssues.some(
  81. issue => issue.status === 'resolved' || issue.status === 'ignored'
  82. );
  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 nestMergeAndReview = useMedia(`(max-width: 1700px`);
  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. ...(organization.features.includes('issue-priority-ui')
  162. ? [
  163. {
  164. key: 'set-priority',
  165. label: t('Set Priority to...'),
  166. hidden: !organization.features.includes('issue-priority-ui'),
  167. isSubmenu: true,
  168. children: [PriorityLevel.HIGH, PriorityLevel.MEDIUM, PriorityLevel.LOW].map(
  169. priority => ({
  170. key: `priority-${priority}`,
  171. textValue: t('Set priority to %s', priority),
  172. label: <GroupPriorityBadge priority={priority} />,
  173. onAction: () =>
  174. openConfirmModal({
  175. bypass: !onShouldConfirm(ConfirmAction.SET_PRIORITY),
  176. onConfirm: () => onUpdate({priority}),
  177. message: confirm({
  178. action: ConfirmAction.SET_PRIORITY,
  179. append: ` to ${priority}`,
  180. canBeUndone: true,
  181. }),
  182. confirmText: label('reprioritize'),
  183. }),
  184. })
  185. ),
  186. },
  187. ]
  188. : []),
  189. {
  190. key: 'delete',
  191. label: t('Delete'),
  192. priority: 'danger',
  193. disabled: !deleteSupported,
  194. details: deleteDisabledReason,
  195. onAction: () => {
  196. openConfirmModal({
  197. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  198. onConfirm: onDelete,
  199. priority: 'danger',
  200. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  201. confirmText: label('delete'),
  202. });
  203. },
  204. },
  205. ];
  206. return (
  207. <Fragment>
  208. {query.includes('is:archived') ? (
  209. <Button
  210. size="xs"
  211. onClick={() => {
  212. openConfirmModal({
  213. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  214. onConfirm: () =>
  215. onUpdate({status: GroupStatus.UNRESOLVED, statusDetails: {}}),
  216. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  217. confirmText: label('unarchive'),
  218. });
  219. }}
  220. disabled={!anySelected}
  221. >
  222. {t('Unarchive')}
  223. </Button>
  224. ) : null}
  225. {selectedProjectSlug ? (
  226. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  227. {({projects, initiallyLoaded, fetchError}) => {
  228. const selectedProject = projects[0];
  229. return (
  230. <ResolveActions
  231. onShouldConfirm={onShouldConfirm}
  232. onUpdate={onUpdate}
  233. anySelected={anySelected}
  234. params={{
  235. hasRelease: selectedProject.hasOwnProperty('features')
  236. ? (selectedProject as Project).features.includes('releases')
  237. : false,
  238. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  239. ? (selectedProject as Project).latestRelease
  240. : undefined,
  241. projectSlug: selectedProject.slug,
  242. confirm,
  243. label,
  244. loadingProjects: !initiallyLoaded,
  245. projectFetchError: !!fetchError,
  246. }}
  247. />
  248. );
  249. }}
  250. </Projects>
  251. ) : (
  252. <ResolveActions
  253. onShouldConfirm={onShouldConfirm}
  254. onUpdate={onUpdate}
  255. anySelected={anySelected}
  256. params={{
  257. hasRelease: false,
  258. multipleProjectsSelected: true,
  259. disabled: true,
  260. confirm,
  261. label,
  262. }}
  263. />
  264. )}
  265. <GuideAnchor
  266. target="issue_stream_archive_button"
  267. position="bottom"
  268. disabled={ignoreDisabled}
  269. >
  270. <ArchiveActions
  271. onUpdate={onUpdate}
  272. shouldConfirm={onShouldConfirm(ConfirmAction.ARCHIVE)}
  273. confirmMessage={() =>
  274. confirm({action: ConfirmAction.ARCHIVE, canBeUndone: true})
  275. }
  276. confirmLabel={label('archive')}
  277. disabled={ignoreDisabled}
  278. />
  279. </GuideAnchor>
  280. {!nestMergeAndReview && (
  281. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  282. )}
  283. {!nestMergeAndReview && (
  284. <ActionLink
  285. aria-label={t('Merge Selected Issues')}
  286. type="button"
  287. disabled={mergeDisabled}
  288. onAction={onMerge}
  289. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  290. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  291. confirmLabel={label('merge')}
  292. title={makeMergeTooltip()}
  293. >
  294. {t('Merge')}
  295. </ActionLink>
  296. )}
  297. <DropdownMenu
  298. size="sm"
  299. items={menuItems}
  300. triggerProps={{
  301. 'aria-label': t('More issue actions'),
  302. icon: <IconEllipsis />,
  303. showChevron: false,
  304. size: 'xs',
  305. }}
  306. isDisabled={!anySelected}
  307. />
  308. </Fragment>
  309. );
  310. }
  311. function isActionSupported(
  312. selectedIssues: BaseGroup[],
  313. actionType: keyof IssueTypeConfig['actions']
  314. ) {
  315. for (const issue of selectedIssues) {
  316. const info = getConfigForIssueType(issue, issue.project).actions[actionType];
  317. if (!info.enabled) {
  318. return info;
  319. }
  320. }
  321. return {enabled: true};
  322. }
  323. export default ActionSet;