actionSet.tsx 8.7 KB

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