projectIssues.tsx 9.4 KB

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