projectIssues.tsx 8.9 KB

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