actionSet.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. multipleProjectsSelected: true,
  241. disabled: true,
  242. confirm,
  243. label,
  244. }}
  245. />
  246. )}
  247. {hasEscalatingIssuesUi ? null : (
  248. <IgnoreActions
  249. onUpdate={onUpdate}
  250. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  251. confirmMessage={() =>
  252. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  253. }
  254. confirmLabel={label('ignore')}
  255. disabled={ignoreDisabled}
  256. />
  257. )}
  258. {!nestMergeAndReview && (
  259. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  260. )}
  261. {!nestMergeAndReview && (
  262. <ActionLink
  263. aria-label={t('Merge Selected Issues')}
  264. type="button"
  265. disabled={mergeDisabled}
  266. onAction={onMerge}
  267. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  268. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  269. confirmLabel={label('merge')}
  270. title={makeMergeTooltip()}
  271. >
  272. {t('Merge')}
  273. </ActionLink>
  274. )}
  275. <DropdownMenu
  276. size="sm"
  277. items={menuItems}
  278. triggerProps={{
  279. 'aria-label': t('More issue actions'),
  280. icon: <IconEllipsis size="xs" />,
  281. showChevron: false,
  282. size: 'xs',
  283. }}
  284. isDisabled={!anySelected}
  285. />
  286. </Fragment>
  287. );
  288. }
  289. function isActionSupported(
  290. selectedIssues: BaseGroup[],
  291. actionType: keyof IssueTypeConfig['actions']
  292. ) {
  293. for (const issue of selectedIssues) {
  294. const info = getConfigForIssueType(issue).actions[actionType];
  295. if (!info.enabled) {
  296. return info;
  297. }
  298. }
  299. return {enabled: true};
  300. }
  301. export default ActionSet;