actionSet.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, Project, ResolutionStatus} 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. ...(hasEscalatingIssuesUi
  112. ? []
  113. : [
  114. {
  115. key: 'mark-reviewed',
  116. label: t('Mark Reviewed'),
  117. hidden: !nestMergeAndReview,
  118. disabled: !canMarkReviewed,
  119. onAction: () => onUpdate({inbox: false}),
  120. },
  121. ]),
  122. {
  123. key: 'bookmark',
  124. label: t('Add to Bookmarks'),
  125. hidden: !canAddBookmark,
  126. onAction: () => {
  127. openConfirmModal({
  128. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  129. onConfirm: () => onUpdate({isBookmarked: true}),
  130. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  131. confirmText: label('bookmark'),
  132. });
  133. },
  134. },
  135. {
  136. key: 'remove-bookmark',
  137. label: t('Remove from Bookmarks'),
  138. hidden: !canRemoveBookmark,
  139. onAction: () => {
  140. openConfirmModal({
  141. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  142. onConfirm: () => onUpdate({isBookmarked: false}),
  143. message: confirm({
  144. action: ConfirmAction.UNBOOKMARK,
  145. canBeUndone: false,
  146. append: ' from your bookmarks',
  147. }),
  148. confirmText: label('remove', ' from your bookmarks'),
  149. });
  150. },
  151. },
  152. {
  153. key: 'unresolve',
  154. label: t('Set status to: Unresolved'),
  155. hidden: !canSetUnresolved,
  156. onAction: () => {
  157. openConfirmModal({
  158. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  159. onConfirm: () => onUpdate({status: ResolutionStatus.UNRESOLVED}),
  160. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  161. confirmText: label('unresolve'),
  162. });
  163. },
  164. },
  165. {
  166. key: 'delete',
  167. label: t('Delete'),
  168. priority: 'danger',
  169. disabled: !deleteSupported,
  170. details: deleteDisabledReason,
  171. onAction: () => {
  172. openConfirmModal({
  173. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  174. onConfirm: onDelete,
  175. priority: 'danger',
  176. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  177. confirmText: label('delete'),
  178. });
  179. },
  180. },
  181. ];
  182. return (
  183. <Fragment>
  184. {hasEscalatingIssuesUi && query.includes('is:archived') ? (
  185. <Button
  186. size="xs"
  187. onClick={() => {
  188. openConfirmModal({
  189. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  190. onConfirm: () => onUpdate({status: ResolutionStatus.UNRESOLVED}),
  191. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  192. confirmText: label('unarchive'),
  193. });
  194. }}
  195. disabled={!anySelected}
  196. >
  197. {t('Unarchive')}
  198. </Button>
  199. ) : null}
  200. {hasEscalatingIssuesUi ? (
  201. <ArchiveActions
  202. onUpdate={onUpdate}
  203. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  204. confirmMessage={() =>
  205. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  206. }
  207. confirmLabel={label('archive')}
  208. disabled={ignoreDisabled}
  209. />
  210. ) : null}
  211. {selectedProjectSlug ? (
  212. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  213. {({projects, initiallyLoaded, fetchError}) => {
  214. const selectedProject = projects[0];
  215. return (
  216. <ResolveActions
  217. onShouldConfirm={onShouldConfirm}
  218. onUpdate={onUpdate}
  219. anySelected={anySelected}
  220. params={{
  221. hasReleases: selectedProject.hasOwnProperty('features')
  222. ? (selectedProject as Project).features.includes('releases')
  223. : false,
  224. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  225. ? (selectedProject as Project).latestRelease
  226. : undefined,
  227. projectId: selectedProject.slug,
  228. confirm,
  229. label,
  230. loadingProjects: !initiallyLoaded,
  231. projectFetchError: !!fetchError,
  232. }}
  233. />
  234. );
  235. }}
  236. </Projects>
  237. ) : (
  238. <ResolveActions
  239. onShouldConfirm={onShouldConfirm}
  240. onUpdate={onUpdate}
  241. anySelected={anySelected}
  242. params={{
  243. hasReleases: false,
  244. confirm,
  245. label,
  246. }}
  247. />
  248. )}
  249. {hasEscalatingIssuesUi ? null : (
  250. <IgnoreActions
  251. onUpdate={onUpdate}
  252. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  253. confirmMessage={() =>
  254. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  255. }
  256. confirmLabel={label('ignore')}
  257. disabled={ignoreDisabled}
  258. />
  259. )}
  260. {!nestMergeAndReview && !hasEscalatingIssuesUi && (
  261. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  262. )}
  263. {!nestMergeAndReview && (
  264. <ActionLink
  265. aria-label={t('Merge Selected Issues')}
  266. type="button"
  267. disabled={mergeDisabled}
  268. onAction={onMerge}
  269. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  270. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  271. confirmLabel={label('merge')}
  272. title={makeMergeTooltip()}
  273. >
  274. {t('Merge')}
  275. </ActionLink>
  276. )}
  277. <DropdownMenu
  278. size="sm"
  279. items={menuItems}
  280. triggerProps={{
  281. 'aria-label': t('More issue actions'),
  282. icon: <IconEllipsis size="xs" />,
  283. showChevron: false,
  284. size: 'xs',
  285. }}
  286. isDisabled={!anySelected}
  287. />
  288. </Fragment>
  289. );
  290. }
  291. function isActionSupported(
  292. selectedIssues: BaseGroup[],
  293. actionType: keyof IssueTypeConfig['actions']
  294. ) {
  295. for (const issue of selectedIssues) {
  296. const info = getConfigForIssueType(issue).actions[actionType];
  297. if (!info.enabled) {
  298. return info;
  299. }
  300. }
  301. return {enabled: true};
  302. }
  303. export default ActionSet;