index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. trackAnalytics('issues_stream.archived', {
  124. status_details: data.statusDetails,
  125. substatus: data.substatus,
  126. organization,
  127. });
  128. }
  129. actionSelectedGroups(itemIds => {
  130. if (data?.inbox === false) {
  131. onMarkReviewed?.(itemIds ?? []);
  132. }
  133. onActionTaken?.(itemIds ?? []);
  134. // If `itemIds` is undefined then it means we expect to bulk update all items
  135. // that match the query.
  136. //
  137. // We need to always respect the projects selected in the global selection header:
  138. // * users with no global views requires a project to be specified
  139. // * users with global views need to be explicit about what projects the query will run against
  140. const projectConstraints = {project: selection.projects};
  141. bulkUpdate(
  142. api,
  143. {
  144. orgId: organization.slug,
  145. itemIds,
  146. data,
  147. query,
  148. environment: selection.environments,
  149. ...projectConstraints,
  150. ...selection.datetime,
  151. },
  152. {}
  153. );
  154. });
  155. }
  156. return (
  157. <Sticky>
  158. <StyledFlex>
  159. {!disableActions && (
  160. <ActionsCheckbox isReprocessingQuery={displayReprocessingActions}>
  161. <Checkbox
  162. onChange={() => SelectedGroupStore.toggleSelectAll()}
  163. checked={pageSelected || (anySelected ? 'indeterminate' : false)}
  164. disabled={displayReprocessingActions}
  165. />
  166. </ActionsCheckbox>
  167. )}
  168. {!displayReprocessingActions && (
  169. <HeaderButtonsWrapper>
  170. {!disableActions && (
  171. <ActionSet
  172. queryCount={queryCount}
  173. query={query}
  174. issues={selectedIdsSet}
  175. allInQuerySelected={allInQuerySelected}
  176. anySelected={anySelected}
  177. multiSelected={multiSelected}
  178. selectedProjectSlug={selectedProjectSlug}
  179. onShouldConfirm={action =>
  180. shouldConfirm(action, {pageSelected, selectedIdsSet})
  181. }
  182. onDelete={handleDelete}
  183. onMerge={handleMerge}
  184. onUpdate={handleUpdate}
  185. />
  186. )}
  187. <IssueListSortOptions sort={sort} query={query} onSelect={onSortChange} />
  188. </HeaderButtonsWrapper>
  189. )}
  190. <Headers
  191. onSelectStatsPeriod={onSelectStatsPeriod}
  192. anySelected={anySelected}
  193. selection={selection}
  194. statsPeriod={statsPeriod}
  195. isReprocessingQuery={displayReprocessingActions}
  196. isSavedSearchesOpen={isSavedSearchesOpen}
  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.panelBorderRadius} ${p => p.theme.panelBorderRadius} 0 0;
  309. margin: 0 -1px -1px;
  310. `;
  311. const ActionsCheckbox = styled('div')<{isReprocessingQuery: boolean}>`
  312. display: flex;
  313. align-items: center;
  314. padding-left: ${space(2)};
  315. margin-bottom: 1px;
  316. ${p => p.isReprocessingQuery && 'flex: 1'};
  317. `;
  318. const HeaderButtonsWrapper = styled('div')`
  319. @media (min-width: ${p => p.theme.breakpoints.large}) {
  320. width: 50%;
  321. }
  322. flex: 1;
  323. margin: 0 ${space(1)};
  324. display: grid;
  325. gap: ${space(0.5)};
  326. grid-auto-flow: column;
  327. justify-content: flex-start;
  328. white-space: nowrap;
  329. `;
  330. const SelectAllNotice = styled('div')`
  331. display: flex;
  332. flex-wrap: wrap;
  333. justify-content: center;
  334. a:not([role='button']) {
  335. color: ${p => p.theme.linkColor};
  336. border-bottom: none;
  337. }
  338. `;
  339. const SelectAllLink = styled('a')`
  340. margin-left: ${space(1)};
  341. `;
  342. export {IssueListActions};
  343. export default IssueListActions;