index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 type {IssueUpdateData} from 'sentry/views/issueList/types';
  22. import {SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY} from 'sentry/views/issueList/utils';
  23. import ActionSet from './actionSet';
  24. import Headers from './headers';
  25. import IssueListSortOptions from './sortOptions';
  26. import {BULK_LIMIT, BULK_LIMIT_STR, ConfirmAction} from './utils';
  27. type IssueListActionsProps = {
  28. allResultsVisible: boolean;
  29. displayReprocessingActions: boolean;
  30. groupIds: string[];
  31. onDelete: () => void;
  32. onSelectStatsPeriod: (period: string) => void;
  33. onSortChange: (sort: string) => void;
  34. query: string;
  35. queryCount: number;
  36. selection: PageFilters;
  37. sort: string;
  38. statsPeriod: string;
  39. onActionTaken?: (itemIds: string[], data: IssueUpdateData) => void;
  40. };
  41. function IssueListActions({
  42. allResultsVisible,
  43. displayReprocessingActions,
  44. groupIds,
  45. onActionTaken,
  46. onDelete,
  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.xlarge : theme.breakpoints.medium
  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. if (selection.projects[0]) {
  121. const trackProject = ProjectsStore.getById(`${selection.projects[0]}`);
  122. trackAnalytics('issues_stream.merged', {
  123. organization,
  124. project_id: trackProject?.id,
  125. platform: trackProject?.platform,
  126. items_merged: allInQuerySelected ? 'all_in_query' : itemIds?.length,
  127. });
  128. }
  129. });
  130. }
  131. function handleUpdate(data: IssueUpdateData) {
  132. if ('status' in data && data.status === 'ignored') {
  133. const statusDetails =
  134. 'ignoreCount' in data.statusDetails
  135. ? 'ignoreCount'
  136. : 'ignoreDuration' in data.statusDetails
  137. ? 'ignoreDuration'
  138. : 'ignoreUserCount' in data.statusDetails
  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 `itemIds` is undefined then it means we expect to bulk update all items
  149. // that match the query.
  150. //
  151. // We need to always respect the projects selected in the global selection header:
  152. // * users with no global views requires a project to be specified
  153. // * users with global views need to be explicit about what projects the query will run against
  154. const projectConstraints = {project: selection.projects};
  155. bulkUpdate(
  156. api,
  157. {
  158. orgId: organization.slug,
  159. itemIds,
  160. data,
  161. query,
  162. environment: selection.environments,
  163. ...projectConstraints,
  164. ...selection.datetime,
  165. },
  166. {
  167. complete: () => {
  168. onActionTaken?.(itemIds ?? [], data);
  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?.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.SET_PRIORITY:
  301. case ConfirmAction.UNBOOKMARK: {
  302. return pageSelected && selectedIdsSet.size > 1;
  303. }
  304. case ConfirmAction.BOOKMARK:
  305. return selectedIdsSet.size > 1;
  306. case ConfirmAction.MERGE:
  307. case ConfirmAction.DELETE:
  308. default:
  309. return true; // By default, should confirm ...
  310. }
  311. }
  312. const StickyActions = styled(Sticky)`
  313. z-index: ${p => p.theme.zIndex.issuesList.stickyHeader};
  314. /* Remove border radius from the action bar when stuck. Without this there is
  315. * a small gap where color can peek through. */
  316. &[data-stuck] > div {
  317. border-radius: 0;
  318. }
  319. `;
  320. const ActionsBar = styled('div')`
  321. display: flex;
  322. min-height: 45px;
  323. padding-top: ${space(1)};
  324. padding-bottom: ${space(1)};
  325. align-items: center;
  326. background: ${p => p.theme.backgroundSecondary};
  327. border: 1px solid ${p => p.theme.border};
  328. border-top: none;
  329. border-radius: ${p => p.theme.panelBorderRadius} ${p => p.theme.panelBorderRadius} 0 0;
  330. margin: 0 -1px -1px;
  331. `;
  332. const ActionsCheckbox = styled('div')<{isReprocessingQuery: boolean}>`
  333. display: flex;
  334. align-items: center;
  335. padding-left: ${space(2)};
  336. margin-bottom: 1px;
  337. ${p => p.isReprocessingQuery && 'flex: 1'};
  338. `;
  339. const HeaderButtonsWrapper = styled('div')`
  340. @media (min-width: ${p => p.theme.breakpoints.large}) {
  341. width: 50%;
  342. }
  343. flex: 1;
  344. margin: 0 ${space(1)};
  345. display: grid;
  346. gap: ${space(0.5)};
  347. grid-auto-flow: column;
  348. justify-content: flex-start;
  349. white-space: nowrap;
  350. `;
  351. const SelectAllNotice = styled('div')`
  352. display: flex;
  353. flex-wrap: wrap;
  354. justify-content: center;
  355. a:not([role='button']) {
  356. color: ${p => p.theme.linkColor};
  357. border-bottom: none;
  358. }
  359. `;
  360. const SelectAllLink = styled('a')`
  361. margin-left: ${space(1)};
  362. `;
  363. export {IssueListActions};
  364. export default IssueListActions;