index.tsx 11 KB

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