actionSet.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 || selectedIssues.some(issue => issue.status === 'resolved');
  77. const makeMergeTooltip = () => {
  78. if (mergeDisabledReason) {
  79. return mergeDisabledReason;
  80. }
  81. if (multipleIssueProjectsSelected) {
  82. return t('Cannot merge issues from different projects');
  83. }
  84. return '';
  85. };
  86. // Determine whether to nest "Merge" and "Mark as Reviewed" buttons inside
  87. // the dropdown menu based on the current screen size
  88. const theme = useTheme();
  89. const nestMergeAndReview = useMedia(`(max-width: ${theme.breakpoints.xlarge})`);
  90. const disabledMarkReviewed = organization.features.includes('remove-mark-reviewed');
  91. const menuItems: MenuItemProps[] = [
  92. {
  93. key: 'merge',
  94. label: t('Merge'),
  95. hidden: !nestMergeAndReview,
  96. disabled: mergeDisabled,
  97. details: makeMergeTooltip(),
  98. onAction: () => {
  99. openConfirmModal({
  100. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  101. onConfirm: onMerge,
  102. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  103. confirmText: label('merge'),
  104. });
  105. },
  106. },
  107. ...(disabledMarkReviewed
  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. {
  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: ResolutionStatus.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. const hasEscalatingIssuesUI = organization.features.includes('escalating-issues-ui');
  179. return (
  180. <Fragment>
  181. {hasEscalatingIssuesUI ? (
  182. <ArchiveActions
  183. onUpdate={onUpdate}
  184. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  185. confirmMessage={() =>
  186. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  187. }
  188. confirmLabel={label('archive')}
  189. disabled={ignoreDisabled}
  190. />
  191. ) : null}
  192. {selectedProjectSlug ? (
  193. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  194. {({projects, initiallyLoaded, fetchError}) => {
  195. const selectedProject = projects[0];
  196. return (
  197. <ResolveActions
  198. onShouldConfirm={onShouldConfirm}
  199. onUpdate={onUpdate}
  200. anySelected={anySelected}
  201. params={{
  202. hasReleases: selectedProject.hasOwnProperty('features')
  203. ? (selectedProject as Project).features.includes('releases')
  204. : false,
  205. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  206. ? (selectedProject as Project).latestRelease
  207. : undefined,
  208. projectId: selectedProject.slug,
  209. confirm,
  210. label,
  211. loadingProjects: !initiallyLoaded,
  212. projectFetchError: !!fetchError,
  213. }}
  214. />
  215. );
  216. }}
  217. </Projects>
  218. ) : (
  219. <ResolveActions
  220. onShouldConfirm={onShouldConfirm}
  221. onUpdate={onUpdate}
  222. anySelected={anySelected}
  223. params={{
  224. hasReleases: false,
  225. confirm,
  226. label,
  227. }}
  228. />
  229. )}
  230. {hasEscalatingIssuesUI ? null : (
  231. <IgnoreActions
  232. onUpdate={onUpdate}
  233. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  234. confirmMessage={() =>
  235. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  236. }
  237. confirmLabel={label('ignore')}
  238. disabled={ignoreDisabled}
  239. />
  240. )}
  241. {!nestMergeAndReview && !disabledMarkReviewed && (
  242. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  243. )}
  244. {!nestMergeAndReview && (
  245. <ActionLink
  246. aria-label={t('Merge Selected Issues')}
  247. type="button"
  248. disabled={mergeDisabled}
  249. onAction={onMerge}
  250. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  251. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  252. confirmLabel={label('merge')}
  253. title={makeMergeTooltip()}
  254. >
  255. {t('Merge')}
  256. </ActionLink>
  257. )}
  258. <DropdownMenu
  259. size="sm"
  260. items={menuItems}
  261. triggerProps={{
  262. 'aria-label': t('More issue actions'),
  263. icon: <IconEllipsis size="xs" />,
  264. showChevron: false,
  265. size: 'xs',
  266. }}
  267. isDisabled={!anySelected}
  268. />
  269. </Fragment>
  270. );
  271. }
  272. function isActionSupported(
  273. selectedIssues: BaseGroup[],
  274. actionType: keyof IssueTypeConfig['actions']
  275. ) {
  276. for (const issue of selectedIssues) {
  277. const info = getConfigForIssueType(issue).actions[actionType];
  278. if (!info.enabled) {
  279. return info;
  280. }
  281. }
  282. return {enabled: true};
  283. }
  284. export default ActionSet;