actionSet.tsx 9.5 KB

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