index.tsx 11 KB

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