index.tsx 12 KB

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