projectIssues.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import pick from 'lodash/pick';
  5. import * as qs from 'query-string';
  6. import type {Client} from 'sentry/api';
  7. import {LinkButton} from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import DiscoverButton from 'sentry/components/discoverButton';
  10. import GroupList from 'sentry/components/issues/groupList';
  11. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  12. import Pagination from 'sentry/components/pagination';
  13. import Panel from 'sentry/components/panels/panel';
  14. import PanelBody from 'sentry/components/panels/panelBody';
  15. import QueryCount from 'sentry/components/queryCount';
  16. import {SegmentedControl} from 'sentry/components/segmentedControl';
  17. import {DEFAULT_RELATIVE_PERIODS, DEFAULT_STATS_PERIOD} from 'sentry/constants';
  18. import {URL_PARAM} from 'sentry/constants/pageFilters';
  19. import {t, tct} from 'sentry/locale';
  20. import {space} from 'sentry/styles/space';
  21. import type {Organization} from 'sentry/types/organization';
  22. import {trackAnalytics} from 'sentry/utils/analytics';
  23. import {browserHistory} from 'sentry/utils/browserHistory';
  24. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  25. import {decodeScalar} from 'sentry/utils/queryString';
  26. import {appendQueryDatasetParam} from 'sentry/views/dashboards/utils';
  27. import NoGroupsHandler from '../issueList/noGroupsHandler';
  28. enum IssuesType {
  29. NEW = 'new',
  30. UNHANDLED = 'unhandled',
  31. REGRESSED = 'regressed',
  32. RESOLVED = 'resolved',
  33. ALL = 'all',
  34. }
  35. enum IssuesQuery {
  36. NEW = 'is:unresolved is:for_review',
  37. UNHANDLED = 'error.unhandled:true is:unresolved',
  38. REGRESSED = 'regressed_in_release:latest',
  39. RESOLVED = 'is:resolved',
  40. ALL = '',
  41. }
  42. type Count = {
  43. all: number;
  44. new: number;
  45. regressed: number;
  46. resolved: number;
  47. unhandled: number;
  48. };
  49. type Props = {
  50. api: Client;
  51. location: Location;
  52. organization: Organization;
  53. projectId: number;
  54. query?: string;
  55. };
  56. function ProjectIssues({organization, location, projectId, query, api}: Props) {
  57. const [pageLinks, setPageLinks] = useState<string | undefined>();
  58. const [onCursor, setOnCursor] = useState<(() => void) | undefined>();
  59. const [issuesType, setIssuesType] = useState<IssuesType>(
  60. (location.query.issuesType as IssuesType) || IssuesType.UNHANDLED
  61. );
  62. const [issuesCount, setIssuesCount] = useState<Count>({
  63. all: 0,
  64. new: 0,
  65. regressed: 0,
  66. resolved: 0,
  67. unhandled: 0,
  68. });
  69. const fetchIssuesCount = useCallback(async () => {
  70. const getIssueCountEndpoint = queryParameters => {
  71. const issuesCountPath = `/organizations/${organization.slug}/issues-count/`;
  72. return `${issuesCountPath}?${qs.stringify(queryParameters)}`;
  73. };
  74. const params = [
  75. `${IssuesQuery.NEW}`,
  76. `${IssuesQuery.ALL}`,
  77. `${IssuesQuery.RESOLVED}`,
  78. `${IssuesQuery.UNHANDLED}`,
  79. `${IssuesQuery.REGRESSED}`,
  80. ];
  81. const queryParams = params.map(param => param);
  82. const queryParameters = {
  83. project: projectId,
  84. query: queryParams,
  85. ...(!location.query.start && {
  86. statsPeriod: location.query.statsPeriod || DEFAULT_STATS_PERIOD,
  87. }),
  88. start: location.query.start,
  89. end: location.query.end,
  90. environment: location.query.environment,
  91. cursor: location.query.cursor,
  92. };
  93. const issueCountEndpoint = getIssueCountEndpoint(queryParameters);
  94. try {
  95. const data = await api.requestPromise(issueCountEndpoint);
  96. setIssuesCount({
  97. all: data[`${IssuesQuery.ALL}`] || 0,
  98. new: data[`${IssuesQuery.NEW}`] || 0,
  99. resolved: data[`${IssuesQuery.RESOLVED}`] || 0,
  100. unhandled: data[`${IssuesQuery.UNHANDLED}`] || 0,
  101. regressed: data[`${IssuesQuery.REGRESSED}`] || 0,
  102. });
  103. } catch {
  104. // do nothing
  105. }
  106. }, [
  107. api,
  108. location.query.cursor,
  109. location.query.end,
  110. location.query.environment,
  111. location.query.start,
  112. location.query.statsPeriod,
  113. organization.slug,
  114. projectId,
  115. ]);
  116. useEffect(() => {
  117. fetchIssuesCount();
  118. }, [fetchIssuesCount]);
  119. function handleOpenInIssuesClick() {
  120. trackAnalytics('project_detail.open_issues', {organization});
  121. }
  122. function handleOpenInDiscoverClick() {
  123. trackAnalytics('project_detail.open_discover', {organization});
  124. }
  125. function handleFetchSuccess(groupListState, cursorHandler) {
  126. setPageLinks(groupListState.pageLinks);
  127. setOnCursor(() => cursorHandler);
  128. }
  129. const discoverQuery =
  130. issuesType === 'unhandled'
  131. ? ['event.type:error error.unhandled:true', query].join(' ').trim()
  132. : ['event.type:error', query].join(' ').trim();
  133. function getDiscoverUrl() {
  134. return {
  135. pathname: `/organizations/${organization.slug}/discover/results/`,
  136. query: {
  137. name: t('Frequent Unhandled Issues'),
  138. field: ['issue', 'title', 'count()', 'count_unique(user)', 'project'],
  139. sort: '-count',
  140. query: discoverQuery,
  141. display: 'top5',
  142. ...normalizeDateTimeParams(pick(location.query, [...Object.values(URL_PARAM)])),
  143. ...appendQueryDatasetParam(organization, SavedQueryDatasets.ERRORS),
  144. },
  145. };
  146. }
  147. const endpointPath = `/organizations/${organization.slug}/issues/`;
  148. const issueQuery = (Object.values(IssuesType) as string[]).includes(issuesType)
  149. ? [`${IssuesQuery[issuesType.toUpperCase()]}`, query].join(' ').trim()
  150. : [`${IssuesQuery.ALL}`, query].join(' ').trim();
  151. const queryParams = {
  152. limit: '5',
  153. ...normalizeDateTimeParams(
  154. pick(location.query, [...Object.values(URL_PARAM), 'cursor'])
  155. ),
  156. query: issueQuery,
  157. sort: 'freq',
  158. };
  159. const issueSearch = {
  160. pathname: endpointPath,
  161. query: queryParams,
  162. };
  163. function handleIssuesTypeSelection(issueType: IssuesType) {
  164. const to = {
  165. ...location,
  166. query: {
  167. ...location.query,
  168. issuesType: issueType,
  169. },
  170. };
  171. browserHistory.replace(to);
  172. setIssuesType(issueType);
  173. }
  174. function renderEmptyMessage() {
  175. const selectedTimePeriod = location.query.start
  176. ? null
  177. : DEFAULT_RELATIVE_PERIODS[
  178. decodeScalar(location.query.statsPeriod, DEFAULT_STATS_PERIOD)
  179. ];
  180. const displayedPeriod = selectedTimePeriod
  181. ? selectedTimePeriod.toLowerCase()
  182. : t('given timeframe');
  183. return (
  184. <Panel>
  185. <PanelBody>
  186. <NoGroupsHandler
  187. api={api}
  188. organization={organization}
  189. query={issueQuery}
  190. selectedProjectIds={[projectId]}
  191. groupIds={[]}
  192. emptyMessage={tct('No [issuesType] issues for the [timePeriod].', {
  193. issuesType: issuesType === 'all' ? '' : issuesType,
  194. timePeriod: displayedPeriod,
  195. })}
  196. />
  197. </PanelBody>
  198. </Panel>
  199. );
  200. }
  201. const issuesTypes = [
  202. {value: IssuesType.ALL, label: t('All Issues'), issueCount: issuesCount.all},
  203. {value: IssuesType.NEW, label: t('New Issues'), issueCount: issuesCount.new},
  204. {
  205. value: IssuesType.UNHANDLED,
  206. label: t('Unhandled'),
  207. issueCount: issuesCount.unhandled,
  208. },
  209. {
  210. value: IssuesType.REGRESSED,
  211. label: t('Regressed'),
  212. issueCount: issuesCount.regressed,
  213. },
  214. {
  215. value: IssuesType.RESOLVED,
  216. label: t('Resolved'),
  217. issueCount: issuesCount.resolved,
  218. },
  219. ];
  220. return (
  221. <Fragment>
  222. <ControlsWrapper>
  223. <SegmentedControl
  224. aria-label={t('Issue type')}
  225. value={issuesType}
  226. onChange={value => handleIssuesTypeSelection(value)}
  227. size="xs"
  228. >
  229. {issuesTypes.map(({value, label, issueCount}) => (
  230. <SegmentedControl.Item key={value} textValue={label}>
  231. {label}&nbsp;
  232. <QueryCount count={issueCount} max={99} hideParens hideIfEmpty={false} />
  233. </SegmentedControl.Item>
  234. ))}
  235. </SegmentedControl>
  236. <OpenInButtonBar gap={1}>
  237. <LinkButton
  238. data-test-id="issues-open"
  239. size="xs"
  240. to={issueSearch}
  241. onClick={handleOpenInIssuesClick}
  242. >
  243. {t('Open in Issues')}
  244. </LinkButton>
  245. <DiscoverButton
  246. onClick={handleOpenInDiscoverClick}
  247. to={getDiscoverUrl()}
  248. size="xs"
  249. >
  250. {t('Open in Discover')}
  251. </DiscoverButton>
  252. <StyledPagination pageLinks={pageLinks} onCursor={onCursor} size="xs" />
  253. </OpenInButtonBar>
  254. </ControlsWrapper>
  255. <GroupList
  256. orgSlug={organization.slug}
  257. queryParams={queryParams}
  258. canSelectGroups={false}
  259. renderEmptyMessage={renderEmptyMessage}
  260. withChart={false}
  261. withPagination={false}
  262. onFetchSuccess={handleFetchSuccess}
  263. source="project"
  264. />
  265. </Fragment>
  266. );
  267. }
  268. const ControlsWrapper = styled('div')`
  269. display: flex;
  270. align-items: flex-end;
  271. justify-content: space-between;
  272. margin-bottom: ${space(1)};
  273. flex-wrap: wrap;
  274. `;
  275. const OpenInButtonBar = styled(ButtonBar)`
  276. margin-top: ${space(1)};
  277. @media (max-width: ${p => p.theme.breakpoints.small}) {
  278. width: 100%;
  279. }
  280. `;
  281. const StyledPagination = styled(Pagination)`
  282. margin: 0;
  283. `;
  284. export default ProjectIssues;