index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import {Fragment, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import uniq from 'lodash/uniq';
  4. import {bulkDelete, bulkUpdate, mergeGroups} from 'sentry/actionCreators/group';
  5. import {addLoadingMessage, clearIndicators} from 'sentry/actionCreators/indicator';
  6. import Alert from 'sentry/components/alert';
  7. import Checkbox from 'sentry/components/checkbox';
  8. import {t, tct, tn} from 'sentry/locale';
  9. import GroupStore from 'sentry/stores/groupStore';
  10. import SelectedGroupStore from 'sentry/stores/selectedGroupStore';
  11. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  12. import space from 'sentry/styles/space';
  13. import {Group, PageFilters, ResolutionStatusDetails} from 'sentry/types';
  14. import theme from 'sentry/utils/theme';
  15. import useApi from 'sentry/utils/useApi';
  16. import useMedia from 'sentry/utils/useMedia';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import ActionSet from './actionSet';
  19. import Headers from './headers';
  20. import IssueListSortOptions from './sortOptions';
  21. import {
  22. BULK_LIMIT,
  23. BULK_LIMIT_STR,
  24. ConfirmAction,
  25. performanceIssuesSupportsIgnoreAction,
  26. } from './utils';
  27. type IssueListActionsProps = {
  28. allResultsVisible: boolean;
  29. displayCount: React.ReactNode;
  30. displayReprocessingActions: boolean;
  31. groupIds: string[];
  32. onDelete: () => void;
  33. onSelectStatsPeriod: (period: string) => void;
  34. onSortChange: (sort: string) => void;
  35. query: string;
  36. queryCount: number;
  37. selection: PageFilters;
  38. sort: string;
  39. statsPeriod: string;
  40. onActionTaken?: (itemIds: string[]) => void;
  41. onMarkReviewed?: (itemIds: string[]) => void;
  42. };
  43. function IssueListActions({
  44. allResultsVisible,
  45. displayReprocessingActions,
  46. groupIds,
  47. onActionTaken,
  48. onDelete,
  49. onMarkReviewed,
  50. onSelectStatsPeriod,
  51. onSortChange,
  52. queryCount,
  53. query,
  54. selection,
  55. sort,
  56. statsPeriod,
  57. }: IssueListActionsProps) {
  58. const api = useApi();
  59. const organization = useOrganization();
  60. const {
  61. pageSelected,
  62. multiSelected,
  63. anySelected,
  64. allInQuerySelected,
  65. selectedIdsSet,
  66. selectedProjectSlug,
  67. setAllInQuerySelected,
  68. } = useSelectedGroupsState();
  69. const disableActions = useMedia(`(max-width: ${theme.breakpoints.small})`);
  70. const numIssues = selectedIdsSet.size;
  71. function actionSelectedGroups(callback: (itemIds: string[] | undefined) => void) {
  72. const selectedIds = allInQuerySelected
  73. ? undefined // undefined means "all"
  74. : groupIds.filter(itemId => selectedIdsSet.has(itemId));
  75. callback(selectedIds);
  76. SelectedGroupStore.deselectAll();
  77. }
  78. // TODO: Remove this when merging/deleting performance issues is supported
  79. // This silently avoids performance issues for bulk actions
  80. const queryExcludingPerformanceIssues = organization.features.includes(
  81. 'performance-issues'
  82. )
  83. ? `${query ?? ''} !issue.category:performance`
  84. : query;
  85. function handleDelete() {
  86. actionSelectedGroups(itemIds => {
  87. bulkDelete(
  88. api,
  89. {
  90. orgId: organization.slug,
  91. itemIds,
  92. query: queryExcludingPerformanceIssues,
  93. project: selection.projects,
  94. environment: selection.environments,
  95. ...selection.datetime,
  96. },
  97. {
  98. complete: () => {
  99. onDelete();
  100. },
  101. }
  102. );
  103. });
  104. }
  105. function handleMerge() {
  106. actionSelectedGroups(itemIds => {
  107. mergeGroups(
  108. api,
  109. {
  110. orgId: organization.slug,
  111. itemIds,
  112. query: queryExcludingPerformanceIssues,
  113. project: selection.projects,
  114. environment: selection.environments,
  115. ...selection.datetime,
  116. },
  117. {}
  118. );
  119. });
  120. }
  121. function handleUpdate(data?: any) {
  122. const hasIssueListRemovalAction = organization.features.includes(
  123. 'issue-list-removal-action'
  124. );
  125. // TODO: Remove this check when performance issues supports ignoring by time window
  126. const ignoreStatusDetails = data?.statusDetails as
  127. | ResolutionStatusDetails
  128. | undefined;
  129. const unsupportedByPerformanceIssues =
  130. ignoreStatusDetails && !performanceIssuesSupportsIgnoreAction(ignoreStatusDetails);
  131. const bulkUpdateQuery = unsupportedByPerformanceIssues
  132. ? queryExcludingPerformanceIssues
  133. : query;
  134. actionSelectedGroups(itemIds => {
  135. // TODO(Kelly): remove once issue-list-removal-action feature is stable
  136. if (!hasIssueListRemovalAction) {
  137. addLoadingMessage(t('Saving changes\u2026'));
  138. }
  139. if (data?.inbox === false) {
  140. onMarkReviewed?.(itemIds ?? []);
  141. }
  142. onActionTaken?.(itemIds ?? []);
  143. // If `itemIds` is undefined then it means we expect to bulk update all items
  144. // that match the query.
  145. //
  146. // We need to always respect the projects selected in the global selection header:
  147. // * users with no global views requires a project to be specified
  148. // * users with global views need to be explicit about what projects the query will run against
  149. const projectConstraints = {project: selection.projects};
  150. bulkUpdate(
  151. api,
  152. {
  153. orgId: organization.slug,
  154. itemIds,
  155. data,
  156. query: bulkUpdateQuery,
  157. environment: selection.environments,
  158. ...projectConstraints,
  159. ...selection.datetime,
  160. },
  161. {
  162. complete: () => {
  163. if (!hasIssueListRemovalAction) {
  164. clearIndicators();
  165. }
  166. },
  167. }
  168. );
  169. });
  170. }
  171. return (
  172. <Sticky>
  173. <StyledFlex>
  174. {!disableActions && (
  175. <ActionsCheckbox isReprocessingQuery={displayReprocessingActions}>
  176. <Checkbox
  177. onChange={() => SelectedGroupStore.toggleSelectAll()}
  178. checked={pageSelected || (anySelected ? 'indeterminate' : false)}
  179. disabled={displayReprocessingActions}
  180. />
  181. </ActionsCheckbox>
  182. )}
  183. {!displayReprocessingActions && (
  184. <HeaderButtonsWrapper>
  185. {!disableActions && (
  186. <ActionSet
  187. queryCount={queryCount}
  188. query={query}
  189. issues={selectedIdsSet}
  190. allInQuerySelected={allInQuerySelected}
  191. anySelected={anySelected}
  192. multiSelected={multiSelected}
  193. selectedProjectSlug={selectedProjectSlug}
  194. onShouldConfirm={action =>
  195. shouldConfirm(action, {pageSelected, selectedIdsSet})
  196. }
  197. onDelete={handleDelete}
  198. onMerge={handleMerge}
  199. onUpdate={handleUpdate}
  200. />
  201. )}
  202. <IssueListSortOptions sort={sort} query={query} onSelect={onSortChange} />
  203. </HeaderButtonsWrapper>
  204. )}
  205. <Headers
  206. onSelectStatsPeriod={onSelectStatsPeriod}
  207. anySelected={anySelected}
  208. selection={selection}
  209. statsPeriod={statsPeriod}
  210. isReprocessingQuery={displayReprocessingActions}
  211. />
  212. </StyledFlex>
  213. {!allResultsVisible && pageSelected && (
  214. <Alert type="warning" system>
  215. <SelectAllNotice data-test-id="issue-list-select-all-notice">
  216. {allInQuerySelected ? (
  217. queryCount >= BULK_LIMIT ? (
  218. tct(
  219. 'Selected up to the first [count] issues that match this search query.',
  220. {
  221. count: BULK_LIMIT_STR,
  222. }
  223. )
  224. ) : (
  225. tct('Selected all [count] issues that match this search query.', {
  226. count: queryCount,
  227. })
  228. )
  229. ) : (
  230. <Fragment>
  231. {tn(
  232. '%s issue on this page selected.',
  233. '%s issues on this page selected.',
  234. numIssues
  235. )}
  236. <SelectAllLink
  237. onClick={() => setAllInQuerySelected(true)}
  238. data-test-id="issue-list-select-all-notice-link"
  239. >
  240. {queryCount >= BULK_LIMIT
  241. ? tct(
  242. 'Select the first [count] issues that match this search query.',
  243. {
  244. count: BULK_LIMIT_STR,
  245. }
  246. )
  247. : tct('Select all [count] issues that match this search query.', {
  248. count: queryCount,
  249. })}
  250. </SelectAllLink>
  251. </Fragment>
  252. )}
  253. </SelectAllNotice>
  254. </Alert>
  255. )}
  256. </Sticky>
  257. );
  258. }
  259. function useSelectedGroupsState() {
  260. const [allInQuerySelected, setAllInQuerySelected] = useState(false);
  261. const selectedIds = useLegacyStore(SelectedGroupStore);
  262. const selected = SelectedGroupStore.getSelectedIds();
  263. const projects = [...selected]
  264. .map(id => GroupStore.get(id))
  265. .filter((group): group is Group => !!(group && group.project))
  266. .map(group => group.project.slug);
  267. const uniqProjects = uniq(projects);
  268. // we only want selectedProjectSlug set if there is 1 project
  269. // more or fewer should result in a null so that the action toolbar
  270. // can behave correctly.
  271. const selectedProjectSlug = uniqProjects.length === 1 ? uniqProjects[0] : undefined;
  272. const pageSelected = SelectedGroupStore.allSelected();
  273. const multiSelected = SelectedGroupStore.multiSelected();
  274. const anySelected = SelectedGroupStore.anySelected();
  275. const selectedIdsSet = SelectedGroupStore.getSelectedIds();
  276. useEffect(() => {
  277. setAllInQuerySelected(false);
  278. }, [selectedIds]);
  279. return {
  280. pageSelected,
  281. multiSelected,
  282. anySelected,
  283. allInQuerySelected,
  284. selectedIdsSet,
  285. selectedProjectSlug,
  286. setAllInQuerySelected,
  287. };
  288. }
  289. function shouldConfirm(
  290. action: ConfirmAction,
  291. {pageSelected, selectedIdsSet}: {pageSelected: boolean; selectedIdsSet: Set<string>}
  292. ) {
  293. switch (action) {
  294. case ConfirmAction.RESOLVE:
  295. case ConfirmAction.UNRESOLVE:
  296. case ConfirmAction.IGNORE:
  297. case ConfirmAction.UNBOOKMARK: {
  298. return pageSelected && selectedIdsSet.size > 1;
  299. }
  300. case ConfirmAction.BOOKMARK:
  301. return selectedIdsSet.size > 1;
  302. case ConfirmAction.MERGE:
  303. case ConfirmAction.DELETE:
  304. default:
  305. return true; // By default, should confirm ...
  306. }
  307. }
  308. const Sticky = styled('div')`
  309. position: sticky;
  310. z-index: ${p => p.theme.zIndex.issuesList.stickyHeader};
  311. top: -1px;
  312. `;
  313. const StyledFlex = styled('div')`
  314. display: flex;
  315. min-height: 45px;
  316. padding-top: ${space(1)};
  317. padding-bottom: ${space(1)};
  318. align-items: center;
  319. background: ${p => p.theme.backgroundSecondary};
  320. border: 1px solid ${p => p.theme.border};
  321. border-top: none;
  322. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  323. margin: 0 -1px -1px;
  324. `;
  325. const ActionsCheckbox = styled('div')<{isReprocessingQuery: boolean}>`
  326. padding-left: ${space(2)};
  327. margin-bottom: 1px;
  328. & input[type='checkbox'] {
  329. margin: 0;
  330. display: block;
  331. }
  332. ${p => p.isReprocessingQuery && 'flex: 1'};
  333. `;
  334. const HeaderButtonsWrapper = styled('div')`
  335. @media (min-width: ${p => p.theme.breakpoints.large}) {
  336. width: 50%;
  337. }
  338. flex: 1;
  339. margin: 0 ${space(1)};
  340. display: grid;
  341. gap: ${space(0.5)};
  342. grid-auto-flow: column;
  343. justify-content: flex-start;
  344. white-space: nowrap;
  345. `;
  346. const SelectAllNotice = styled('div')`
  347. display: flex;
  348. flex-wrap: wrap;
  349. justify-content: center;
  350. a:not([role='button']) {
  351. color: ${p => p.theme.linkColor};
  352. border-bottom: none;
  353. }
  354. `;
  355. const SelectAllLink = styled('a')`
  356. margin-left: ${space(1)};
  357. `;
  358. export {IssueListActions};
  359. export default IssueListActions;