actionSet.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import ActionLink from 'sentry/components/actions/actionLink';
  4. import ArchiveActions from 'sentry/components/actions/archive';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import {Button} from 'sentry/components/button';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  9. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  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} 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 theme = useTheme();
  94. const nestMergeAndReview = useMedia(`(max-width: ${theme.breakpoints.xlarge})`);
  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}),
  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: () => onUpdate({status: GroupStatus.UNRESOLVED}),
  187. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  188. confirmText: label('unarchive'),
  189. });
  190. }}
  191. disabled={!anySelected}
  192. >
  193. {t('Unarchive')}
  194. </Button>
  195. ) : null}
  196. {selectedProjectSlug ? (
  197. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  198. {({projects, initiallyLoaded, fetchError}) => {
  199. const selectedProject = projects[0];
  200. return (
  201. <ResolveActions
  202. onShouldConfirm={onShouldConfirm}
  203. onUpdate={onUpdate}
  204. anySelected={anySelected}
  205. params={{
  206. hasRelease: selectedProject.hasOwnProperty('features')
  207. ? (selectedProject as Project).features.includes('releases')
  208. : false,
  209. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  210. ? (selectedProject as Project).latestRelease
  211. : undefined,
  212. projectSlug: selectedProject.slug,
  213. confirm,
  214. label,
  215. loadingProjects: !initiallyLoaded,
  216. projectFetchError: !!fetchError,
  217. }}
  218. />
  219. );
  220. }}
  221. </Projects>
  222. ) : (
  223. <ResolveActions
  224. onShouldConfirm={onShouldConfirm}
  225. onUpdate={onUpdate}
  226. anySelected={anySelected}
  227. params={{
  228. hasRelease: false,
  229. multipleProjectsSelected: true,
  230. disabled: true,
  231. confirm,
  232. label,
  233. }}
  234. />
  235. )}
  236. <GuideAnchor
  237. target="issue_stream_archive_button"
  238. position="bottom"
  239. disabled={ignoreDisabled}
  240. >
  241. <ArchiveActions
  242. onUpdate={onUpdate}
  243. shouldConfirm={onShouldConfirm(ConfirmAction.ARCHIVE)}
  244. confirmMessage={() =>
  245. confirm({action: ConfirmAction.ARCHIVE, canBeUndone: true})
  246. }
  247. confirmLabel={label('archive')}
  248. disabled={ignoreDisabled}
  249. />
  250. </GuideAnchor>
  251. {!nestMergeAndReview && (
  252. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  253. )}
  254. {!nestMergeAndReview && (
  255. <ActionLink
  256. aria-label={t('Merge Selected Issues')}
  257. type="button"
  258. disabled={mergeDisabled}
  259. onAction={onMerge}
  260. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  261. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  262. confirmLabel={label('merge')}
  263. title={makeMergeTooltip()}
  264. >
  265. {t('Merge')}
  266. </ActionLink>
  267. )}
  268. <DropdownMenu
  269. size="sm"
  270. items={menuItems}
  271. triggerProps={{
  272. 'aria-label': t('More issue actions'),
  273. icon: <IconEllipsis />,
  274. showChevron: false,
  275. size: 'xs',
  276. }}
  277. isDisabled={!anySelected}
  278. />
  279. </Fragment>
  280. );
  281. }
  282. function isActionSupported(
  283. selectedIssues: BaseGroup[],
  284. actionType: keyof IssueTypeConfig['actions']
  285. ) {
  286. for (const issue of selectedIssues) {
  287. const info = getConfigForIssueType(issue, issue.project).actions[actionType];
  288. if (!info.enabled) {
  289. return info;
  290. }
  291. }
  292. return {enabled: true};
  293. }
  294. export default ActionSet;