actionSet.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 GuideAnchor from 'sentry/components/assistant/guideAnchor';
  7. import {Button} from 'sentry/components/button';
  8. import {openConfirmModal} from 'sentry/components/confirm';
  9. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  10. import {IconEllipsis} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import GroupStore from 'sentry/stores/groupStore';
  13. import {BaseGroup, GroupStatus, Project} from 'sentry/types';
  14. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  15. import {IssueTypeConfig} from 'sentry/utils/issueTypeConfig/types';
  16. import Projects from 'sentry/utils/projects';
  17. import useMedia from 'sentry/utils/useMedia';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import ResolveActions from './resolveActions';
  20. import ReviewAction from './reviewAction';
  21. import {ConfirmAction, getConfirm, getLabel} from './utils';
  22. type Props = {
  23. allInQuerySelected: boolean;
  24. anySelected: boolean;
  25. issues: Set<string>;
  26. multiSelected: boolean;
  27. onDelete: () => void;
  28. onMerge: () => void;
  29. onShouldConfirm: (action: ConfirmAction) => boolean;
  30. onUpdate: (data?: any) => void;
  31. query: string;
  32. queryCount: number;
  33. selectedProjectSlug?: string;
  34. };
  35. function ActionSet({
  36. queryCount,
  37. query,
  38. allInQuerySelected,
  39. anySelected,
  40. multiSelected,
  41. issues,
  42. onUpdate,
  43. onShouldConfirm,
  44. onDelete,
  45. onMerge,
  46. selectedProjectSlug,
  47. }: Props) {
  48. const organization = useOrganization();
  49. const numIssues = issues.size;
  50. const confirm = getConfirm({
  51. numIssues,
  52. allInQuerySelected,
  53. query,
  54. queryCount,
  55. organization,
  56. });
  57. const label = getLabel(numIssues, allInQuerySelected);
  58. const selectedIssues = [...issues]
  59. .map(issueId => GroupStore.get(issueId))
  60. .filter(issue => issue) as BaseGroup[];
  61. // Merges require multiple issues of a single project type
  62. const multipleIssueProjectsSelected = multiSelected && !selectedProjectSlug;
  63. const {enabled: mergeSupported, disabledReason: mergeDisabledReason} =
  64. isActionSupported(selectedIssues, 'merge');
  65. const {enabled: deleteSupported, disabledReason: deleteDisabledReason} =
  66. isActionSupported(selectedIssues, 'delete');
  67. const mergeDisabled =
  68. !multiSelected || multipleIssueProjectsSelected || !mergeSupported;
  69. const ignoreDisabled = !anySelected;
  70. const canMarkReviewed =
  71. anySelected && (allInQuerySelected || selectedIssues.some(issue => !!issue?.inbox));
  72. // determine which ... dropdown options to show based on issue(s) selected
  73. const canAddBookmark =
  74. allInQuerySelected || selectedIssues.some(issue => !issue.isBookmarked);
  75. const canRemoveBookmark =
  76. allInQuerySelected || selectedIssues.some(issue => issue.isBookmarked);
  77. const canSetUnresolved =
  78. allInQuerySelected ||
  79. selectedIssues.some(
  80. issue => issue.status === 'resolved' || issue.status === 'ignored'
  81. );
  82. const makeMergeTooltip = () => {
  83. if (mergeDisabledReason) {
  84. return mergeDisabledReason;
  85. }
  86. if (multipleIssueProjectsSelected) {
  87. return t('Cannot merge issues from different projects');
  88. }
  89. return '';
  90. };
  91. // Determine whether to nest "Merge" and "Mark as Reviewed" buttons inside
  92. // the dropdown menu based on the current screen size
  93. const theme = useTheme();
  94. const nestMergeAndReview = useMedia(`(max-width: ${theme.breakpoints.xlarge})`);
  95. const hasEscalatingIssuesUi = organization.features.includes('escalating-issues');
  96. const menuItems: MenuItemProps[] = [
  97. {
  98. key: 'merge',
  99. label: t('Merge'),
  100. hidden: !nestMergeAndReview,
  101. disabled: mergeDisabled,
  102. details: makeMergeTooltip(),
  103. onAction: () => {
  104. openConfirmModal({
  105. bypass: !onShouldConfirm(ConfirmAction.MERGE),
  106. onConfirm: onMerge,
  107. message: confirm({action: ConfirmAction.MERGE, canBeUndone: false}),
  108. confirmText: label('merge'),
  109. });
  110. },
  111. },
  112. {
  113. key: 'mark-reviewed',
  114. label: t('Mark Reviewed'),
  115. hidden: !nestMergeAndReview,
  116. disabled: !canMarkReviewed,
  117. onAction: () => onUpdate({inbox: false}),
  118. },
  119. {
  120. key: 'bookmark',
  121. label: t('Add to Bookmarks'),
  122. hidden: !canAddBookmark,
  123. onAction: () => {
  124. openConfirmModal({
  125. bypass: !onShouldConfirm(ConfirmAction.BOOKMARK),
  126. onConfirm: () => onUpdate({isBookmarked: true}),
  127. message: confirm({action: ConfirmAction.BOOKMARK, canBeUndone: false}),
  128. confirmText: label('bookmark'),
  129. });
  130. },
  131. },
  132. {
  133. key: 'remove-bookmark',
  134. label: t('Remove from Bookmarks'),
  135. hidden: !canRemoveBookmark,
  136. onAction: () => {
  137. openConfirmModal({
  138. bypass: !onShouldConfirm(ConfirmAction.UNBOOKMARK),
  139. onConfirm: () => onUpdate({isBookmarked: false}),
  140. message: confirm({
  141. action: ConfirmAction.UNBOOKMARK,
  142. canBeUndone: false,
  143. append: ' from your bookmarks',
  144. }),
  145. confirmText: label('remove', ' from your bookmarks'),
  146. });
  147. },
  148. },
  149. {
  150. key: 'unresolve',
  151. label: t('Set status to: Unresolved'),
  152. hidden: !canSetUnresolved,
  153. onAction: () => {
  154. openConfirmModal({
  155. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  156. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED}),
  157. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  158. confirmText: label('unresolve'),
  159. });
  160. },
  161. },
  162. {
  163. key: 'delete',
  164. label: t('Delete'),
  165. priority: 'danger',
  166. disabled: !deleteSupported,
  167. details: deleteDisabledReason,
  168. onAction: () => {
  169. openConfirmModal({
  170. bypass: !onShouldConfirm(ConfirmAction.DELETE),
  171. onConfirm: onDelete,
  172. priority: 'danger',
  173. message: confirm({action: ConfirmAction.DELETE, canBeUndone: false}),
  174. confirmText: label('delete'),
  175. });
  176. },
  177. },
  178. ];
  179. return (
  180. <Fragment>
  181. {hasEscalatingIssuesUi && query.includes('is:archived') ? (
  182. <Button
  183. size="xs"
  184. onClick={() => {
  185. openConfirmModal({
  186. bypass: !onShouldConfirm(ConfirmAction.UNRESOLVE),
  187. onConfirm: () => onUpdate({status: GroupStatus.UNRESOLVED}),
  188. message: confirm({action: ConfirmAction.UNRESOLVE, canBeUndone: true}),
  189. confirmText: label('unarchive'),
  190. });
  191. }}
  192. disabled={!anySelected}
  193. >
  194. {t('Unarchive')}
  195. </Button>
  196. ) : null}
  197. {selectedProjectSlug ? (
  198. <Projects orgId={organization.slug} slugs={[selectedProjectSlug]}>
  199. {({projects, initiallyLoaded, fetchError}) => {
  200. const selectedProject = projects[0];
  201. return (
  202. <ResolveActions
  203. onShouldConfirm={onShouldConfirm}
  204. onUpdate={onUpdate}
  205. anySelected={anySelected}
  206. params={{
  207. hasRelease: selectedProject.hasOwnProperty('features')
  208. ? (selectedProject as Project).features.includes('releases')
  209. : false,
  210. latestRelease: selectedProject.hasOwnProperty('latestRelease')
  211. ? (selectedProject as Project).latestRelease
  212. : undefined,
  213. projectSlug: selectedProject.slug,
  214. confirm,
  215. label,
  216. loadingProjects: !initiallyLoaded,
  217. projectFetchError: !!fetchError,
  218. }}
  219. />
  220. );
  221. }}
  222. </Projects>
  223. ) : (
  224. <ResolveActions
  225. onShouldConfirm={onShouldConfirm}
  226. onUpdate={onUpdate}
  227. anySelected={anySelected}
  228. params={{
  229. hasRelease: false,
  230. multipleProjectsSelected: true,
  231. disabled: true,
  232. confirm,
  233. label,
  234. }}
  235. />
  236. )}
  237. {hasEscalatingIssuesUi ? (
  238. <GuideAnchor
  239. target="issue_stream_archive_button"
  240. position="bottom"
  241. disabled={ignoreDisabled}
  242. >
  243. <ArchiveActions
  244. onUpdate={onUpdate}
  245. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  246. confirmMessage={() =>
  247. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  248. }
  249. confirmLabel={label('archive')}
  250. disabled={ignoreDisabled}
  251. />
  252. </GuideAnchor>
  253. ) : (
  254. <IgnoreActions
  255. onUpdate={onUpdate}
  256. shouldConfirm={onShouldConfirm(ConfirmAction.IGNORE)}
  257. confirmMessage={() =>
  258. confirm({action: ConfirmAction.IGNORE, canBeUndone: true})
  259. }
  260. confirmLabel={label('ignore')}
  261. disabled={ignoreDisabled}
  262. />
  263. )}
  264. {!nestMergeAndReview && (
  265. <ReviewAction disabled={!canMarkReviewed} onUpdate={onUpdate} />
  266. )}
  267. {!nestMergeAndReview && (
  268. <ActionLink
  269. aria-label={t('Merge Selected Issues')}
  270. type="button"
  271. disabled={mergeDisabled}
  272. onAction={onMerge}
  273. shouldConfirm={onShouldConfirm(ConfirmAction.MERGE)}
  274. message={confirm({action: ConfirmAction.MERGE, canBeUndone: false})}
  275. confirmLabel={label('merge')}
  276. title={makeMergeTooltip()}
  277. >
  278. {t('Merge')}
  279. </ActionLink>
  280. )}
  281. <DropdownMenu
  282. size="sm"
  283. items={menuItems}
  284. triggerProps={{
  285. 'aria-label': t('More issue actions'),
  286. icon: <IconEllipsis />,
  287. showChevron: false,
  288. size: 'xs',
  289. }}
  290. isDisabled={!anySelected}
  291. />
  292. </Fragment>
  293. );
  294. }
  295. function isActionSupported(
  296. selectedIssues: BaseGroup[],
  297. actionType: keyof IssueTypeConfig['actions']
  298. ) {
  299. for (const issue of selectedIssues) {
  300. const info = getConfigForIssueType(issue, issue.project).actions[actionType];
  301. if (!info.enabled) {
  302. return info;
  303. }
  304. }
  305. return {enabled: true};
  306. }
  307. export default ActionSet;