index.tsx 11 KB

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