actionSet.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 IgnoreActions from 'sentry/components/actions/ignore';
  6. import {openConfirmModal} from 'sentry/components/confirm';
  7. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  8. import {IconEllipsis} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import GroupStore from 'sentry/stores/groupStore';
  11. import {BaseGroup, Project, ResolutionStatus} from 'sentry/types';
  12. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  13. import {IssueTypeConfig} from 'sentry/utils/issueTypeConfig/types';
  14. import Projects from 'sentry/utils/projects';
  15. import useMedia from 'sentry/utils/useMedia';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import ResolveActions from './resolveActions';
  18. import ReviewAction from './reviewAction';
  19. import {ConfirmAction, getConfirm, getLabel} from './utils';
  20. type Props = {
  21. allInQuerySelected: boolean;
  22. anySelected: boolean;
  23. issues: Set<string>;
  24. multiSelected: boolean;
  25. onDelete: () => void;
  26. onMerge: () => void;
  27. onShouldConfirm: (action: ConfirmAction) => boolean;
  28. onUpdate: (data?: any) => void;
  29. query: string;
  30. queryCount: number;
  31. selectedProjectSlug?: string;
  32. };
  33. function ActionSet({
  34. queryCount,
  35. query,
  36. allInQuerySelected,
  37. anySelected,
  38. multiSelected,
  39. issues,
  40. onUpdate,
  41. onShouldConfirm,
  42. onDelete,
  43. onMerge,
  44. selectedProjectSlug,
  45. }: Props) {
  46. const organization = useOrganization();
  47. const numIssues = issues.size;
  48. const confirm = getConfirm({
  49. numIssues,
  50. allInQuerySelected,
  51. query,
  52. queryCount,
  53. organization,
  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 hasEscalatingIssuesUi = organization.features.includes('escalating-issues-ui');
  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. ...(hasEscalatingIssuesUi
  111. ? []
  112. : [
  113. {
  114. key: 'mark-reviewed',
  115. label: t('Mark Reviewed'),
  116. hidden: !nestMergeAndReview,
  117. disabled: !canMarkReviewed,
  118. onAction: () => onUpdate({inbox: false}),
  119. },
  120. ]),
  121. {
  122. key: 'bookmark',
  123. label: t('Add to Bookmarks'),
  124. hidden: !canAddBookmark,
  125. onAction: () => {
  126. openConfirmModal({
  127. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  128. onConfirm: () => onUpdate({isBookmarked: true}),
  129. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  130. confirmText: label('bookmark'),
  131. });
  132. },
  133. },
  134. {
  135. key: 'remove-bookmark',
  136. label: t('Remove from Bookmarks'),
  137. hidden: !canRemoveBookmark,
  138. onAction: () => {
  139. openConfirmModal({
  140. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  141. onConfirm: () => onUpdate({isBookmarked: false}),
  142. message: confirm({
  143. action: ConfirmAction.UNBOOKMARK,
  144. canBeUndone: false,
  145. append: ' from your bookmarks',
  146. }),
  147. confirmText: label('remove', ' from your bookmarks'),
  148. });
  149. },
  150. },
  151. {
  152. key: 'unresolve',
  153. label: t('Set status to: Unresolved'),
  154. hidden: !canSetUnresolved,
  155. onAction: () => {
  156. openConfirmModal({
  157. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  158. onConfirm: () => onUpdate({status: ResolutionStatus.UNRESOLVED}),
  159. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  160. confirmText: label('unresolve'),
  161. });
  162. },
  163. },
  164. {
  165. key: 'delete',
  166. label: t('Delete'),
  167. priority: 'danger',
  168. disabled: !deleteSupported,
  169. details: deleteDisabledReason,
  170. onAction: () => {
  171. openConfirmModal({
  172. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  173. onConfirm: onDelete,
  174. priority: 'danger',
  175. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  176. confirmText: label('delete'),
  177. });
  178. },
  179. },
  180. ];
  181. return (
  182. <Fragment>
  183. {hasEscalatingIssuesUi ? (
  184. <ArchiveActions
  185. onUpdate={onUpdate}
  186. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  187. confirmMessage={() =>
  188. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  189. }
  190. confirmLabel={label('archive')}
  191. disabled={ignoreDisabled}
  192. />
  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. hasReleases: 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. projectId: 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. hasReleases: false,
  227. confirm,
  228. label,
  229. }}
  230. />
  231. )}
  232. {hasEscalatingIssuesUi ? null : (
  233. <IgnoreActions
  234. onUpdate={onUpdate}
  235. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  236. confirmMessage={() =>
  237. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  238. }
  239. confirmLabel={label('ignore')}
  240. disabled={ignoreDisabled}
  241. />
  242. )}
  243. {!nestMergeAndReview && !hasEscalatingIssuesUi && (
  244. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  245. )}
  246. {!nestMergeAndReview && (
  247. <ActionLink
  248. aria-label={t('Merge Selected Issues')}
  249. type="button"
  250. disabled={mergeDisabled}
  251. onAction={onMerge}
  252. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  253. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  254. confirmLabel={label('merge')}
  255. title={makeMergeTooltip()}
  256. >
  257. {t('Merge')}
  258. </ActionLink>
  259. )}
  260. <DropdownMenu
  261. size="sm"
  262. items={menuItems}
  263. triggerProps={{
  264. 'aria-label': t('More issue actions'),
  265. icon: <IconEllipsis size="xs" />,
  266. showChevron: false,
  267. size: 'xs',
  268. }}
  269. isDisabled={!anySelected}
  270. />
  271. </Fragment>
  272. );
  273. }
  274. function isActionSupported(
  275. selectedIssues: BaseGroup[],
  276. actionType: keyof IssueTypeConfig['actions']
  277. ) {
  278. for (const issue of selectedIssues) {
  279. const info = getConfigForIssueType(issue).actions[actionType];
  280. if (!info.enabled) {
  281. return info;
  282. }
  283. }
  284. return {enabled: true};
  285. }
  286. export default ActionSet;