actionSet.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 {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. organization,
  55. });
  56. const label = getLabel(numIssues, allInQuerySelected);
  57. const selectedIssues = [...issues]
  58. .map(issueId => GroupStore.get(issueId))
  59. .filter(issue => issue) as BaseGroup[];
  60. // Merges require multiple issues of a single project type
  61. const multipleIssueProjectsSelected = multiSelected && !selectedProjectSlug;
  62. const {enabled: mergeSupported, disabledReason: mergeDisabledReason} =
  63. isActionSupported(selectedIssues, 'merge');
  64. const {enabled: deleteSupported, disabledReason: deleteDisabledReason} =
  65. isActionSupported(selectedIssues, 'delete');
  66. const mergeDisabled =
  67. !multiSelected || multipleIssueProjectsSelected || !mergeSupported;
  68. const ignoreDisabled = !anySelected;
  69. const canMarkReviewed =
  70. anySelected && (allInQuerySelected || selectedIssues.some(issue => !!issue?.inbox));
  71. // determine which ... dropdown options to show based on issue(s) selected
  72. const canAddBookmark =
  73. allInQuerySelected || selectedIssues.some(issue => !issue.isBookmarked);
  74. const canRemoveBookmark =
  75. allInQuerySelected || selectedIssues.some(issue => issue.isBookmarked);
  76. const canSetUnresolved =
  77. allInQuerySelected ||
  78. selectedIssues.some(
  79. issue => issue.status === 'resolved' || issue.status === 'ignored'
  80. );
  81. const makeMergeTooltip = () => {
  82. if (mergeDisabledReason) {
  83. return mergeDisabledReason;
  84. }
  85. if (multipleIssueProjectsSelected) {
  86. return t('Cannot merge issues from different projects');
  87. }
  88. return '';
  89. };
  90. // Determine whether to nest "Merge" and "Mark as Reviewed" buttons inside
  91. // the dropdown menu based on the current screen size
  92. const theme = useTheme();
  93. const nestMergeAndReview = useMedia(`(max-width: ${theme.breakpoints.xlarge})`);
  94. const hasEscalatingIssuesUi = organization.features.includes('escalating-issues');
  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. {hasEscalatingIssuesUi && 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. {hasEscalatingIssuesUi ? (
  197. <ArchiveActions
  198. onUpdate={onUpdate}
  199. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  200. confirmMessage={() =>
  201. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  202. }
  203. confirmLabel={label('archive')}
  204. disabled={ignoreDisabled}
  205. />
  206. ) : null}
  207. {selectedProjectSlug ? (
  208. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  209. {({projects, initiallyLoaded, fetchError}) => {
  210. const selectedProject = projects[0];
  211. return (
  212. <ResolveActions
  213. onShouldConfirm={onShouldConfirm}
  214. onUpdate={onUpdate}
  215. anySelected={anySelected}
  216. params={{
  217. hasRelease: selectedProject.hasOwnProperty('features')
  218. ? (selectedProject as Project).features.includes('releases')
  219. : false,
  220. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  221. ? (selectedProject as Project).latestRelease
  222. : undefined,
  223. projectSlug: selectedProject.slug,
  224. confirm,
  225. label,
  226. loadingProjects: !initiallyLoaded,
  227. projectFetchError: !!fetchError,
  228. }}
  229. />
  230. );
  231. }}
  232. </Projects>
  233. ) : (
  234. <ResolveActions
  235. onShouldConfirm={onShouldConfirm}
  236. onUpdate={onUpdate}
  237. anySelected={anySelected}
  238. params={{
  239. hasRelease: false,
  240. confirm,
  241. label,
  242. }}
  243. />
  244. )}
  245. {hasEscalatingIssuesUi ? null : (
  246. <IgnoreActions
  247. onUpdate={onUpdate}
  248. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  249. confirmMessage={() =>
  250. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  251. }
  252. confirmLabel={label('ignore')}
  253. disabled={ignoreDisabled}
  254. />
  255. )}
  256. {!nestMergeAndReview && (
  257. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  258. )}
  259. {!nestMergeAndReview && (
  260. <ActionLink
  261. aria-label={t('Merge Selected Issues')}
  262. type="button"
  263. disabled={mergeDisabled}
  264. onAction={onMerge}
  265. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  266. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  267. confirmLabel={label('merge')}
  268. title={makeMergeTooltip()}
  269. >
  270. {t('Merge')}
  271. </ActionLink>
  272. )}
  273. <DropdownMenu
  274. size="sm"
  275. items={menuItems}
  276. triggerProps={{
  277. 'aria-label': t('More issue actions'),
  278. icon: <IconEllipsis size="xs" />,
  279. showChevron: false,
  280. size: 'xs',
  281. }}
  282. isDisabled={!anySelected}
  283. />
  284. </Fragment>
  285. );
  286. }
  287. function isActionSupported(
  288. selectedIssues: BaseGroup[],
  289. actionType: keyof IssueTypeConfig['actions']
  290. ) {
  291. for (const issue of selectedIssues) {
  292. const info = getConfigForIssueType(issue).actions[actionType];
  293. if (!info.enabled) {
  294. return info;
  295. }
  296. }
  297. return {enabled: true};
  298. }
  299. export default ActionSet;